1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
from torch import nn import torch.nn.functional as F
class Mnist_NN(nn.Module): def __init__(self): super().__init__() self.hidden1 = nn.Linear(784, 128) self.hidden2 = nn.Linear(128, 256) self.out = nn.Linear(256, 10)
def forward(self, x): x = F.relu(self.hidden1(x)) x = F.relu(self.hidden2(x)) x = self.out(x) return x
import gzip import pickle import torch from torch.utils.data import TensorDataset from torch.utils.data import DataLoader
with gzip.open('data/mnist/mnist.pkl.gz', 'rb') as f: ((x_train, y_train), (x_test, y_test), _) = pickle.load(f, encoding='latin-1') x_train, y_train, x_test, y_test = map( torch.tensor, (x_train, y_train, x_test, y_test) )
train_dataset = TensorDataset(x_train, y_train) test_dataset = TensorDataset(x_test, y_test)
def getData(train_dataset, test_dataset, batch_size): return ( DataLoader(train_dataset, batch_size=batch_size, shuffle=True), DataLoader(test_dataset, batch_size=batch_size * 2), )
import numpy as np from torch import optim
loss_func = F.cross_entropy
def getModel(): model = Mnist_NN() return model, optim.SGD(model.parameters(), lr=0.001)
def loss_batch(model, loss_func, x_bath, y_bath, opt=None): loss = loss_func(model(x_bath), y_bath) if opt is not None: loss.backward() opt.step() opt.zero_grad()
return loss.item(), len(x_bath)
def mnist(steps, model, loss_func, opt, train_data, test_data): for step in range(steps): for x_bath, y_bath in train_data: loss_batch(model, loss_func, x_bath, y_bath, opt)
with torch.no_grad(): losses, nums = zip( *[loss_batch(model, loss_func, x_bath, y_bath) for x_bath, y_bath in test_data] ) val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums) print('当前step:' + str(step), '验证集损失:' + str(val_loss))
|