写在前面

今天调通了pytorch模型,同时进行了简单的模型测试

知识点总结

1:python忽略某些特定语句的warning:

import warnings
        with warnings.catch_warnings():#ignore some warnings
            warnings.simplefilter("ignore")
            loss = criterion(outputs, labels)  # calculate loss

2:pytorch将cpu上的数据移动到gpu上:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device
mnist_net.to(device)
print(next(mnist_net.parameters()).device)

        images = images.to(device)
        labels = labels.to(device)

3:函数形参位置,带有默认值的参数应该放在没有默认值参数的后面,否则会报错的;

4:torch.max()用法:

parameter,index = torch.max(data,0), 返回列最大值以及对应索引,索引在后,数值在前

parameter,index = troch.max(data,1), 返回行最大值以及对应索引,索引在后,数值在前

5:pytorch在单行内进行刷新输出:

import sys

for i, (imgs, labs) in enumerate(usermnist_validate_loader):
    sys.stdout.write('\r'+str(i)+str())

6:pytorch报错:

“RuntimeError: expected scalar type Float but found Double”
这个错误是数据类型不统一引起的。

解决方法:

torch.set_default_tensor_type(torch.DoubleTensor)

这个语句主要是设置tensor的默认数据类型

今日工作

今天主要解决了之前程序里的上述第6个知识点里的bug,并且将整个程序了起来,源码如下:


# coding: utf-8

# In[1]:


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

#print beta
print(torch.__version__)


# In[2]:


from torch.utils.data import Dataset,DataLoader
import torchvision.transforms as transforms
import struct
import matplotlib.pyplot as plt


# In[3]:


torch.set_default_tensor_type(torch.DoubleTensor)


# In[4]:


import sys
import warnings


# In[16]:


user_train_imgs_path = './dataset/train-images.idx3-ubyte'# 6w
user_train_labels_path = './dataset/train-labels.idx1-ubyte'

user_validate_imgs_path = './dataset/t10k-images.idx3-ubyte'# 1w
user_validate_labels_path = './dataset/t10k-labels.idx1-ubyte'

#hyperparameters
input_size = 784 #28*28
hidden_size = 16
user_batch_size = 100
out_put_size = 10 #0~9


# In[25]:


class UserMNIST(Dataset):
    def __init__(self, imgs_path, labels_path, root='', train=True):
        super(UserMNIST, self).__init__()
        self.train = train #type of datasets
        self.train_nums = int(6e4)
        self.test_ratio = int(9e-1)
        self.validate_nums = int(1e4)
#         print(self.train_nums)#The scientific counting method is float,which should be changed by user

        #load files path
        self.imgs_folder_path = imgs_path
        self.labels_folder_path = labels_path
        if self.train :      
            self.img_nums = self.train_nums
        else:
            self.img_nums = self.validate_nums

        #load dataset
        with open(self.imgs_folder_path, 'rb') as _imgs:
            self._train_images = _imgs.read()
        with open(self.labels_folder_path, 'rb') as _labs:
            self._train_labels = _labs.read()


    def __getitem__(self, index):
        image = self.getImages(self._train_images, index)
        label = self.getLabels(self._train_labels, index)
        return image,label

    def __len__(self):
        return self.img_nums

    def getImages(self, image, index):
        img_size_bit = struct.calcsize('>784B')
        start_index = struct.calcsize('>IIII') + index * img_size_bit
        temp = struct.unpack_from('>784B', image, start_index)
        img = self.normalization(np.array(temp, dtype=float))
        return img

    def getLabels(self, label, index):
        lab_size_bit = struct.calcsize('>1B')
        start_index = struct.calcsize('>II') + index * lab_size_bit
        lab = struct.unpack_from('>1B', label, start_index)
        lab = torch.Tensor(lab)
        return lab

    def normalization(self, x):
        max = float(255)
        min = float(0)
        for i in range(0, 784):
            x[i] = (x[i] - min) / (max - min)
        return x



# In[26]:


usermnist_train = UserMNIST(user_train_imgs_path, user_train_labels_path, train=True)#how to one data,return numpy(user define) type
usermnist_train_loader = DataLoader(dataset=usermnist_train, batch_size=user_batch_size, shuffle=True)#do somethings to get all data,return tensor type

usermnist_validate = UserMNIST(user_validate_imgs_path, user_validate_labels_path, train=False)#how to one data,return numpy(user define) type
usermnist_validate_loader = DataLoader(dataset=usermnist_validate, batch_size=user_batch_size, shuffle=True)#do somethings to get all data,return tensor type


# In[27]:


img, lab = usermnist_train.__getitem__(6) # get the 34th sample
print(type(img))
print(type(lab))
# plt.imshow(img)
# plt.show()


# In[28]:


dataiter = iter(usermnist_train_loader)
images,labels = dataiter.next()
# print(images.shape, labels)
print(images.size(), labels.size(), labels.size(0))
print(type(images), type(labels))


# In[43]:


#set up NeuralNet
class NeuralNet(nn.Module):
    def __init__(self, input_size, hidden_size, out_put_size):
        super(NeuralNet, self).__init__()
        #recode hyperparameters
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.out_put_size = out_put_size

#       2 hidden_layers
        self.gap0 = nn.Linear(input_size, hidden_size)
        self.gap1 = nn.Linear(hidden_size, hidden_size)
        self.gap2 = nn.Linear(hidden_size, out_put_size)


    def forward(self, x):
        out = self.gap0(x)
        out = torch.relu(out)
        out = self.gap1(out)
        out = torch.relu(out)
        out = self.gap2(out)
        out = torch.sigmoid(out)
        return out

learning_rate = 1e-2
criterion = nn.MSELoss()

mnist_net = NeuralNet(input_size, hidden_size, out_put_size)
optimizer = torch.optim.SGD(mnist_net.parameters(), lr=learning_rate)
print(mnist_net)


# In[44]:


for name,parameters in mnist_net.named_parameters():
    print(name,':',parameters.size())
#     print(parameters)


# In[45]:


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device
mnist_net.to(device)
print(next(mnist_net.parameters()).device)


# In[47]:


epoches = 5
for epoch in range(epoches):
    print('current epoch = %d' % epoch)
    for i, (images, labels) in enumerate(usermnist_train_loader):  
#         validate_data(usermnist_validate_loader, mnist_net)
#         images = torch.Tensor(images)
#         labels = torch.Tensor(labels)
#         print(images, labels, images.size(), type(images), type(labels))
        images = images.to(device)
        labels = labels.to(device)

        optimizer.zero_grad()  
        outputs = mnist_net(images)  
        print(outputs[0], labels[0])
        with warnings.catch_warnings():#ignore some warnings
            warnings.simplefilter("ignore")
#             print(outputs[0], labels[0])
            loss = criterion(outputs, labels)  # calculate loss
        loss.backward()  
#         print(loss.device)
        optimizer.step()

        if i % user_batch_size == 0:
#             print(i)
            print('current loss = %.5f' % loss.item())


# In[46]:


def validate_data(usermnist_validate_loader, mnist_net):
    with torch.no_grad():
        total = 0
        correct = 0
        for i, (imgs, labs) in enumerate(usermnist_validate_loader):
#             print("idx: %d" %(i), end='|')
            sys.stdout.write('\r'+str(i)+str())
            imgs = imgs.to(device)
            labs = labs.to(device)
            outputs = mnist_net(imgs)
            print(outputs)
            _, predicted = torch.max(outputs, 1)
            total += labs.size(0)
            correct += (predicted == labs).sum()
#             print(correct, total)
    print('validate right rate: %d %%' % (100 * correct / total))

validate_data(usermnist_validate_loader, mnist_net)

明天就可以真正开始调参了。