下面是“PyTorch安装与基本使用详解”的完整攻略,包括安装步骤、基本使用以及两个示例。
PyTorch安装与基本使用详解
安装
安装前的准备工作
在安装PyTorch之前,我们需要先安装以下环境:
安装PyTorch
安装PyTorch可以通过Anaconda/Miniconda或pip来进行。这里我们介绍以下两种安装方式:
通过Anaconda/Miniconda安装PyTorch
可以通过以下命令来安装PyTorch:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
其中,cudatoolkit=10.2
表示我们使用CUDA 10.2版本,如果你的CUDA版本不一样,可以修改这个参数。
通过pip安装PyTorch
可以通过以下命令来安装PyTorch:
pip install torch torchvision torchaudio
验证安装
安装完成后,我们可以通过如下的方式来验证是否安装成功:
import torch
print(torch.__version__)
如果输出了当前安装的PyTorch版本,则说明安装成功。
基本使用
以下代码演示了如何使用PyTorch构建一个简单的神经网络:
import torch
import torch.nn as nn
import torch.optim as optim
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 100)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(100, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc1(x)
x = self.relu1(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x
X_train = torch.randn(100, 10)
y_train = torch.randn(100, 1)
net = Net()
criterion = nn.BCELoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)
for epoch in range(1000):
optimizer.zero_grad()
outputs = net(X_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
torch.save(net.state_dict(), 'net.pth')
代码解释:
- 定义神经网络
Net
,继承nn.Module
; - 实现网络的前向传播,包括两个全连接层和一个激活函数;
- 定义训练数据
X_train
和标签y_train
; - 初始化网络、代价函数和优化器;
- 进行1000轮训练,每轮训练进行一次前向传播、代价计算、反向传播和优化;
- 保存训练得到的网络权重参数到文件中。
示例
示例1:使用卷积神经网络进行图像分类
下面是一个使用PyTorch实现的卷积神经网络(Convolutional Neural Network,CNN)进行图像分类的示例,具体步骤如下:
- 加载并预处理 CIFAR-10 数据集;
- 定义网络结构,包括多个卷积层、池化层和全连接层;
- 定义损失函数和优化算法;
- 进行训练和测试,并输出训练结果。
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import time
# 加载训练数据集和测试数据集
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=128,
shuffle=False, num_workers=2)
# 定义网络结构
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.relu1 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(64, 128, 3, padding=1)
self.bn2 = nn.BatchNorm2d(128)
self.relu2 = nn.ReLU(inplace=True)
self.maxpool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(128, 256, 3, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.relu3 = nn.ReLU(inplace=True)
self.maxpool3 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(4 * 4 * 256, 1024)
self.dropout1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(1024, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.maxpool3(x)
x = x.view(-1, 4 * 4 * 256)
x = self.fc1(x)
x = self.dropout1(x)
x = self.fc2(x)
return x
net = Net()
# 定义损失函数和优化算法
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)
# 进行网络训练和测试
device = 'cuda' if torch.cuda.is_available() else 'cpu'
net = net.to(device)
start_time = time.time()
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 100 == 99:
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
print('Finished Training. Time Used:', time.time()-start_time)
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on the test images: %d %%' % (100 * correct / total))
示例2:使用循环神经网络进行序列预测
下面是一个使用PyTorch实现的循环神经网络(Recurrent Neural Network,RNN)进行序列预测的示例,具体步骤如下:
- 加载并预处理数据集;
- 定义网络结构,包括一个LSTM层、一个线性层和一个激活函数;
- 定义损失函数和优化算法;
- 进行训练和测试,并输出训练结果。
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
# 准备数据集
data = [i*np.pi/180 for i in range(0, 720, 5)]
sin_data = np.sin(data)
seq_len = 20
X, y = [], []
for i in range(len(sin_data)-seq_len-1):
X.append(sin_data[i:i+seq_len])
y.append(sin_data[i+seq_len])
X = np.array(X, dtype=np.float32).reshape(-1, seq_len, 1)
y = np.array(y, dtype=np.float32).reshape(-1, 1)
# 定义模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.lstm = nn.LSTM(input_size=1, hidden_size=10, batch_first=True)
self.fc = nn.Linear(in_features=10, out_features=1)
self.relu = nn.ReLU()
def forward(self, x):
x, _ = self.lstm(x)
x = self.fc(x[:, -1, :])
x = self.relu(x)
return x
net = Net()
# 定义损失函数和优化算法
criterion = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)
# 进行网络训练和测试
device = 'cuda' if torch.cuda.is_available() else 'cpu'
net = net.to(device)
X, y = torch.tensor(X).to(device), torch.tensor(y).to(device)
start_time = time.time()
for epoch in range(100):
optimizer.zero_grad()
outputs = net(X)
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
if epoch % 10 == 9:
print("Epoch: {}, Loss: {:.5f}".format(epoch+1, loss.item()))
print('Finished Training. Time Used:', time.time()-start_time)
# 进行测试
net.eval()
with torch.no_grad():
X_test, y_test = [], []
for i in range(len(sin_data)-seq_len*2):
X_test.append(sin_data[i:i+seq_len])
y_test.append(sin_data[i+seq_len])
X_test = np.array(X_test, dtype=np.float32).reshape(-1, seq_len, 1)
y_test = np.array(y_test, dtype=np.float32).reshape(-1, 1)
X_test, y_test = torch.tensor(X_test).to(device), torch.tensor(y_test).to(device)
outputs = net(X_test)
loss_test = criterion(outputs, y_test)
print('MSE on the test data: {:.5f}'.format(loss_test.item()))
以上就是PyTorch安装与基本使用的详细攻略,包括了安装步骤、基本使用和两个示例的说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyTorch安装与基本使用详解 - Python技术站