- 用pytorch搭建神经网络,拟合曲线:
y
=
x
2
y=x^2
y=x2
- 环境 pytoch1.1.0
1. 生成训练数据
x_data = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=-1)
y_data = x_data.pow(2)
2. 搭建神经网络
class Neuro_net(torch.nn.Module):
"""搭建神经网络"""
def __init__(self, n_feature, n_hidden_layer, n_output):
super(Neuro_net, self).__init__()
self.hidden_layer = torch.nn.Linear(n_feature, n_hidden_layer)
self.output_layer = torch.nn.Linear(n_hidden_layer, n_output)
def forward(self, x_data):
hidden_layer_x = torch.relu(self.hidden_layer(x_data))
pridect_y = self.output_layer(hidden_layer_x)
return pridect_y
3. 训练神经网络
- 我这边选择训练500步,拟合效果还行,步数在往上增加你会发现loss还会减小,拟合效果还会更好,但也有可能遇到振荡。
num_feature = 1
num_hidden_layer = 10
num_output = 1
epoch = 500
net = Neuro_net(num_feature, num_hidden_layer, num_output)
optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_funaction = torch.nn.MSELoss()
for step in range(epoch):
pridect_y = net(x_data)
loss = loss_funaction(pridect_y, y_data)
optimizer.zero_grad()
loss.backward()
optimizer.step()
4. 为了更好理解训练过程,加入可视化
import matplotlib.pyplot as plt
plt.ion()
for step in range(epoch):
...
loss.backward()
optimizer.step()
if step % 5 == 0:
print("已训练{}步 | loss:{}。".format(step, loss))
plt.cla()
plt.scatter(x_data.numpy(), y_data.numpy())
plt.plot(x_data.numpy(), pridect_y.data.numpy(), color="green", marker="o", linewidth=6, label="predict_line")
plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 15, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
整体代码:
import torch
import matplotlib.pyplot as plt
class Neuro_net(torch.nn.Module):
"""搭建神经网络"""
def __init__(self, n_feature, n_hidden_layer, n_output):
super(Neuro_net, self).__init__()
self.hidden_layer = torch.nn.Linear(n_feature, n_hidden_layer)
self.output_layer = torch.nn.Linear(n_hidden_layer, n_output)
def forward(self, x_data):
hidden_layer_x = torch.relu(self.hidden_layer(x_data))
pridect_y = self.output_layer(hidden_layer_x)
return pridect_y
x_data = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=-1)
y_data = x_data.pow(2)
num_feature = 1
num_hidden_layer = 10
num_output = 1
epoch = 500
net = Neuro_net(num_feature, num_hidden_layer, num_output)
optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_funaction = torch.nn.MSELoss()
plt.ion()
for step in range(epoch):
pridect_y = net(x_data)
loss = loss_funaction(pridect_y, y_data)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 5 == 0:
print("已训练{}步 | loss:{}。".format(step, loss))
plt.cla()
plt.scatter(x_data.numpy(), y_data.numpy())
plt.plot(x_data.numpy(), pridect_y.data.numpy(), color="green", marker="o", linewidth=6, label="predict_line")
plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 13, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
可视化动态拟合图
- 训练开始
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/259f20b5f094e30a7ba18eb93c631bfa.png)
- 训练结束
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/22d4e246eca7656e62745b5bf2b54694.png)