import torch
import numpy as np
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torch import nn
from scipy.io import loadmat
learning_rate = 0.01
epoch_num = 5
data = loadmat('/content/drive/MyDrive/colab/data/NUMIMG/ex4data1.mat')
print(data.keys())
print(len(data['X']),type(data['X']),data['X'].shape)
print(np.max(data['y']))
class ImagDataset(Dataset):
def __init__(self):
path = '/content/drive/MyDrive/colab/data/NUMIMG/ex4data1.mat'
data = loadmat(path)
self.data_x = torch.from_numpy(data['X']).float()
self.data_y = torch.from_numpy(data['y']).long().reshape(-1) - 1
self.sample_num = len(data['X'])
def __getitem__(self,index):
return self.data_x[index], self.data_y[index]
def __len__(self):
return self.sample_num
dataset = ImagDataset()
dataloader = DataLoader(dataset=dataset, batch_size=32,shuffle=True)
class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.linear1 = nn.Linear(400, 512)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(512, 10)
def forward(self, x):
out = self.linear1(x)
out = self.relu(out)
out = self.linear2(out)
return out
model = NeuralNet()
loss_fc = nn.CrossEntropyLoss()
optim = torch.optim.Adam(model.parameters(), lr=learning_rate)
total_step = len(dataloader)
print(total_step)
for epoch in range(epoch_num):
for i, (images, labels) in enumerate(dataloader):
out = model(images)
loss = loss_fc(out,labels)
optim.zero_grad()
loss.backward()
optim.step()
if (i + 1) % 32 == 0:
print('epoch [{}/{}] step [{}/{}] loss: {:.4f}'.format(epoch + 1, epoch_num, i + 1, total_step, loss.item()))
with torch.no_grad():
correct = 0
total = 0
for images,label in dataloader:
out = model(images)
val, pred = torch.max(out, 1)
total += images.shape[0]
correct += (pred == label).sum().item()
print('accuracy on {} test cases is {}%'.format(len(dataset), 100 * correct / len(dataset)))
里面有很多细节,比如在colab使用本地数据,并结合dataset和dataloader使用;dataset的一些细节问题
|