繁体   English   中英

我想将下面的代码(神经网络)从keras转换为pytorch

[英]i want to convert the code below(neural network) from keras to pytorch

我想用pytorch代替keras,但我自己做不到

Keras

def _model(self):
    model = Sequential()
    model.add(Dense(units=64, input_dim=self.state_size, 
    activation="relu"))
    model.add(Dense(units=32, activation="relu"))
    model.add(Dense(units=8, activation="relu"))
    model.add(Dense(self.action_size, activation="linear"))
    model.compile(loss="mse", optimizer=Adam(lr=0.001))

    return model

Pytorch

class Model(nn.Module):
    def __init__(self, input_dim):
        super(Model, self).__init__()
        self.fc1 = nn.ReLU(input_dim, 64)
        self.fc2 = nn.ReLU(64,32)
        self.fc3 = nn.Relu(32, 8)
        self.fc4 = nn.Linear(8, 3)
model = Model()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

model = Model()

由于__init__(self, input_dims)需要一个参数__init__(self, input_dims)在调用Model()时需要提供一个参数。

应该是这样的:

model = Model(<integer dimensions for your network>)

暂无
暂无

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

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