【发布时间】:2021-05-13 20:15:45
【问题描述】:
目前,我正在尝试为 LSTM 和 GRU 制作训练模型。 LSTM 运行良好,但是一旦我切换到 GRU 训练,就会弹出错误,例如大小不匹配错误。
这是我的代码
path = "new_z_axis"
device = "cuda:0"
in_size = 3
h_size = 50
n_layers = 3
fc = 20
out = 1
batch_size = 16
seq = 100
epoch = 100
learning_rate = 1e-3
ratio = 0.8
checkpoint = os.path.join("checkpoints","model_"+path+"_"+str(in_size)+".pth")
class GRUNet(nn.Module):
def __init__(self,in_size,h_size,n_layers,fc_out,out_size,dropout=0.5):
super(GRUNet, self).__init__()
self.gru = nn.GRU(input_size=in_size,hidden_size=h_size,num_layers=n_layers,dropout=dropout,bias=False)
self.fc = nn.Linear(in_features=h_size,out_features=fc_out,bias=False)
self.relu = nn.ReLU(inplace=True)
self.out = nn.Linear(in_features=fc_out,out_features=out_size,bias=False)
self.tanh = nn.Tanh()
def forward(self, x, hidden):
out, hidden = self.gru(x, hidden)
x = self.fc(x)
x = self.relu(x)
x = self.out(x)
x = self.tanh(x)
return x, hidden
class MyLstm(nn.Module):
def __init__(self,in_size,h_size,n_layers,fc_out,out_size,dropout=0.5):
super(MyLstm, self).__init__()
self.lstm = nn.LSTM(input_size=in_size,hidden_size=h_size,num_layers=n_layers,dropout=dropout,bias=False)
self.fc = nn.Linear(in_features=h_size,out_features=fc_out,bias=False)
self.relu = nn.ReLU(inplace=True)
self.out = nn.Linear(in_features=fc_out,out_features=out_size,bias=False)
self.tanh = nn.Tanh()
def forward(self,x,hidden):
x, hidden = self.lstm(x,hidden)
# x = x[-1:]
x = self.fc(x)
x = self.relu(x)
x = self.out(x)
x = self.tanh(x)
return x, hidden
def train(model,train_list,val_list,path,seq,epoch,batch_size,criterion,optimizer,model_type):
for e in range(epoch):
train_data = load_data(train_list,batch_size)
a_loss = 0
a_size = 0
model.train()
for x,y in train_data:
x,y = x.to(device),y.to(device)
bs = x.size()[1]
# hidden = (hidden[0].detach(),hidden[1].detach())
# print(x.size(),hidden[0].size())
if model_type == "GRU":
h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
hidden = h1
hidden = hidden.data
else:
h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
h2 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
hidden = (h1,h2)
hidden = tuple([e.data for e in hidden])
model.zero_grad()
print (len(hidden))
pred,hidden = model(x,hidden)
loss = criterion(pred,y)
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(),5)
optimizer.step()
a_loss += loss.detach()
a_size += bs
# print(e,a_loss/a_size*1e+6)
model.eval()
with torch.no_grad():
val_data = load_data(val_list,batch_size)
b_loss = 0
b_size = 0
for x,y in val_data:
x,y = x.to(device),y.to(device)
bs = x.size()[1]
if model_type == "GRU":
h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
hidden = h1
hidden = hidden.data
else:
h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
h2 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
hidden = (h1,h2)
hidden = tuple([e.data for e in hidden])
pred,hidden = model(x,hidden)
loss = criterion(pred,y)
b_loss += loss.detach()
b_size += bs
print("epoch: {} - train_loss: {} - val_loss: {}".format(e+1,float(a_loss.item()/a_size*1e+6),b_loss.item()/b_size*1e+6))
train(modelGRU,train_list,val_list,path,seq,epoch,batch_size,criterionGRU,optimizerGRU,model_type="GRU")
这是我遇到的错误
----------------------------------- --- ------------- RuntimeError Traceback(最近一次调用最后一次)在 ---- > 1 列火车(modelGRU,train_list,val_list,path,seq,epoch,batch_size,criteriaGRU,optimizerGRU,model_type = "GRU" )
in train (model, train_list, val_list, 路径、seq、epoch、batch_size、标准、优化器、model_type) 61型号。 zero_grad ( ) 62打印(len(隐藏)) ---> 63 pred , hidden = 模型 ( x , hidden ) 64 损失 = 标准 (pred, y) 65损失.backward()
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py 在调用 (自我,*输入,**kwargs) 539 结果 = 自我。 _slow_forward ( * 输入 , ** kwargs ) 540 其他: --> 541 结果 = 自我。转发(*输入,** kwargs) 542 用于自我挂钩。 _forward_hooks 。值 ( ) : 543 hook_result = hook(自我,输入,结果)
向前 (self, x, hidden) 11 def forward ( self , x , hidden ) : 12出,隐藏=自我。 gru ( x , 隐藏 ) ---> 13 x = 自我。 fc ( x ) 14 x = 自我。 relu ( x ) 15 x =自我。出 ( x )
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py 在调用 (自我,*输入,**kwargs) 539 结果 = 自我。 _slow_forward ( * 输入 , ** kwargs ) 540 其他: --> 541 结果 = 自我。转发(*输入,** kwargs) 542 用于自我挂钩。 _forward_hooks 。值 ( ) : 543 hook_result = hook(自我,输入,结果)
~\Anaconda3\lib\site-packages\torch\nn\modules\linear.py 在前进(自我,输入) 85
86 def forward(自我,输入): ---> 87 返回 F 。线性(输入,自我。权重,自我。负偏差)88 89 def extra_repr(自我):~\Anaconda3\lib\site-packages\torch\nn\functional.py 线性(输入, 重量,偏差)1370 ret = 火炬。 addmm ( 偏差 , 输入 , 重量 。 t ( ) ) 1371 否则: -> 1372 输出 = 输入。 matmul ( weight . t ( ) ) 1373 如果偏差不是 None : 1374 输出 += 偏差
RuntimeError:尺寸不匹配,m1:[1600 x 3],m2:[50 x 20] C:/w/1/s/tmp_conda_3.7_104508/conda/conda-bld/pytorch_1572950778684/work/aten/src\THC/ 通用/THCTensorMathBlas.cu:290
有什么建议吗? 谢谢
【问题讨论】:
标签: python machine-learning pytorch lstm