TVP

# 非线性回归

import torch

from torch import nn,optim

import matplotlib.pyplot as plt

import pandas as pd

x = torch.from_numpy(data['time'].values.reshape(-1,1)).type(torch.float32)

y = torch.from_numpy(data['deltaT'].values.reshape(-1,1)).type(torch.float32)

class NonLinearNet(nn.Module):

def __init__(self):

super(NonLinearNet,self).__init__()

self.linear = nn.Sequential(

nn.Linear(1,20),

nn.ReLU(),

nn.Linear(20,12),

nn.ReLU(),

nn.Linear(12,1)

)

def forward(self,x):

out = self.linear(x)

return out

model = NonLinearNet()

loss_fn = nn.MSELoss()

for epoch in range(10000):

output = model(x)

loss = loss_fn(output,y)

loss.backward()

optimizer.step()

if epoch % 500 == 499:

print('epoch:%d,loss=%.3f'%(epoch+1,loss.item()))

plt.cla()

plt.scatter(x, y, s=3, color='blue')

y_pred = model(x)

plt.plot(x, y_pred.detach().numpy(), lw=2, color='red')

plt.grid()

plt.title('epoch:%d,loss=%.3f'%(epoch+1,loss.item()))

plt.pause(0.05)

plt.show()

torch.save(model.state_dict(),'NonLinearNet.pkl')

model = NonLinearNet()

test_x = torch.tensor([[700.0]],dtype=torch.float32)

print(model(test_x))

import torch

from torch import nn,optim

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

# 准备数据

x = np.linspace(0,2*np.pi,num=100).reshape(-1,1)

ysin = np.sin(x).reshape(-1,1)

class NonLinearNet(nn.Module):

def __init__(self):

super(NonLinearNet,self).__init__()

self.linear = nn.Sequential(

nn.Linear(1,20),

nn.ReLU(),

nn.Linear(20,12),

nn.ReLU(),

nn.Linear(12,4),

nn.ReLU(),

nn.Linear(4,1)

)

def forward(self,x):

out = self.linear(x)

return out

model = NonLinearNet()

loss_fn = nn.MSELoss()

for epoch in range(10000):

x = torch.Tensor(x)

y = torch.Tensor(ysin)

output = model(x)

loss = loss_fn(output,y)

loss.backward()

optimizer.step()

if epoch % 1000 == 999:

print('epoch:%d,loss=%.3f'%(epoch+1,loss.item()))

plt.cla()

plt.scatter(x, y, s=3, color='blue')

y_pred = model(x)

plt.plot(x, y_pred.detach().numpy(), lw=2, color='red')

plt.grid()

plt.title('epoch:%d,loss=%.5f'%(epoch+1,loss.item()))

plt.pause(0.05)

plt.show()

import torch

from torch import nn,optim

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

# 准备数据

x = np.linspace(0,2*np.pi,num=100).reshape(-1,1)

# ysin = np.sin(x).reshape(-1,1)

ycos = np.cos(x).reshape(-1,1)

class NonLinearNet(nn.Module):

def __init__(self):

super(NonLinearNet,self).__init__()

self.linear = nn.Sequential(

nn.Linear(1,20),

nn.ReLU(),

nn.Linear(20,12),

nn.ReLU(),

nn.Linear(12,4),

nn.ReLU(),

nn.Linear(4,1)

)

def forward(self,x):

out = self.linear(x)

return out

model = NonLinearNet()

loss_fn = nn.MSELoss()

for epoch in range(10000):

x = torch.Tensor(x)

y = torch.Tensor(ycos)

output = model(x)

loss = loss_fn(output,y)

loss.backward()

optimizer.step()

if epoch % 1000 == 999:

print('epoch:%d,loss=%.3f'%(epoch+1,loss.item()))

plt.cla()

plt.scatter(x, y, s=3, color='blue')

y_pred = model(x)

plt.plot(x, y_pred.detach().numpy(), lw=2, color='red')

plt.grid()

plt.title('epoch:%d,loss=%.5f'%(epoch+1,loss.item()))

plt.pause(0.05)

plt.show()

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20210223A07EG100?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2023-09-27

2023-09-27

2023-09-27

2023-09-27

2023-09-27

2023-09-27

2023-09-27

2023-09-27

2018-04-27

2023-09-27

2023-09-27

Get大咖技术交流圈