如何应用MLP来拟合数据?

问题描述 投票:0回答:0

我正在尝试应用 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]

它总是产生如下情节。

enter image description here

我尝试了不同数量的隐藏单元和层,以及不同的学习率。但结果看起来并不好。问题可能是欠拟合。但是,我不知道解决方案。

deep-learning pytorch neural-network
© www.soinside.com 2019 - 2024. All rights reserved.