import os
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
讀取數據
EPOCH =1# train the training data n times, to save time, we just train 1 epoch
BATCH_SIZE =1
DOWNLOAD_MNIST =False
LR =0.001# Mnist digits datasetifnot(os.path.exists('./mnist/'))ornot os.listdir('./mnist/'):# not mnist dir or mnist is empyt dirDOWNLOAD_MNIST =Truetrain_data = torchvision.datasets.MNIST(root='./mnist/',train=True,# this is training datatransform=torchvision.transforms.ToTensor(),download=DOWNLOAD_MNIST,)# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE)# , shuffle=True)
import osimport matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvisionEPOCH =5000# train the training data n times, to save time, we just train 1 epoch
BATCH_SIZE =60000
DOWNLOAD_MNIST =False# Mnist digits datasetifnot(os.path.exists('./mnist/'))ornot os.listdir('./mnist/'):# not mnist dir or mnist is empyt dirDOWNLOAD_MNIST =Truetrain_data = torchvision.datasets.MNIST(root='./mnist/',train=True,# this is training datatransform=torchvision.transforms.ToTensor(),download=DOWNLOAD_MNIST,)# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)classLogits(nn.Module):def__init__(self):super(Logits, self).__init__()self.linear = nn.Linear(28*28,10)self.sigmoid = nn.Sigmoid()self.softmax = nn.Softmax(dim=1)defforward(self, x):x = self.linear(x)x = self.sigmoid(x)x = self.softmax(x)return xtest_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor).cuda()/255.# shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labelsalpha =0.05logits = Logits().cuda()# optimizer = torch.optim.SGD(logits.parameters(), lr=LR) # optimize all cnn parameters# optimizer.zero_grad()
loss_func = nn.CrossEntropyLoss()# the target label is not one-hottedAccurate =[]
Astore =[]
bstore =[]
A, b =[i for i in logits.parameters()]
A.cuda()
b.cuda()
x, b_y =[(i, j)for i, j in train_loader][0]
b_x = x.view(-1,28*28).cuda()# reshape x to (batch, time_step, input_size)
b_y = b_y.cuda()for e inrange(EPOCH):output = logits(b_x)# logits outputloss = loss_func(output, b_y)# cross entropy lossif A.grad isnotNone:A.grad.zero_()b.grad.zero_()loss.backward()# backpropagation, compute gradientsA.data = A.data - alpha * A.grad.datab.data = b.data - alpha * b.grad.datatest_output = logits(test_x.view(-1,28*28))# print(e)if e %10==0:pred_y = torch.max(test_output,1)[1].cuda().data.squeeze()Accurate.append(sum(test_y.cpu().numpy()== pred_y.cpu().numpy())/(1.0*len(test_y.cpu().numpy())))print(e, Accurate[-1])Astore.append(A.detach())bstore.append(b.detach())test_output = logits(test_x.view(-1,28*28))
pred_y = torch.max(test_output,1)[1].cuda().data.squeeze()print(pred_y,'prediction number')print(test_y,'real number')
Accurate.append(sum(test_y.cpu().numpy()== pred_y.cpu().numpy())/(1.0*len(test_y.cpu().numpy())))print(Accurate[-1])for i inrange(len(Astore)):Astore[i]=(Astore[i]- Astore[-1]).norm()bstore[i]=(bstore[i]- bstore[-1]).norm()plt.plot(Astore, label='A')
plt.plot(bstore, label='b')
plt.legend()
plt.show()
plt.cla()
plt.plot(Accurate)
plt.show()