本例以pytorch框架进行实验:
#coding = utf-8
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.optim as optim
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
from torch.autograd import Variable
# torch.manual_seed(1) # reproducible
# Hyper Parameters
EPOCH = 5
BATCH_SIZE = 64
LR = 0.001 # learning rate
DOWNLOAD_MNIST = True
N_TEST_IMG = 5
# Mnist digits dataset
train_data = torchvision.datasets.MNIST(
root='./mnist_data/',
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST, # download it if