人工智能人工智能PyTorch
Nuyoah PyTorch
使用神经网络最重要的一件事情就是:他可以帮我们把反向传播全部计算好
Tenser 常见的格式:
-
scalar
通常就是一个数值
-
vector
例如:[-5. , 2. , 0. ],在深度学习中通常指特征,例如词向量特征,某一维度特征等
1
| v = tensor([1.5, -0.5, 3.0])
|
-
matrix
一般计算的都是矩阵,通常是多维的
1
| M = tensor([1., 2. ],[3., 4.])
|
-
n-dimensional tensor
基本使用方法
创建一个矩阵
1 2 3 4 5
| tensor([[-2.1607e-32, 3.9797e-43, -2.1607e-32], [ 3.9797e-43, -2.1607e-32, 3.9797e-43], [-2.1607e-32, 3.9797e-43, -2.1606e-32], [ 3.9797e-43, -2.1606e-32, 3.9797e-43], [-2.1607e-32, 3.9797e-43, -2.1606e-32]])
|
初始化随机矩阵
1 2 3 4 5
| tensor([[0.0888, 0.0026, 0.9993], [0.2157, 0.5465, 0.2976], [0.7008, 0.4855, 0.2934], [0.2556, 0.7336, 0.4070], [0.7086, 0.7576, 0.1706]])
|
初始化全为零的矩阵:
1 2 3 4 5
| tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
|
得到一个tensor格式的数据
1 2
| x = torch.tensor([5.5, 3]) x
|
1
| tensor([5.5000, 3.0000])
|
得到矩阵的大小
基本计算
1 2 3
| y = torch.rand(5,3) x + y torch.add(x, y)
|
1 2 3 4 5
| tensor([[0.7346, 0.6349, 0.1346], [0.7563, 0.8254, 0.2026], [0.7040, 0.7037, 0.8722], [0.0234, 0.4327, 0.4905], [0.5578, 0.4028, 0.7179]])
|
索引操作
1
| tensor([0.4955, 0.2037, 0.5270, 0.9147, 0.5009])
|
view操作可以改变矩阵维度
1 2 3 4 5
| x = torch.randn(4,4) y = x.view(16) z = x.view(-1, 8) print(x.size(), y.size(), z.size())
|
与numpy的协同操作
1 2 3 4
| a = torch.ones(5) b = a.numpy() b
|
1 2 3 4 5
| import numpy as np a = np.ones(5) b = torch.from_numpy(a) b
|
自动求导
我们在设置矩阵的时候可能设置一个参数能够让这个矩阵具有自动求导的功能
1 2 3 4 5 6
| x = torch.randn(3,4,requires_grad = True)
x = torch.randn(3,4) x.require_grad = True
|
当我们在使用一些参数的时候,虽然没有给它指定require_grad = True 但是如果需要使用它求导的话,会默认设为True的
1 2 3 4 5 6 7 8 9 10 11
| t = x + b y = t.sum() print(y)
y.backward()
print(x.requires_grad, y.requires_grad, z.requires_grad)
|
例子:
我们在进行上面式子的反向传播的时候:
1 2 3 4 5 6 7 8 9
| x = torch.rand(1) b = torch.rand(1, requires_grad=True) w = torch.rand(1, requires_grad=True) y = w*x z = y+b print(x.requires_grad, b.requires_grad, w.requires_grad, y.requires_grad)
|
我们还可以判断该节点是不是叶子结点
1 2
| print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf, z.is_leaf)
|
反向传播进行计算
1 2 3
| z.backward(retain_graph=True) w.grad b.grad
|
线性回归测试
我们先构建输入数据x和其对应的标签y
1 2 3 4 5 6 7 8 9 10 11
| x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)
x_train = x_train.reshape(-1, 1)
y_values = [2*i+1 for i in x_values] y_train = np.array(y_values, dtype=np.float32) y_train = y_train.reshape(-1, 1)
|
定义线性回归类
1 2 3 4 5 6 7 8 9 10
| import torch import torch.nn as nn class LinearRegressionModel(nn.Module): def __init__(self, input_dim, output_dim): super(LinearRegressionModel, self).__init__() self.linear = nn.Linear(input_dim, output_dim) def forward(self, x): out = self.linear(x) return out
|
调用线性回归类
1 2 3 4 5
| input_dim = 1 output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
|
指定好参数和损失函数
1 2 3 4 5 6 7 8
| epochs = 1000
learning_rate = 0.01
optimizer = torch.optim.SDG(model.parameters(), lr = learning_rate)
criterion = nn.MSELoss()
|
训练模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| for epoch in range(epochs): epoch += 1 inputs = torch.from_numpy(x_train) labels = torch.from_numpy(y_train) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backword() optimizer.step() if epoch % 50 == 0: print("epoch{}, loss{}".format(epoch, loss.item()))
|
测试模型预测结果:
1
| predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()
|
模型的保存与读取
1 2 3 4
| torch.save(model.state_dict(), "model.pkl")
model.load_state_dict(torch.load("model.pkl"))
|
使用GPU来进行训练
1 2 3 4 5 6 7 8 9
| import torch import torch.nn as nn import numpy as np
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(divice)
|
模型训练
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| for epoch in range(epochs): epoch += 1 inputs = torch.from_numpy(x_train).to(device) labels = torch.from_numpy(y_train).to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backword() optimizer.step() if epoch % 50 == 0: print("epoch{}, loss{}".format(epoch, loss.item()))
|