是否有人为Pytorch LSTM实施了Optuna超参数优化?
我正在尝试针对Pytorch LSTM实现Optuna超参数优化。但是我不知道如何正确定义我的模型。 当我只使用nn.lin.linear
Erverything工作正常时,但是当我使用nn.lstmcell
时,我会收到以下错误:
AttributeError: 'tuple' object has no attribute 'dim'
由于LSTM返回Tupel而不是tupel,因此会增加错误张量。但是我不知道如何修复它,也找不到具有Optuna优化的Pytorch 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)
I am trying to implemented a Optuna Hyperparameter optimization for a Pytorch LSTM. But I do not know how to define my model correctly.
When I just use nn.linear
erverything works fine but when I use nn.LSTMCell
I get the following error:
AttributeError: 'tuple' object has no attribute 'dim'
The error gets raised because, the LSTM returns a tupel not a tensor. But I do not know how to fix it and can not find an example of an Pytorch LSTM with Optuna optimization online.
Here the Model definition:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前已经实施了Optuna优化LSTM的示例,希望它能为您提供帮助:
I have implemented an example of optuna optimizing LSTM before, I hope it will help you:
我自己实现了解决方案。我不确定它是最柔软的,但它起作用。
欢迎改进建议。
I implemented a solution by my self. I am not sure if it's the most pythonic but it works.
Suggestions for improvement are welcome.