[Solved] How to create a Variable from a python list of Variables

Hi,

I am quite new to PyTorch and try to build my own loss function. The key component in my model is the ability to create a max/min/mean of losses. Specifically, suppose we have a python list of losses, each of which being a PyTorch Variable:

losses = [Variable(torch.randn(1)) for _ in xrange(5)]

Is there a way for me to create the mean loss of this python list of losses? Note that the mean loss should also have type as Variable as at the end I need to call backward to compute the gradient.

You can use the builtin pytjon operator sum for that.

total_loss = sum(losses) / len(losses)
1 Like

Another way would be to concatenate all the losses into one tensor, and call the operation you want.

cat_losses = torch.cat(losses, 0)
mean_loss = cat_losses.mean()
...

Both approaches work with autograd

That works! Thanks a lot!!