繁体   English   中英

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

[英]How can I write the below equivalent code of Keras Neural Net in 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_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轻松匹配某些图层,请参阅Pytorch的文档 ,您应该可以从那里继续进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM