【问题标题】:i want to convert the code below(neural network) from keras to pytorch我想将下面的代码(神经网络)从 keras 转换为 pytorch
【发布时间】:2019-12-07 03:55:03
【问题描述】:

我想使用 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

火炬

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)

【问题讨论】:

  • 您是否遇到了某种错误?转换有什么问题?
  • __init__() 缺少 1 个必需的位置参数:'input_dim',这是运行训练时的错误,奇怪的是我正在打印它以调试 input_dim 值为 10
  • 因为您在调用model = Model() 时需要input_dim,所以您需要提供一个参数,它应该是model = Model(<integer dimensions for your network>)。当您调用 Model() 时,它会调用 Model 调用的构造函数,即 __init__(self, input_dim)
  • 是的,谢谢,这解决了问题,我想问你另一个问题,在代理类 'self.model.predict(next_state)' 的 pytorch 中什么是等效的。它说模型对象没有属性预测
  • 因为您的模型类中没有一个名为 predict 的函数,所以它不知道 model.predict(next_state) 的含义

标签: python machine-learning keras pytorch


【解决方案1】:

model = Model()

调用Model() 时需要提供参数,因为您的__init__(self, input_dims) 需要参数。

应该是这样的:

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

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2019-08-18
    • 2014-01-15
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2017-05-24
    • 2018-12-16
    • 2013-05-17
    相关资源
    最近更新 更多