import torch as t
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
'''
定义网络
'''
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84,10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(x.size()[0], -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
print('net: \n' + str(net))
print('-'*30)
params = list(net.parameters())
print('len(params): ' + str(len(params)))
print('-'*30)
print('name:parameters.size(): ')
for name, parameters in net.named_parameters():
print(name, ':', parameters.size())
print('-'*30)
input = t.randn(1, 1, 32, 32)
out = net(input)
print('out.size(): ' + str(out.size()))
print('-'*30)
net.zero_grad()
out.backward(t.ones(1,10))
'''
损失函数
'''
output = net(input)
target = t.arange(0, 10).view(1, 10).float()
criterion = nn.MSELoss()
loss = criterion(output, target)
print('loss: ' + str(loss))
print('-'*30)
net.zero_grad()
print('反向传播之前 conv1.bias 的梯度: \n' + str(net.conv1.bias.grad))
loss.backward()
print('反向传播之后 conv1.bias 的梯度: \n' + str(net.conv1.bias.grad))
'''
优化器
'''
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
optimizer = optim.SGD(net.parameters(), lr = 0.01)
optimizer.zero_grad()
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()
|