Python 如何在Pytorch中编写Keras神经网络的以下等效代码?

Python 如何在Pytorch中编写Keras神经网络的以下等效代码?,python,python-3.x,keras,pytorch,Python,Python 3.x,Keras,Pytorch,如何在Pytorch中编写Keras神经网络的以下等效代码 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_

如何在Pytorch中编写Keras神经网络的以下等效代码

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

已经有人问过类似的问题,但这里是这样的:

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)
初始化:默认情况下,从版本1.0开始,线性层将使用Kaiming Uniform初始化(请参阅)。如果您想以不同的方式初始化权重,请参阅对的最上乘答案


您也可以使用Python的
OrderedDict
更容易地匹配某些层,请参见,您应该能够从那里开始。已经有人问过类似的问题,但这里是:

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)
初始化:默认情况下,从版本1.0开始,线性层将使用Kaiming Uniform初始化(请参阅)。如果您想以不同的方式初始化权重,请参阅对的最上乘答案


您还可以使用Python的
OrderedDict
更容易地匹配某些层,请参见,您应该能够从那里开始。

到目前为止您尝试了什么?到目前为止您尝试了什么?