PyTorch框架起步
阅读原文时间:2023年07月09日阅读:2

PyTorch框架基本处理操作

基本使用方法:

import torch
torch.__version__    # 查看版本相关
x = torch.rand(5, 3)
print(x)
# 初始化一个全零的矩阵
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
# 直接传入数据
x = torch.tensor([5.5, 3])
# 展示矩阵大小

基本计算方法

y = torch.rand(5, 3)
print(x + y)    # x在前面
print(torch.add(x, y))    #一样的也是加法
# 索引
print(x[:, 1])


# view操作可以改变矩阵维度
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)    # -1的意思是,有8列,行补齐
print(x.size(), y.size(), z.size())
# 与Numpy的协同操作
a = torch.ones(5, dtype=torch.long)
print(a)
b = a.numpy()
print(b)

a = np.ones(5)
b = torch.from_numpy(a)
print(b)
  • 框架帮我们把返向传播全部计算好了

    import torch

    需要求导的,可以手动定义:

    #方法1
    x = torch.randn(3,4,requires_grad=True)
    print(x)
    #方法2
    x = torch.randn(3,4)
    x.requires_grad=True
    print(x)
    b = torch.randn(3,4,requires_grad=True)
    t = x + b
    y = t.sum()
    print(y)
    y.backward()
    print(b.grad)

    虽然没有指定t的requires_grad但是需要用到它,也会默认的

    print(x.requires_grad, b.requires_grad, t.requires_grad)

举例说明:

#计算流程
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)  #注意y也是需要的
print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf, z.is_leaf)    # 判断是不是叶子节点

# 反向传播计算
z.backward(retain_graph=True)    #如果不清空会累加起来
z.backward(retain_graph=True)    #如果不清空会累加起来
z.backward(retain_graph=True)    #如果不清空会累加起来
z.backward(retain_graph=True)    #如果不清空会累加起来
print(w.grad)
print(b.grad)


# 做一个线性回归
# 构造一组输入数据X和其对应的标签y
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)
x_train = x_train.reshape(-1, 1)
print(x_train)
print(x_train.shape)

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)
print(y_train)
print(y_train.shape)

# 线性回归模型
# 其实线性回归就是一个不加激活函数的全连接层

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)    # 前向传播怎么走,改成自己的  全连接层  {前向传播自己写,反向传播pytouch会自动计算}
        return out

input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
print(model)

# 指定好参数和损失函数
epochs = 1000    # 训练次数
learning_rate = 0.01    # 学习率
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)    # 优化器 这里选择SGD,优化参数和学习率
criterion = nn.MSELoss()    # 损失函数    预测值与真实值之间的均方误差

# 训练模型
for epoch in range(epochs):
    epoch += 1    # 每次epoch自加1,直到1000次结束
    # 注意转行成tensor    将np.array转换成tensor模式
    inputs = torch.from_numpy(x_train)
    labels = torch.from_numpy(y_train)

    # 梯度要清零每一次迭代    因为不清零梯度会累加的
    optimizer.zero_grad()

    # 前向传播
    outputs = model(inputs)

    # 计算损失
    loss = criterion(outputs, labels)

    # 返向传播
    loss.backward()

    # 更新权重参数    基于学习率和梯度值完成更新
    optimizer.step()
    if epoch % 50 == 0:    # 每隔50次打印一下loss值
        print('epoch {}, loss {}'.format(epoch, loss.item()))

# 测试模型预测结果:得出得结果转化成numpy, np.array的格式
predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()
print(predicted)

# 模型的保存与读取
# 保存
torch.save(model.state_dict(), 'model.pkl')    # 保存为字典格式,这里仅仅保存模型(即线性模型的权重和参数)
# 读取
print(model.load_state_dict(torch.load('model.pkl')))

若有GPU则用GPU加速

# 使用GPU进行训练
import torch
import torch.nn as nn
import numpy as np

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

input_dim = 1
output_dim = 1

model = LinearRegressionModel(input_dim, output_dim)

# 如果GPU配置好了,将模型放入GPU中
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)

criterion = nn.MSELoss()

learning_rate = 0.01

optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

epochs = 1000
for epoch in range(epochs):
    epoch += 1
    # 将输入和输出放进GPU中进行计算
    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.backward()

    optimizer.step()

    if epoch % 50 == 0:
        print('epoch {}, loss {}'.format(epoch, loss.item()))
  1. scalar

  2. vector

  3. matrix

  4. n-dimensional tensor

    import torch
    from torch import tensor

    scalar:通常就是一个数值

    x = tensor(42.)

    print(x)

    print(x.dim())

    print(x * 2)

    print(x.item())

    vector:向量

    v = tensor([1.5, -0.5, 3.0])

    print(v)

    print(v.dim()) # 维度

    print(v.size())

    matrix:矩阵

    M = tensor([[1., 2.], [3., 4.]])
    print(M)
    print(M.matmul(M))
    print(tensor([1., 0.]).matmul(M))
    print(M*M)
    print(tensor([1., 2.]).matmul(M))