【发布时间】: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