一、安装与运行
1、conda是一个开源包管理工具,可用于安装、运行各种软件包。安装conda之后,可以通过conda install命令来安装Torch。
conda install pytorch torchvision torchaudio -c pytorch
2、可以通过conda创建虚拟环境并安装Torch,以避免系统中存在多个Torch版本的问题。
conda create -n myenv python=3.8
conda activate myenv
conda install pytorch torchvision torchaudio -c pytorch
二、Torch的基础操作
1、Tensor是Torch中的核心数据结构,可以理解为多维数组。可以通过Tensor创建零向量、随机向量和手动设置向量等操作。
#创建一个2x3的零向量
import torch
x = torch.zeros(2, 3)
print(x)
#创建一个2x3的随机向量
x = torch.randn(2, 3)
print(x)
#创建一个2x3的手动设置向量
x = torch.tensor([[1,2,3],[4,5,6]])
print(x)
2、可以通过Tensor进行数学运算,如加减乘除和矩阵乘法等操作。
x = torch.randn(2, 3)
y = torch.randn(2, 3)
#加
z = x + y
#减
z = x - y
#乘
z = x * y
#除
z = x / y
#矩阵乘法
x = torch.rand(2, 3)
y = torch.rand(3, 2)
z = torch.mm(x, y) # z的大小是(2,2)
print(z)
三、使用Torch进行深度学习
1、Torch的神经网络包nn可以帮助构建神经网络模型。可以通过nn包中的内置模块实现卷积神经网络(CNN)和长短时记忆网络(LSTM)等模型。
import torch.nn as nn
#构建简单的神经网络模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
#构建CNN模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=5, padding=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=5, padding=2)
self.fc1 = nn.Linear(64*7*7, 1024)
self.fc2 = nn.Linear(1024, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
#构建LSTM模型
class LSTM(nn.Module):
def __init__(self):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(256, 512, num_layers=3, batch_first=True)
self.fc = nn.Linear(512, 10)
def forward(self, x):
h0 = torch.zeros(3, x.size(0), 512)
c0 = torch.zeros(3, x.size(0), 512)
out, _ = self.lstm(x, (h0,c0))
out = F.softmax(self.fc(out[:, -1, :]), dim=1)
return out
2、可以使用Torch进行图像分类、目标检测和自然语言处理等各种深度学习任务。
import torch.optim as optim
#在MNIST数据集上训练CNN模型
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
#加载MNIST数据集
train_dataset = dsets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='./data', train=False, transform=transforms.ToTensor(),download=True)
#加载数据集并转换为Tensor格式
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
#初始化模型、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)
#训练模型
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
#在CIFAR-10数据集上训练ResNet模型
from torchvision import models
from torchsummary import summary
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#加载CIFAR-10数据集
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=100, shuffle=False, num_workers=2)
#设置ResNet18模型
net = models.resnet18(pretrained=False)
num_ftrs = net.fc.in_features
net.fc = nn.Linear(num_ftrs, 10)
net.to(device)
#训练模型
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200)
for epoch in range(num_epochs):
net.train()
train_loss = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
四、Torch的应用场景
1、深度学习研究及开发
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
from torch.autograd import Variable
import torchvision.datasets as dsets
#加载数据集
train_dataset = dsets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='./data', train=False, transform=transforms.ToTensor(),download=True)
#初始化模型、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
#训练模型
for epoch in range(2):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
#使用Torch进行分布式训练
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
def train(model, device, train_loader, criterion, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
def setup(rank, world_size):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12345'
# initialize the process group
dist.init_process_group("gloo", rank=rank, world_size=world_size)
def cleanup():
dist.destroy_process_group()
if __name__ == '__main__':
MP = False
rank, world_size = 0, 1
if MP:
mp.set_start_method("spawn")
world_size = mp.cpu_count()
rank = args.nr * args.gpus + i
setup(rank, world_size)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Net().to(device)
if MP:
model = DDP(model, device_ids=[i], output_device=i)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
train_loader = torch.utils.data.DataLoader(, batch_size=64, shuffle=True, num_workers=0, pin_memory=True)
for epoch in range(2):
train(model, device, train_loader, criterion, optimizer, epoch)
if MP:
cleanup()
2、机器学习算法实现
import torch
import pandas as pd
#基于线性回归算法实现房价预测
from torch.utils.data import Dataset, DataLoader
class HousingDataset(Dataset):
def __init__(self):
#使用pandas读取数据集,并将其转换为Tensor格式
train_data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data', header=None, sep='\s+').values
self.len = train_data.shape[0]
self.x_data = torch.from_numpy(train_data[:, :-1]).float()
self.y_data = torch.from_numpy(train_data[:, -1].reshape(-1,1)).float()
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
dataset = HousingDataset()
train_loader = DataLoader(dataset=dataset, batch_size=32, shuffle=True)
class LinearRegression(torch.nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = torch.nn.Linear(13, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearRegression()
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
epochs = 1000
for epoch in range(epochs):
for x, y in train_loader:
y_pred = model(x)
loss = criterion(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print('epoch {}, loss {}'.format(epoch, loss.item()))
五、总结
本文从安装与运行、Torch的基础操作、使用Torch进行深度学习、Torch的应用场景等多个方面详细阐述了conda torch。通过掌握本文所述的知识,可以熟练使用Torch进行深度学习、机器学习算法实现等各种任务。