#!/usr/bin/env python
coding: utf-8
In[1]:
get_ipython().magic(u’matplotlib’)
神经网络的典型训练过程如下:
1:定义包含一些参数(或者权重)的神经网络模型
2:在数据集上迭代,
3:通过神经网络处理输入
4:计算损失,输出值和正确值的插值大小
5:将梯度反向传播会回网络的参数
6:更新网络的参数,主要使用如下的更新原则:weight = weight - learning_rate * gradient
# 定义网络
In[2]:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module): #继承父类,nn.Module类
def init(self): #初始化函数
super(Net,self).init() #继承nn.Model类的初始化函数
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel 网络核心
self.conv1 = nn.Conv2d(1,6,5) # in_channels, out_channels, kernel_size(int or tuple)
self.conv2 = nn.Conv2d(6,16,5)
# an affine operation: y = Wx + b ,映射函数
self.fc1 = nn.Linear(1655,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)
def forward(self,x):
# 最大池(2,2)窗口
x = F.m