Error module 'pyro' has no attribute 'optim'

When I want to use optimizer I got an error
“AttributeError: module ‘pyro’ has no attribute ‘optim’”
. Then, I checked using dir(pyro) I can not find optim as an attribute. But when I used import pyro.optim now I can use pyro.optim and suddenly dir(pyro) showed optim as the attribute. This is the screenshot:

Is this a bug? I am using pyro 0.2.0 and pytorch 0.4.

Not all pyro modules are exposed from the top-level pyro module. When you call import pyro.optim, it loads all the classes defined in the pyro.optim.__init__.py file. That’s why you see dir(pyro) get populated with the optim module, after the import call.

So you will need to explicitly call:

from pyro.optim import Adam

or

import pyro.optim as optim

optim.Adam({'lr': 0.01})
2 Likes

If anyone is having trouble importing pyro.optim.Adam in PyCharm, use Spyder instead

And I will beg to the developers to look upon this bug

I think the only reason its working on Spyder is that it is loading all modules by default. Why not simply from pyro.optim import Adam? I am open to exposing all optimizers from the top module itself too if there is a compelling reason.

Hi! Thank you so much for your reply.
So yesterday neither "from pyro.optim import Adam did not work either " or “from torch.optim import Adam” worked in PyCharm.
With this example:Gaussian Mixture Model — Pyro Tutorials 1.8.4 documentation.

I runned it again today with “from torch.optim import Adam”, and now it works. This is a mistery to my beginner self.
At least we have several possible solutions,

Best

this seems like a problem with your IDE and not related to pyro. from pyro.optim import Adam and import pyro.optim should always work. you can verify that by going into the REPL and running that command. anything else isn’t guaranteed to work. note that you have to use pyro’s optimizer not torch’s in SVI.