MLP拟合数据效果不佳,调整参数后仍无改善求解决方案
用MLP拟合数据效果不佳的问题
我尝试使用MLP拟合我的数据,但效果未达预期。当前设置的MLP是一个4层网络,每个隐藏层的隐藏单元数为100。
import torch from torch import nn from torch.utils.data import DataLoader from torch.utils.data import TensorDataset import numpy as np import pandas as pd sg = pd.read_csv("/Users/xxxxxx/Desktop/sgf.csv",header=None) sg = np.array(sg) t = np.linspace(0,1500,51) sg1 = sg[0,:] X = np.expand_dims(t,axis=1) Y = sg1.reshape(51,-1) dataset = TensorDataset(torch.tensor(X,dtype=torch.float),torch.tensor(Y,dtype=torch.float)) dataloader = DataLoader(dataset,batch_size=51,shuffle=True) class Net(nn.Module): def __init__(self): super(Net,self).__init__() self.net = nn.Sequential(nn.Linear(in_features=1,out_features=100),nn.Sigmoid(), nn.Linear(100,100),nn.Sigmoid(), nn.Linear(100,100),nn.Sigmoid(), nn.Linear(100,100),nn.Sigmoid(), nn.Linear(100,1)) def forward(self,input): return self.net(input) net = Net() optim = torch.optim.Adam(net.parameters(),lr=0.0001) loss = nn.MSELoss() for epoch in range(1000): ls = None for bx,by in dataloader: optim.zero_grad() y_hat = net(bx) ls = loss(y_hat,by) ls.backward() optim.step() if (epoch+1) % 500 == 0: print("step: {0}, loss: {1}".format(epoch+1,ls.item())) pred = net(torch.tensor(X,dtype = torch.float)) %matplotlib inline import matplotlib.pyplot as plt plt.plot(t,sg1,label="fact") plt.plot(t,pred.detach().numpy(),label='predict') plt.title("sin function") plt.xlabel("x") plt.ylabel("sin(x)") plt.legend() plt.show()
数据示例
sg1 = [ 0.08196445, 0.03914855, 0.00515015, -0.02003076, -0.03639418, -0.03993232, -0.05617677, -0.04173793, -0.02260713, -0.00150733, 0.00902901, 0.023227 , 0.0238875 , 0.02220099, 0.01067397, -0.00998361, -0.03089296, -0.02376404, -0.0205146 , -0.01143468, -0.01102645, 0.00288759, 0.02869272, 0.03123309, 0.03807579, 0.02819487, 0.0163046 , 0.00416688, -0.00608408, -0.00566473, 0.00194873, 0.01498575, 0.00616927, -0.01562626, -0.01950474, -0.00951809, -0.00754946, -0.01478931, -0.01092594, 0.00464668, 0.00144806, 0.00459074, 0.0138626 , 0.0161684 , -0.00040095, -0.00075886, -0.00839019, -0.00918888, -0.0072783 , -0.00265845, 0.00467066]
拟合结果图像

我尝试过调整隐藏单元数、网络层数及学习率,但结果仍不理想,怀疑存在欠拟合问题,却不知道该如何解决。
内容的提问来源于stack exchange,提问作者BonHaha
相关产品推荐
相关产品推荐

