一、如何使用GPU训练
方法一:在网络模型,数据和损失函数处调用cuda()方法 在上一节的模型训练套路的代码中直接进行修改
train_data = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
class Test(nn.Module):
def __init__(self):
super(Test, self).__init__()
self.model1 = Sequential{
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
test = Test()
if torch.cuda.is_available():
test = test.cuda()
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
loss_fn = loss_fn.cuda()
learning_rate = 0.01
optimizer = torch.optim.SGD(test.parameters(), lr = learning_rate)
total_train_step = 0
total_test_step = 0
epoch = 10
for i in range(epoch):
print("---------第{}轮训练开始----------".format(i+1))
test.train()
for data in train_dataloader:
imgs, targets = data
if torch.cuda.is_available():
imgs = imgs.cuda()
targets = targets.cuda()
outputs = test(imgs)
loss = loss_fn(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_setp = total_train_step + 1
if total_train_step % 100 == 0
print("训练次数:{}, loss:{}".format(total_train_step, loss.item()))
test.eval()
total_test_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
if torch.cuda.is_available():
imgs = imgs.cuda()
targets = targets.cuda()
outputs = test(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test_loss + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuacy = total_accuracy + accuracy
print("整体测试集上的Loss:{}".format(total_test_loss))
print("整体测试集上的正确率:{}".format(total_accuracy/len(test_data)))
torch.save(test, "test_{}.pth".format(i))
方法二:定义运行设备,使用时直接修改device
device = torch.device("cuda")
train_data = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
class Test(nn.Module):
def __init__(self):
super(Test, self).__init__()
self.model1 = Sequential{
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
test = Test()
test.to(device)
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)
learning_rate = 0.01
optimizer = torch.optim.SGD(test.parameters(), lr = learning_rate)
total_train_step = 0
total_test_step = 0
epoch = 10
for i in range(epoch):
print("---------第{}轮训练开始----------".format(i+1))
test.train()
for data in train_dataloader:
imgs, targets = data
imgs = imgs.to(device)
targets = targets.to(device)
outputs = test(imgs)
loss = loss_fn(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_setp = total_train_step + 1
if total_train_step % 100 == 0
print("训练次数:{}, loss:{}".format(total_train_step, loss.item()))
test.eval()
total_test_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
imgs = imgs.to(device)
targets = targets.to(de)
outputs = test(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test_loss + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuacy = total_accuracy + accuracy
print("整体测试集上的Loss:{}".format(total_test_loss))
print("整体测试集上的正确率:{}".format(total_accuracy/len(test_data)))
torch.save(test, "test_{}.pth".format(i))
完整的模型测试套路
利用已经训练好的模型,然后给他提供输入
model.eval()
with torch.no_grad():
output = model(image)
print(output)
print(output.argmax(1))
结束
完结!大家可以去GitHub上面下载一些pytorch的代码训练学习啦!
|