What does fs[:, i, :] = f do?

Hello, I am trying action recognition. If I understand correctly the code `f = f.view(f.size(0), -1)` flattens the tensor but for `fs[:, i, :] = f`, I can’t seem to guess what it is doing there.

``````  def forward(self, inputs, hidden=None, steps=0):
length = len(inputs)
fs = torch.zeros(inputs[0].size(0), length, self.rnn.input_size).cuda()

for i in range(length):
f = inputs[i]
f = f.view(f.size(0), -1)
f = self.fc_pre(f)
fs[:, i, :] = f
``````

`fs[:, i, :] = f` assigns the tensor `f` in the shape `[fs.size(0), fs.size(2)]` to each “row” in `fs`.
Here is a small example where `f` is containing increasing values for the sake of simplicity:

``````fs = torch.randn(2, 3, 4)
f = torch.randn(2, 4)

for i in range(fs.size(1)):
f = torch.ones(fs.size(0), fs.size(2)) * i
print(f)
fs[:, i, :] = f

# tensor([[0., 0., 0., 0.],
#         [0., 0., 0., 0.]])
# tensor([[1., 1., 1., 1.],
#         [1., 1., 1., 1.]])
# tensor([[2., 2., 2., 2.],
#         [2., 2., 2., 2.]])

print(fs)
# tensor([[[0., 0., 0., 0.],
#          [1., 1., 1., 1.],
#          [2., 2., 2., 2.]],

#         [[0., 0., 0., 0.],
#          [1., 1., 1., 1.],
#          [2., 2., 2., 2.]]])
``````
1 Like

Thank you for the explanation @ptrblck