0

How can I write the below equivalent code of Keras Neural Net in Pytorch?

actor = Sequential()
        actor.add(Dense(20, input_dim=9, activation='relu', kernel_initializer='he_uniform'))
        actor.add(Dense(20, activation='relu'))
        actor.add(Dense(27, activation='softmax', kernel_initializer='he_uniform'))
        actor.summary()
        # See note regarding crossentropy in cartpole_reinforce.py
        actor.compile(loss='categorical_crossentropy',
                      optimizer=Adam(lr=self.actor_lr))[Please find the image eq here.][1]


  [1]: https://i.stack.imgur.com/gJviP.png
Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
Murtaza
  • 413
  • 1
  • 3
  • 13

1 Answers1

2

Similiar questions have already been asked, but here it goes:

import torch

actor = torch.nn.Sequential(
    torch.nn.Linear(9, 20), # output shape has to be specified
    torch.nn.ReLU(),
    torch.nn.Linear(20, 20), # same goes over here
    torch.nn.ReLU(),
    torch.nn.Linear(20, 27), # and here
    torch.nn.Softmax(),
)

print(actor)

Initialization: By default, from version 1.0 onward, linear layers will be initialized with Kaiming Uniform (see this post). If you want to initialize your weights differently, see most upvoted answer to this question.

You may also use Python's OrderedDict to match certain layers easier, see Pytorch's documentation, you should be able to proceed from there.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83