Reputation: 2489
I am trying to convert my current code, which assigns tensors in place, to an outer operation.
Meaning currently the code is
self.X[:, nc:] = D
Where D is in the same shape as self.X[:, nc:]
But I would like to convert it to
sliced_index = ~ somehow create an indexed tensor from self.X[:, nc:]
self.X = self.X.scatter(1,sliced_index,mm(S_, Z[:, :n - nc]))
And don't know how to create that index mask tensor that represents only the entries in the sliced tensor
Minimal example:
a = [[0,1,2],[3,4,5]]
D = [[6],[7]]
Not_in_place = [[0,1,6],[3,4,7]]
Upvotes: 3
Views: 3441
Reputation: 22294
A masked scatter is a little easier. The mask itself can be computed as an in-place operation after which you can use masked_scatter
mask = torch.zeros(self.X.shape, device=self.X.device, dtype=torch.bool)
mask[:, nc:] = True
self.X = self.X.masked_scatter(mask, D)
A more specialized version which relies on broadcasting but should be more efficient would be
mask = torch.zeros([1, self.X.size(1)], device=self.X.device, dtype=torch.bool)
mask[0, nc:] = True
self.X = self.X.masked_scatter(mask, D)
Upvotes: 4
Reputation: 7723
Use Tensor.clone
to copy tensor.
a = torch.tensor([[0,1,2],[3,4,5]])
D = torch.tensor([[6],[7]])
n, n[:,-1:] = a.clone(), D
n
tensor([[0, 1, 6],
[3, 4, 7]])
a
tensor([[0, 1, 2],
[3, 4, 5]])
Upvotes: 1