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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.