Pytorch 中 LSTM 的贝叶斯优化

时间:2021-07-11 16:10:43

标签: python pytorch lstm

我正在尝试使用贝叶斯优化来优化 LSTM 的超参数。但是我在运行代码时收到了错误消息 ValueError: setting an array element with a sequence。我需要做什么来解决错误?

trainX shape=379,5,1

trainY shape=379,1

5 是时间步长。第一个隐藏大小为 1。输入大小为 1。

我的贝叶斯优化代码:

def bayopt(neurons,learning_rate,batch_size):
  batch_size = round(batch_size)
  neurons = round(neurons)
  learning_rate= round(learning_rate)
  model=MyWeatherPredictor(input_size,neurons)
  criterion=torch.nn.MSELoss()
  optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)
  loss_history = []
  for epoch in range(epochs): 
      optimizer.zero_grad() 
      input = model(trainX)           
      loss = criterion(input,trainY)  
      loss.backward()    
      optimizer.step()
      loss_history.append(loss.item()) 
  return loss_history
params={'neurons':(2,5),
        'learning_rate':(0.001,0.1),
        'batch_size':(200,400)}
opt=BayesianOptimization(bayopt,params,random_state=777)
opt.maximize(init_points=5,n_iter=8)

我的超参数是隐藏神经元的数量、学习率和批量大小。我首先实现了 Lstm 单元(MyLSTM)(ct、ht 等)。然后我在整个层中应用了 lstm 单元。

模型=MyWeatherPredictor 代码:

class MyWeatherPredictor(torch.nn.Module):

def __init__(self, input_dim, hidden_dim):
    super().__init__()
    
    random.seed(501)
    np.random.seed(501)
    torch.manual_seed(501)
    self.hidden_dim=hidden_dim
    self.input_dim=input_dim
    self.lstm=MyLSTM(input_dim,hidden_dim)
    self.fc=torch.nn.Linear(hidden_dim,1)  
    
def forward(self, X):   
  out,hn=self.lstm(X)    
  prediction=self.fc(hn[:,-1,:])
  return prediction

我的错误是代码:

 TypeError                                 Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'tuple'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-23-8640f2ea9b51> in <module>()
      2         'learning_rate':(0.001,0.1),
      3         'batch_size':(200,400)}
----> 4 opt=BayesianOptimization(bayopt,params,random_state=777)
      5 opt.maximize(init_points=5,n_iter=8)

1 frames
/usr/local/lib/python3.7/dist-packages/bayes_opt/target_space.py in __init__(self, target_func, pbounds, random_state)
     47         self._bounds = np.array(
     48             [item[1] for item in sorted(pbounds.items(), key=lambda x: x[0])],
---> 49             dtype=np.float
     50         )
     51 

ValueError: setting an array element with a sequence.

0 个答案:

没有答案