import os
# third-party library
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
# 定义一些参数
EPOCH = 1 # 训练数据的次数,我们这里假定训练一次
BATCH_SIZE = 50 # 每次训练的数据量,这个会产生每一次训练分多少次进行,或者多少批进行
LR = 0.001 # 学习率
DOWNLOAD_MNIST = False
# 下载并且加载数据集
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
# not mnist dir or mnist is empyt dir
DOWNLOAD_MNIST = True
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True, # 表示训练数据
transform=torchvision.transforms.ToTensor(), # 将数据转换成tensor
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST,
)
# plot one example
print(train_data.train_data.size()) # (60000, 28, 28)
print(train_data.train_labels.size())