我分配了不同的weight_decay
for参数,而training loss
和testing loss
都是nan。
我打印了prediction_train,loss_train,running_loss_train,prediction_test,loss_test,and running_loss_test
,他们都是南。
我用numpy.any(numpy.isnan(dataset))
检查了数据,它返回了False
。
如果我使用optimizer = torch.optim.Adam(wnn.parameters())
而不是为参数指定不同的weight_decay
,那就没有问题。
你能告诉我怎么解决吗?这是代码,我自己定义了激活功能。谢谢:)
class Morlet(nn.Module):
def __init__(self):
super(Morlet,self).__init__()
def forward(self,x):
x=(torch.cos(1.75*x))*(torch.exp(-0.5*x*x))
return x
morlet=Morlet()
class WNN(nn.Module):
def __init__(self):
super(WNN,self).__init__()
self.a1=torch.nn.Parameter(torch.randn(64,requires_grad=True))
self.b1=torch.nn.Parameter(torch.randn(64,requires_grad=True))
self.layer1=nn.Linear(30,64,bias=False)
self.out=nn.Linear(64,1)
def forward(self,x):
x=self.layer1(x)
x=(x-self.b1)/self.a1
x=morlet(x)
out=self.out(x)
return out
wnn=WNN()
optimizer = torch.optim.Adam([{'params': wnn.layer1.weight, 'weight_decay':0.01},
{'params': wnn.out.weight, 'weight_decay':0.01},
{'params': wnn.out.bias, 'weight_decay':0},
{'params': wnn.a1, 'weight_decay':0.01},
{'params': wnn.b1, 'weight_decay':0.01}])
criterion = nn.MSELoss()
for epoch in range(10):
prediction_test_list=[]
running_loss_train=0
running_loss_test=0
for i,(x1,y1) in enumerate(trainloader):
prediction_train=wnn(x1)
#print(prediction_train)
loss_train=criterion(prediction_train,y1)
#print(loss_train)
optimizer.zero_grad()
loss_train.backward()
optimizer.step()
running_loss_train+=loss_train.item()
#print(running_loss_train)
tr_loss=running_loss_train/train_set_y_array.shape[0]
for i,(x2,y2) in enumerate(testloader):
prediction_test=wnn(x2)
#print(prediction_test)
loss_test=criterion(prediction_test,y2)
#print(loss_test)
running_loss_test+=loss_test.item()
print(running_loss_test)
prediction_test_list.append(prediction_test.detach().cpu())
ts_loss=running_loss_test/test_set_y_array.shape[0]
print('Epoch {} Train Loss:{}, Test Loss:{}'.format(epoch+1,tr_loss,ts_loss))
test_set_y_array_plot=test_set_y_array*(dataset.max()-dataset.min())+dataset.min()
prediction_test_np=torch.cat(prediction_test_list).numpy()
prediction_test_plot=prediction_test_np*(dataset.max()-dataset.min())+dataset.min()
plt.plot(test_set_y_array_plot.flatten(),'r-',linewidth=0.5,label='True data')
plt.plot(prediction_test_plot,'b-',linewidth=0.5,label='Predicted data')
plt.legend()
plt.show()
print('Finish training')
输出是:
Epoch 1 Train Loss:nan, Test Loss:nan
重量衰减将L2正则化应用于学习的参数,快速浏览一下你的代码,你使用a1
权重作为这里的命名符x=(x-self.b1)/self.a1
,重量衰减为.01,这可能导致消除一些a1
权重为零,除零的结果是什么?