pytorch的基本使用
PyTorch 使用 torch.Tensor 来进行数值计算和模型训练。它是多维数组(类似 NumPy)并且可以在 GPU 上进行计算。
import torch # 创建一个 2x3 的全零张量 tensor_zeros = torch.zeros(2, 3) print(tensor_zeros) # 创建一个 2x3 的全一张量 tensor_ones = torch.ones(2, 3) print(tensor_ones) # 从数据创建张量 tensor_from_data = torch.tensor([[1, 2], [3, 4]]) print(tensor_from_data)
# 张量加法 tensor_a = torch.tensor([1, 2, 3]) tensor_b = torch.tensor([4, 5, 6]) sum_tensor = tensor_a + tensor_b print(sum_tensor) # 张量乘法 product_tensor = tensor_a * tensor_b print(product_tensor) # 矩阵乘法 matrix_a = torch.tensor([[1, 2], [3, 4]]) matrix_b = torch.tensor([[5, 6], [7, 8]]) matrix_product = torch.mm(matrix_a, matrix_b) print(matrix_product)
# 在 CPU 上创建张量 tensor_cpu = torch.tensor([1.0, 2.0, 3.0]) # 将张量移到 GPU 上(需要安装 CUDA 并有支持的 GPU) if torch.cuda.is_available(): tensor_gpu = tensor_cpu.to('cuda') print(tensor_gpu)
PyTorch 提供了一个非常灵活的方式来定义和训练神经网络,通常通过继承 torch.nn.Module 来定义模型。
import torch import torch.nn as nn import torch.optim as optim # 定义一个简单的全连接网络 class SimpleNet(nn.Module): def __init__(self): super(SimpleNet, self).__init__() self.fc1 = nn.Linear(10, 20) # 输入 10 维,输出 20 维 self.fc2 = nn.Linear(20, 2) # 输入 20 维,输出 2 维 def forward(self, x): x = torch.relu(self.fc1(x)) # ReLU 激活函数 x = self.fc2(x) return x # 实例化网络 model = SimpleNet() print(model)
# 使用均方误差损失函数(MSE) criterion = nn.MSELoss() # 使用随机梯度下降优化器(SGD) optimizer = optim.SGD(model.parameters(), lr=0.01)
# 假设输入数据为一个批次 (batch),大小为 (batch_size, input_size) inputs = torch.randn(5, 10) # 5 个样本,每个样本 10 个特征 targets = torch.randn(5, 2) # 5 个标签,每个标签 2 个特征 # 训练循环 for epoch in range(100): # 训练 100 次 # 前向传播 outputs = model(inputs) # 计算损失 loss = criterion(outputs, targets) # 后向传播 optimizer.zero_grad() # 清空上一步的梯度 loss.backward() # 计算梯度 optimizer.step() # 更新权重 # 输出每 10 次的损失值 if epoch % 10 == 0: print(f'Epoch {epoch}, Loss: {loss.item()}')
要在 GPU 上运行模型和张量运算,只需要确保所有的张量都被移动到同一个设备(CPU 或 GPU)。例如:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 将模型移到 GPU model.to(device) # 输入数据也需要移到相同设备 inputs = inputs.to(device) targets = targets.to(device)
训练循环(同上)
# 保存模型的状态字典(权重) torch.save(model.state_dict(), 'model.pth')
# 重新实例化模型 model = SimpleNet() # 加载保存的权重 model.load_state_dict(torch.load('model.pth'))
激活函数:torch.relu(), torch.sigmoid(), torch.tanh()
张量运算:torch.add(), torch.matmul(), torch.mean(), torch.sum()
常用方法:tensor.size(), tensor.view(), tensor.reshape()
注
如果有任何错误或需要改进,欢迎留言指正。
参考资料
来自ai