why don’t you directly sum the losses instead of stacking them into a tensor ?

loss_sum = 0

for i in range(100):
     result_state = play_episode()
     loss = mse_loss(result_state, target_state)
     loss_sum += reward

optimizer.zero_grad()
loss_sum.backward()
optimizer.step()


we need more information about your code (maybe somewhere your computation graph could be alleviated for the backward pass)