请先看该文章。
当要运用到自己的图片文件的时候可在上文中加上以下代码,并相应进行改变神经网络中data运算。参考可见。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from torch.utils.data import Dataset, DataLoader class picture(Dataset): def __init__(self): self.x = data self.y = label self.len = len(self.y) def __len__(self): return self.len def __getitem__(self, item): return self.x[item], self.y[item] trainset = word() trainsetloader = torch.utils.data.DataLoader(trainset, batch_size=30, shuffle=False) |
准确率计算参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import torch from torch import nn from torchvision import datasets, transforms model = torch.load("my_handwrite_recognize_model.pt") transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) testset = datasets.MNIST(root='./data', train=True, download=True, transform=transform) testdataiter = iter(testset) sum = 0 correct = 0 for testimages, testlabels in testdataiter: sum += 1 img_vector = testimages[0].squeeze().view(1, -1) # 模型返回的是一个1x10的矩阵,第几个元素值最大那就是表示模型认为当前图片是数字几 result_digit = model(img_vector) if int(result_digit.max(1)[1]) == testlabels: correct += 1 print(correct / sum) |
下面用分类损失与回归损失代码对比。可供测试data文件。测试文件说明:最后一列为label,除最后一列外为data
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 |
import torch import numpy as np # 打开文件 with open('data.csv') as f: data = np.loadtxt(f, delimiter=",", skiprows=0) # 注意此时 label 是一维,不能是二维 label = data[:, -1] data = data[:, :-1] # numpy2torch data = torch.from_numpy(data).float() label = torch.from_numpy(label).long() # 网络结构 hidden = [32, 32, 32, 32] dim_in, dim_out = 7, 28 model = torch.nn.Sequential( torch.nn.Linear(dim_in, hidden[0]), torch.nn.ReLU(), torch.nn.Linear(hidden[0], hidden[1]), torch.nn.ReLU(), torch.nn.Linear(hidden[1], hidden[2]), torch.nn.ReLU(), torch.nn.Linear(hidden[2], dim_out), ) # 损失函数 loss_fn = torch.nn.CrossEntropyLoss() # 优化器 learning_rate = 1e-4 optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # 训练神经网络 for t in range(100000): # 让神经网络根据现有的参数,根据当前的输入计算一个输出 model_output = model(data) # 用所设计算损失(误差)函数计算误差 loss = loss_fn(model_output, label) print(loss) # 每次训练前清零之前计算的梯度(导数) optimizer.zero_grad() # 根据误差反向传播计算误差对各个权重的导数 loss.backward() # 根据优化器里面的算法自动调整神经网络权重 optimizer.step() total = len(data) corr = 0 for i in range(len(data)): label_ = model(data[i]).argmax() if label_ == label[i]: corr += 1 print(corr / total) # 保存下训练好的模型,省得下次再重新训练 torch.save(model, './word.pt') |
以上运用分类损失,效果一般,只有46%的分类准确率。
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 |
import torch import numpy as np # 打开文件 with open('data.csv') as f: data = np.loadtxt(f, delimiter=",", skiprows=0) # 注意此时 label 是二维,不能是一维 label = data[:, -1:] data = data[:, :-1] # numpy2torch data = torch.from_numpy(data).float() label = torch.from_numpy(label).float() # 网络结构 hidden = [32, 32, 32, 32] dim_in, dim_out = 7, 1 model = torch.nn.Sequential( torch.nn.Linear(dim_in, hidden[0]), torch.nn.ReLU(), torch.nn.Linear(hidden[0], hidden[1]), torch.nn.ReLU(), torch.nn.Linear(hidden[1], hidden[2]), torch.nn.ReLU(), torch.nn.Linear(hidden[2], dim_out), ) # 损失函数 loss_fn = torch.nn.MSELoss() def r2_loss(y_test, y): return 1 - ((y_test - y) ** 2).sum() / ((y.mean() - y) ** 2).sum() # 优化器 learning_rate = 1e-4 optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # 训练神经网络 for t in range(100000): # 让神经网络根据现有的参数,根据当前的输入计算一个输出 model_output = model(data) # 用所设计算损失(误差)函数计算误差 loss = loss_fn(model_output, label) print(loss) # 每次训练前清零之前计算的梯度(导数) optimizer.zero_grad() # 根据误差反向传播计算误差对各个权重的导数 loss.backward() # 根据优化器里面的算法自动调整神经网络权重 optimizer.step() print(r2_loss(model(data), label)) # 保存下训练好的模型,省得下次再重新训练 torch.save(model, './word2.pt') |
以上运用回归损失,效果显著,R2=0.96(评价函数)