import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
import torch.nn as nn
num_inputs = 2
num_examples = 1000
true_w = [2,-3.4]
true_b = 4.2
features = torch.tensor(np.random.normal(0,1,(num_examples,num_inputs)),dtype=torch.float)
labels = true_w[0] * features[:,0] + true_w[1] * features[:,1] + true_b
labels += torch.tensor(np.random.normal(0,0.01,size = labels.size()),dtype=torch.float)
import torch.utils.data as Data
batch_size = 10
dataset = Data.TensorDataset(features,labels)
data_iter = Data.DataLoader(dataset,batch_size,shuffle=True)
for X,Y in data_iter:
print(X,Y)
break
"""
tensor([[-3.0391e+00, 2.4009e-01],
[ 5.3108e-01, 1.1738e+00],
[-6.4292e-01, -2.7932e-01],
[-2.2719e-03, 6.5884e-01],
[ 4.5355e-01, 1.3269e+00],
[ 7.3476e-01, -3.0537e-02],
[ 3.1508e-01, -1.1509e+00],
[ 8.6587e-02, 1.3892e+00],
[-1.2793e+00, -8.5319e-02],
[-2.8471e-01, -1.0173e+00]]) tensor([-2.6953, 1.2816, 3.8702, 1.9493, 0.5765, 5.7743, 8.7348, -0.3593,
1.9270, 7.0863])
"""
"""
pytorch定义了很多神经网络,我们不需要自己去设计,直接调用即可。导入torch.nn模块,该模块定义了大量神经网络的层。
nn的核心数据结构就是Module,它是一个抽象概念,既可以表示神经网络中的某个层,也可以表示一个包含很多层的神经网络。在实际应用中
通常是继承nn.Module,撰写自己的网络/层。
"""
net = nn.Sequential(nn.Linear(num_inputs,1)
)
net = nn.Sequential()
net.add_module('linear',nn.Linear(num_inputs,1))
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
('linear',nn.Linear(num_inputs,1))
]))
print(net)
"""
Sequential(
(linear): Linear(in_features=2, out_features=1, bias=True)
)
"""
print(net[0])
"""
Linear(in_features=2, out_features=1, bias=True)
"""
for param in net.parameters():
print(param)
"""
Parameter containing:
tensor([[-0.1717, -0.1062]], requires_grad=True)
Parameter containing:
tensor([0.0345], requires_grad=True)
"""
"""
pytorch在init模块中提供了多种参数初始化方法,我们通过init_normal_将权重参数每个元素初始化为随机采样于均值为0,
标准差为0.01的正太分布,偏差会初始化为0
"""
from torch.nn import init
init.normal_(net[0].weight,mean = 0,std = 0.01)
init.constant_(net[0].bias,val = 0)
loss = nn.MSELoss()
"""
我们无需自己实现小批量随机梯度下降算法。torch.optim模块提供了很多常用的优化算法,例如:SGD,Adam和RMSPro等
下面我们创建一个用于优化net所有参数的优化器实例,并指定学习率为0.03的小批量随机梯度下降(SGD)为优化算法
"""
import torch.optim as optim
optimizer = optim.SGD(net.parameters(),lr = 0.03)
print(optimizer)
"""
SGD (
Parameter Group 0
dampening: 0
lr: 0.03
momentum: 0
nesterov: False
weight_decay: 0
)
"""
"""
1、修改optimizer.param_groups中对应的学习率
2、另外一种更加简单和高效的做法是:新建优化器
"""
for param_group in optimizer.param_groups:
param_group['lr'] *= 0.1
num_epochs = 3
for epoch in range(1,num_epochs + 1):
for X,Y in data_iter:
out_put = net(X)
l = loss(out_put,Y.view(-1,1))
optimizer.zero_grad()
optimizer.step()
print('epoch %d,loss:%f' % (epoch,l.item()))
"""
epoch 1,loss:28.709951
epoch 2,loss:26.306438
epoch 3,loss:39.618736
"""
dense = net[0]
print(true_w,dense.weight)
print(true_b,dense.bias)
"""
[2, -3.4] Parameter containing:
tensor([[-0.0102, -0.0031]], requires_grad=True)
4.2 Parameter containing:
tensor([0.], requires_grad=True)
"""