我正在尝试为 Pytorch LSTM 实现 Optuna 超参数优化。但是我不知道如何正确定义我的模型。
当我只使用 nn.linear
时,一切正常,但是当我使用 nn.LSTMCell
时,我会遇到以下错误:
AttributeError: 'tuple' object has no attribute 'dim'
错误是由于LSTM返回的是一个元组而不是张量所引起的。但我不知道如何修复它,也找不到一个在Pytorch中使用Optuna优化的LSTM的示例。
以下是模型定义:
def build_model_custom(trail):
# Suggest the number of layers of neural network model
n_layers = trail.suggest_int("n_layers", 1, 3)
layers = []
in_features = 20
for i in range(n_layers):
# Suggest the number of units in each layer
out_features = trail.suggest_int("n_units_l{}".format(i), 4, 18)
layers.append(nn.LSTMCell(in_features, out_features))
in_features = out_features
layers.append(nn.Linear(in_features, 2))
return nn.Sequential(*layers)