学习pytorch (下)
——莫烦python之CNN
我通过莫烦python教学视频入门pytorch,通过分析一篇cnn代码样例进行学习。
1. 全局代码
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
#Hyper parameter
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root = './mnist',
train = True,
transform = torchvision.transform.ToTensor(),
download = DOWNLOAD_MNIST
)
# #plot one example
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(),cmap='gray')
# plt.title('%i'%train_data.train_labels[0])
# plt.show()
train_loader = Data.DataLoader(
dataset = train_data,
batch_size = BATCH_SIZE,
shuffle = True,
num_workers