Pytorch

基本操作

张量是基本的数据类型,张量就是一组 n 维数组。

import torch

data = torch.tensor([
    [1, 2, 3, 4, 5],
    [2, 3, 4, 5, 6],
])

# 形状
print(data.shape)

# 数据类型
print(data.dtype)

# === 基本操作 ===
# 类型转换
data = data.to(torch.int)

# 下标取值
print(data[0][4].item())

# 四则运算
source = torch.ones([3, 3]) + 1 # [[2, 2, 2], [2, 2, 2], [2, 2, 2]]
print(source + torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
print(source - torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
print(source * torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
print(source / torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

# 所有单元格的和
print(torch.sum(source).item())

# 计算某个维度的和
source = torch.tensor([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])
print(torch.sum(source, dim=1))

# 计算平均值
print(torch.mean(source.to(torch.float), dim=1))
import torch

predict = torch.tensor([1, 0, 1, 0, 2, 0, 2, 1, 0])
label = torch.tensor([0, 1, 1, 0, 0, 2, 2, 0, 1])

eq = predict == label

print(eq)
print(torch.mean(eq.to(torch.float))) # 计算准确率
import torch

data = torch.tensor([1, 2, 3, 0, 7, -1, -4])

# 索引排序
print(torch.argsort(data))
# 最小值的索引
print(torch.argmin(data))
# 最大值的索引
print(torch.argmax(data))
import torch

data = torch.tensor([
    [1, 2, 3, 0, 7, -1, -4],
    [3, 1, 8, 2, 4, 7, 9],
])

# 变形
data = data.view(-1)

print(data)

搭建神经网络

import torch
import torch.nn as nn
from pathlib import Path

train_features = torch.rand(1, 300).cuda() # .cuda() 是启用 gpu 加速
train_labels = torch.rand(1, 300).cuda()
test_features = torch.rand(1, 300).cuda()
test_labels = torch.rand(1, 300).cuda()

model = nn.Sequential(
    nn.Linear(300, 250),
    nn.ReLU(),

    nn.Linear(250, 320),
    nn.ReLU(),

    nn.Linear(320, 320),
    nn.ReLU(),

    nn.Linear(320, 10),
    nn.Softmax(),
)

model.cuda() # 启动 gpu 加速

# 损失函数
lossfunction = nn.CrossEntropyLoss()

# 优化器
optimizer = torch.optim.Adam(
    params=model.parameters(),
    lr=0.0001,
)

# 训练的轮数
for i in range(100):
    # 清空优化器梯度(偏导)
    optimizer.zero_grad()
    predict = model(train_features) # 前向传播(预测)
    result = torch.argmax(predict, dim=1)
    train_acc = torch.mean(result == train_labels).to(torch.float) # 准确率
    loss = lossfunction(predict, train_labels) # 计算损失值
    loss.backward() # 反向传播
    optimizer.step() # 梯度下降

    print(loss.item(), train_acc.item()) # 打印每一轮训练完成的损失值,准确率

    # 测试
    optimizer.zero_grad()
    predict = model(test_features) # 预测
    result = torch.argmax(predict, dim=1)
    test_acc = torch.mean(result == test_labels).to(torch.float) # 准确率
    loss = lossfunction(predict, test_labels) # 计算损失值
    print(loss.item(), test_acc.item()) # 打印测试集合的损失值,准确率

model_save_path = 'mymodel.pt'

# 保存模型
torch.save(model, model_save_path)


# 加载模型
params = torch.load(model_save_path) # 加载模型文件中的参数 w b
model.load_state_dict(params) # 把参数塞进模型里
predict = model(test_features) # 预测
result = torch.argmax(predict, dim=1) # 预测结果