赞
踩
总结 - Training | Microsoft Learn 博主在微软教程中有引用,同学们要是想深入研究可以自行追溯
本博客从零带大家起一个简单的深度学习模型(强学校相关性,弱迁移性)
以下是详细代码:
第1步:引用+下载数据集
- import torch
- from torch.utils.data import Dataset
- from torchvision import datasets
- from torchvision.transforms import ToTensor, Lambda
- import matplotlib.pyplot as plt
- import os
- os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
-
- training_data = datasets.FashionMNIST(
- root="data",
- train=True,
- download=True,
- transform=ToTensor()
- )
-
- test_data = datasets.FashionMNIST(
- root="data",
- train=False,
- download=True,
- transform=ToTensor()
- )
第2步:
- labels_map = {
- 0: "T-Shirt",
- 1: "Trouser",
- 2: "Pullover",
- 3: "Dress",
- 4: "Coat",
- 5: "Sandal",
- 6: "Shirt",
- 7: "Sneaker",
- 8: "Bag",
- 9: "Ankle Boot",
- }
- figure = plt.figure(figsize=(8, 8))
- cols, rows = 3, 3
- for i in range(1, cols * rows + 1):
- sample_idx = torch.randint(len(training_data), size=(1,)).item()
- img, label = training_data[sample_idx]
- figure.add_subplot(rows, cols, i)
- plt.title(labels_map[label])
- plt.axis("off")
- plt.imshow(img.squeeze(), cmap="gray")
- plt.show()
第3步:
- from torch.utils.data import DataLoader
-
- train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
- test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)
-
-
- for X, y in test_dataloader:
- print("Shape of X [N, C, H, W]: ", X.shape)
- print("Shape of y: ", y.shape, y.dtype)
- break
-
- # Display sample data
- figure = plt.figure(figsize=(10, 8))
- cols, rows = 5, 5
- for i in range(1, cols * rows + 1):
- idx = torch.randint(len(test_data), size=(1,)).item()
- img, label = test_data[idx]
- figure.add_subplot(rows, cols, i)
- plt.title(label)
- plt.axis("off")
- plt.imshow(img.squeeze(), cmap="gray")
- plt.show()
第4步:Dataset 简而言之就是批次处理数据集
- from torchvision import datasets
- from torchvision.transforms import ToTensor, Lambda
-
- ds = datasets.FashionMNIST(
- root="data",
- train=True,
- download=True,
- transform=ToTensor(),
- target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
- )
第5步:!!!!
- device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
- print('Using {} device'.format(device))
我们可以win+R调用出终端Terminal
输入 nvidia-smi
查看电脑显卡调用情况 学校的台式机只有一个GPU 0卡,所以就cuda:0即可
例:
第6步:设定Model模型
- from torch import nn
-
- class NeuralNetwork(nn.Module):
- def __init__(self):
- super(NeuralNetwork, self).__init__()
- self.flatten = nn.Flatten()
- self.linear_relu_stack = nn.Sequential(
- nn.Linear(28*28, 512),
- nn.ReLU(),
- nn.Linear(512, 512),
- nn.ReLU(),
- nn.Linear(512, 10),
- nn.ReLU()
- )
-
- def forward(self, x):
- x = self.flatten(x)
- logits = self.linear_relu_stack(x)
- return logits
-
- model = NeuralNetwork().to(device)
- print(model)
第7步:设定损失、学习率、迭代器等变量以及训练测试模型
- loss_fn = nn.CrossEntropyLoss()
- learning_rate = 1e-3
- optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
-
- def train(dataloader, model, loss_fn, optimizer):
- size = len(dataloader.dataset)
- for batch, (X, y) in enumerate(dataloader):
- X, y = X.to(device), y.to(device)
-
- # Compute prediction error
- pred = model(X)
- loss = loss_fn(pred, y)
-
- # Backpropagation
- optimizer.zero_grad()
- loss.backward()
- optimizer.step()
-
- if batch % 100 == 0:
- loss, current = loss.item(), batch * len(X)
- print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
-
- def test(dataloader, model):
- size = len(dataloader.dataset)
- model.eval()
- test_loss, correct = 0, 0
- with torch.no_grad():
- for X, y in dataloader:
- X, y = X.to(device), y.to(device)
- pred = model(X)
- test_loss += loss_fn(pred, y).item()
- correct += (pred.argmax(1) == y).type(torch.float).sum().item()
- test_loss /= size
- correct /= size
- print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
第8步:
- epochs = 15
- for t in range(epochs):
- print(f"Epoch {t+1}\n-------------------------------")
- train(train_dataloader, model, loss_fn, optimizer)
- test(test_dataloader, model)
- print("Done!")
最后,存储模型参数,供之后使用
- torch.save(model.state_dict(), "data/model.pth")
- print("Saved PyTorch Model State to model.pth")
-
- model = NeuralNetwork()
- model.load_state_dict(torch.load("data/model.pth"))
-
- classes = [
- "T-shirt/top",
- "Trouser",
- "Pullover",
- "Dress",
- "Coat",
- "Sandal",
- "Shirt",
- "Sneaker",
- "Bag",
- "Ankle boot",
- ]
-
- model.eval()
- x, y = test_data[0][0], test_data[0][1]
- with torch.no_grad():
- pred = model(x)
- predicted, actual = classes[pred[0].argmax(0)], classes[y]
- print(f'Predicted: "{predicted}", Actual: "{actual}"')
以上就是全部过程,感谢大家
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。