Using dicts and lists for Variables in cuda

Hi,

since my model has a variety of qualitatively different outputs, I would like to do something like this:

  1. Return my predictions from the model in a dictionary with keywoards that indicate the prediction.
  2. Pass that dictionary on to an autograd function that checks whether a keywoard exists and if so, passes it on to a corresponding loss.

Is there any reason this is a bad idea? Is it generally advisable to store cuda() variables such as predictions or losses in lists or dictionaries?

I tried both and didnt get any errors or anything, but that of course does not necessarily mean that it is a good idea…

Thanks for your advice!

That is the nice thing with having everything working just with Variables, you can move them around whatever way you want (plain variable, in a list, in a dictionnary, in a custom class…) and it will just work :slight_smile:

So no problem with doing that !

1 Like

Great, thank you! I didn’t experience any kind of slow down or such, so I figured, but since I am rather new to PyTorch and Python, I very much appreciate the assurance!

A Variable, like any instance of a class in Python isn’t actually copied when you add it to a list, the list just contains references to the Variables that you put into it. The underlying storage used for the data doesn’t get moved or copied.

1 Like