Name of the optimizer being used

I’m trying to display the optimizer currently in use by the model for documentation purposes.

So the following snippet

optimizer = torch.optim.SGD(model.parameters(), lr=1e-5)
print(optimizer)

gives the following output

SGD (
Parameter Group 0
    dampening: 0
    lr: 1e-05
    momentum: 0
    nesterov: False
    weight_decay: 0
)

Is there a way in which I could just get SGD as the output (and same for other optimizers also)?
I figured I could take the substring of the displayed output till the first parenthesis. Is there a cleaner way of doing this?

1 Like

Hi Nihal!

print (type (optimizer).__name__)

will do what you want.

(This is generally true for python objects, and is not specific to pytorch.)

Best.

K. Frank

3 Likes