< >
Home » Turbot-DL入门教程 » Turbot-DL入门教程篇-Pytorch应用-搭建一个简易神经网络

Turbot-DL入门教程篇-Pytorch应用-搭建一个简易神经网络

说明:

  • 介绍使用Pytorch搭建一个简易神经网络

环境:

  • Python 3.5.2

步骤:

  • 新建文件
$ vim pytorch_third.py
  • 内容如下
# coding:utf-8
import torch

batch_n = 100
hidden_layer = 100
input_data = 1000
output_data = 10

x = torch.randn(batch_n, input_data)
y = torch.randn(batch_n, output_data)

w1 = torch.randn(input_data, hidden_layer)
w2 = torch.randn(hidden_layer, output_data)

epoch_n = 20
learning_rate = 1e-6

for epoch in range(epoch_n):
    h1 = x.mm(w1)  # 100*1000
    h1 = h1.clamp(min=0)
    y_pred = h1.mm(w2)  # 100*10
    # print(y_pred)

    loss = (y_pred - y).pow(2).sum()
    print("Epoch:{} , Loss:{:.4f}".format(epoch, loss))

    gray_y_pred = 2 * (y_pred - y)
    gray_w2 = h1.t().mm(gray_y_pred)

    grad_h = gray_y_pred.clone()
    grad_h = grad_h.mm(w2.t())
    grad_h.clamp_(min=0)
    grad_w1 = x.t().mm(grad_h)

    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * gray_w2
  • 运行
python3 pytorch_third.py
  • 结果如下
Epoch:0 , Loss:55005852.0000
Epoch:1 , Loss:131827080.0000
Epoch:2 , Loss:455499616.0000
Epoch:3 , Loss:633762304.0000
Epoch:4 , Loss:23963018.0000
Epoch:5 , Loss:10820027.0000
Epoch:6 , Loss:6080145.5000
Epoch:7 , Loss:3903527.5000
Epoch:8 , Loss:2783492.7500
Epoch:9 , Loss:2160689.0000
Epoch:10 , Loss:1788741.0000
Epoch:11 , Loss:1549332.1250
Epoch:12 , Loss:1383139.6250
Epoch:13 , Loss:1259326.3750
Epoch:14 , Loss:1161324.7500
Epoch:15 , Loss:1080014.2500
Epoch:16 , Loss:1010260.2500
Epoch:17 , Loss:949190.7500
Epoch:18 , Loss:894736.6875
Epoch:19 , Loss:845573.3750

Process finished with exit code 0
  • 可以看出,loss值从之前的巨大误差逐渐缩减,这说明我们的模型经过二十次训练和权重参数优化之后,得到的预测的值和真实值之间的差距越来越小了

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: turbot-dl入门教程篇