1.cnn#
Copy
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision import datasets, transforms torch.manual_seed(0) EPOCH = 1 BATCH_SIZE = 50 DOWNLOAD_MNIST = False train_dataset = datasets.MNIST( root="./mnist", train=True, transform=transforms.ToTensor(), download=False) test_dataset = datasets.MNIST( root="./mnist", train=False, transform=transforms.ToTensor(), download=False) train_loader = torch.utils.data.DataLoader( dataset=train_dataset, batch_size=100, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=100, shuffle=False) test_x = torch.unsqueeze(test_dataset.test_data, dim=1).type(torch.FloatTensor)[ :2000] / 255. test_y = test_dataset.test_labels[:2000] class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d( in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1) self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d( in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1) self.fc = nn.Linear(64 * 7 * 7, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 64 * 7 * 7) x = self.fc(x) return x model = CNN() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.01) for epoch in range(1): for i, (images, labels) in enumerate(train_loader): outputs = model(images) loss = criterion(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() if i % 50 == 0: data_all = model(test_x) last_layer = data_all test_output = data_all pred_y = torch.max(test_output, 1)[1].data.numpy() accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0)) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.4f' % accuracy) data_all1 = model(test_x[:10]) test_output = data_all1 _ = data_all1 pred_y = torch.max(test_output, 1)[1].data.numpy() print(pred_y, 'prediction number') print(test_y[:10].numpy(), 'real number')
2.bpnn#
Copy
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision import datasets, transforms import torchvision DOWNLOAD_MNIST = False # 表示还没有下载数据集,如果数据集下载好了就写False BATCH_SIZE = 50 LR = 0.01 # 学习率 # 下载mnist手写数据集 train_loader = torchvision.datasets.MNIST( root='./mnist/', # 保存或提取的位置 会放在当前文件夹中 train=True, # true说明是用于训练的数据,false说明是用于测试的数据 transform=torchvision.transforms.ToTensor(), # 转换PIL.Image or numpy.ndarray download=DOWNLOAD_MNIST, # 已经下载了就不需要下载了 ) test_loader = torchvision.datasets.MNIST( root='./mnist/', train=False # 表明是测试集 ) train_data = torch.utils.data.DataLoader(dataset=train_loader, batch_size=BATCH_SIZE, shuffle=True) # 为了节约时间, 我们测试时只测试前2000个 test_x = torch.unsqueeze(test_loader.test_data, dim=1).type(torch.FloatTensor)[ :2000] / 255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1) test_y = test_loader.test_labels[:2000] # 定义模型 class BPNN(nn.Module): def __init__(self): super(BPNN, self).__init__() self.fc1 = nn.Linear(28 * 28, 512)#定义了一个全连接层fc1,该层的输入是28 * 28个数字,输出是512个数字 self.fc2 = nn.Linear(512, 512) self.fc3 = nn.Linear(512, 10) def forward(self, x):#x是输入的图像 x = x.view(-1, 28 * 28)#将输入x的形状转换为二维,分别是batch_size和28 * 28 x = F.relu(self.fc1(x))#将x通过第1个全连接层fc1进行计算,并将结果通过ReLU激活函数处理 x = F.relu(self.fc2(x)) x = self.fc3(x) #Softmax函数是一种分类模型中常用的激活函数,它能将输入数据映射到(0,1)范围内,并且满足所有元素的和为1 return F.log_softmax(x, dim=1)#dim=1表示对每一行的数据进行运算 # 初始化模型 bpnn = BPNN() print(bpnn) # 定义损失函数和优化器 optimizer = torch.optim.Adam(bpnn.parameters(), lr=LR) # optimize all parameters loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted # # criterion = nn.NLLLoss() # optimizer = optim.SGD(bpnn.parameters(), lr=0.01, momentum=0.5) # 训练模型 for epoch in range(1): for step, (b_x,b_y) in enumerate(train_data): b_x = b_x.view(-1, 28, 28) # reshape x to (batch, time_step, input_size) output = bpnn(b_x) loss = loss_func(output, b_y) optimizer.zero_grad() loss.backward() optimizer.step() if step % 50 == 0: test_x = test_x.view(-1, 28, 28) test_output = bpnn(test_x) pred_y = torch.max(test_output, 1)[1].data.squeeze() acc = (pred_y == test_y).sum().float() / test_y.size(0) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.float(), 'test acc: ', acc.numpy()) test_output = bpnn(test_x[:10].view(-1, 28, 28)) pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() print(pred_y, 'prediction number') print(test_y[:10], 'real number') # # 评估模型 # bpnn.eval() # correct = 0 # with torch.no_grad(): # for data, target in test_loader: # output = bpnn(data) # pred = output.argmax(dim=1, keepdim=True) # correct += pred.eq(target.view_as(pred)).sum().item() # # print('Test accuracy:', correct / len(test_loader.dataset))
3.lstm#
Copy
import torch from torch import nn import torchvision.datasets as dsets import torchvision.transforms as transforms import matplotlib.pyplot as plt import numpy as np torch.manual_seed(1) EPOCH = 1 BATCH_SIZE = 64 TIME_STEP = 28 INPUT_SIZE = 28 LR = 0.01 DOWNLOAD_MNIST = False train_data = dsets.MNIST( root='./mnist/', train=True, transform=transforms.ToTensor(), download=DOWNLOAD_MNIST, ) test_data = dsets.MNIST(root='./mnist/', train=False) train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[ :2000] / 255. test_y = test_data.test_labels[:2000] class Lstm(nn.Module): def __init__(self): super(Lstm, self).__init__() self.Lstm = nn.LSTM( input_size=28, hidden_size=64, num_layers=1, batch_first=True, ) self.out = nn.Linear(64, 10) def forward(self, x): r_out, (h_n, h_c) = self.Lstm(x, None) out = self.out(r_out[:, -1, :]) return out Lstm = Lstm() print(Lstm) optimizer = torch.optim.Adam(Lstm.parameters(), lr=LR) loss_func = nn.CrossEntropyLoss() for epoch in range(EPOCH): for step, (x, b_y) in enumerate(train_loader): b_x = x.view(-1, 28, 28) output = Lstm(b_x) loss = loss_func(output, b_y) optimizer.zero_grad() loss.backward() optimizer.step() if step % 50 == 0: test_x = test_x.view(-1, 28, 28) test_output = Lstm(test_x) pred_y = torch.max(test_output, 1)[1].data.squeeze() acc = (pred_y == test_y).sum().float() / test_y.size(0) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.float(), 'test acc: ', acc.numpy()) test_output = Lstm(test_x[:10].view(-1, 28, 28)) pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() print(pred_y, 'prediction number') print(test_y[:10], 'real number')
出处:https://www.cnblogs.com/twq46/p/17115263.html