Create tensor from key, value, shape

Hey there,

I would like to get a vector from two vectors indices and values.
It sounds like a sparse vector (give values and indices, or implicitly use 0), but I didn’t find much doc about it and failed this way.

I looked about scatter that does not helps. In fact I would like a kind of fill_values similar to existing fill but with a vector as argument, not a scalar.


In order words:

Given two vectors:

  • v FloatTensor, [n, 1]
  • i LongTensor, [n, 1]

I would like to make a FloatTensor r [dim, 1] such as:

  • for each j in [0, n[: r[i[j]] = v[j] (A)
  • dim is known, and for each i in [0, n[ we have dim > i[j]

Since I haven’t found any built-in function I tried to do it with loops, starting with a zeros(dim) vector, iterating over j and using (A) to copy the value, but I’m facing an error:

~RuntimeError: copy from Variable to torch.FloatTensor isn't implemented~
Edit: using .data solves this, it was in fact a variable, not a tensor my bad here.

Are loops the only way to do it? I’m not sure how efficient it would be.

Thx