步驟如下:
1.使用torchvision載入並預處理cifar-10資料集、
2.定義網路
3.定義損失函式和優化器
4.訓練網路並更新網路引數
5.測試網路
執行環境:
windows+python3.6.3+pycharm+pytorch0.3.0
import torchvision as tv
import torchvision.transforms as transforms
import torch as t
from torchvision.transforms import topilimage
show=topilimage() #把tensor轉成image,方便視覺化
import matplotlib.pyplot as plt
import torchvision
import numpy as np
###############資料載入與預處理
transform = transforms.compose([transforms.totensor(),#轉為tensor
transforms.normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),#歸一化
])#訓練集
trainset=tv.datasets.cifar10(root='/python projects/test/data/',
train=true,
download=true,
transform=transform)
trainloader=t.utils.data.dataloader(trainset,
batch_size=4,
shuffle=true,
num_workers=0)
#測試集
testset=tv.datasets.cifar10(root='/python projects/test/data/',
train=false,
download=true,
transform=transform)
testloader=t.utils.data.dataloader(testset,
batch_size=4,
shuffle=true,
num_workers=0)
classes=('plane','car','bird','cat','deer','dog','frog','horse','ship','truck')
(data,label)=trainset[100]
print(classes[label])
show((data+1)/2).resize((100,100))
# dataiter=iter(trainloader)
# images,labels=dataiter.next()
# print(''.join('11%s'%classes[labels[j]] for j in range(4)))
# show(tv.utils.make_grid(images+1)/2).resize((400,100))
defimshow
(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
dataiter = iter(trainloader)
images, labels = dataiter.next()
print(images.size())
imshow(torchvision.utils.make_grid(images))
plt.show()#關掉才能往後繼續算
#########################定義網路
import torch.nn as nn
import torch.nn.functional as f
class
net(nn.module):
def__init__
(self):
super(net,self).__init__()
self.conv1=nn.conv2d(3,6,5)
self.conv2=nn.conv2d(6,16,5)
self.fc1=nn.linear(16*5*5,120)
self.fc2=nn.linear(120,84)
self.fc3=nn.linear(84,10)
defforward
(self, x):
x = f.max_pool2d(f.relu(self.conv1(x)),2)
x = f.max_pool2d(f.relu(self.conv2(x)),2)
x = x.view(-1, 16 * 5 * 5)
x = f.relu(self.fc1(x))
x = f.relu(self.fc2(x))
x = self.fc3(x)
return x
net=net()
print(net)
#############定義損失函式和優化器
from torch import optim
criterion=nn.crossentropyloss()
optimizer=optim.sgd(net.parameters(),lr=0.01,momentum=0.9)
##############訓練網路
from torch.autograd import variable
import time
start_time = time.time()
for epoch in range(2):
running_loss=0.0
for i,data in enumerate(trainloader,0):
#輸入資料
inputs,labels=data
inputs,labels=variable(inputs),variable(labels)
#梯度清零
optimizer.zero_grad()
outputs=net(inputs)
loss=criterion(outputs,labels)
loss.backward()
#更新引數
optimizer.step()
# 列印log
running_loss += loss.data[0]
if i % 2000 == 1999:
print('[%d,%5d] loss:%.3f' % (epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('finished training')
end_time = time.time()
print("spend time:", end_time - start_time)
Pytorch實現CIFAR 10資料集
練習pytorch,做個記錄。寫的有點亂 import torch import torchvision import torch.nn as nn from torchvision import transforms from torch.utils.data.dataloader import ...
機器學習 利用pickle載入cifar檔案
然後奉獻 def load cifar10 root 載入cifar全部資料 xs ys for b in range 1,2 f os.path.join root,data batch d b,x,y load cifar batch f 將所有batch整合起來 xtr np.concaten...
利用pytorch對二維資料進行求解梯度
今天我們利用pytorch對二維資料進行求導並輸出顯示,主要用到pytorch中的linear mseloss 等函式,具體的求導過程詳見下面 import torch create tensors of shape 10,3 and 10,2 x torch.randn 10,3 y torch....