一、軟件安裝篇
pytorch安裝目前僅支持linux和mac安裝酌摇,windows下的小伙伴目前暫不提供安裝。我的電腦是Ubuntu 16.04 版本系統(tǒng),安裝pytorch的過程參考官網(wǎng)給出的步驟:
在命令窗口下運(yùn)行以下兩步
___第一步:pip install https://s3.amazonaws.com/pytorch/whl/cu75/torch-0.1.9.post2-cp27-none-linux_x86_64.whl ___
第二步:pip install torchvision
安裝過程脆炎,如果提示沒有權(quán)限的話,直接在pip前面加上管理員權(quán)限sudo氓辣。第二步中torchvision中主要包含了一些內(nèi)置數(shù)據(jù)集秒裕。
二、資料整理篇
參考資料來源知乎回答:新手如何入門pytorch?
三钞啸、軟件學(xué)習(xí)篇
該部分的參考資料絕大部分來源于pytorch官方文檔
1. torch.Tensor
torch.Tensor是一個(gè)類似于numpy中的多維矩陣几蜻,其中包含的元素類型只能有一種。
-
tensor(張量)創(chuàng)建
tensor可以通過python中的list或者sequence來創(chuàng)建体斩,例如
#torch.Tensor默認(rèn)等價(jià)于torch.FloatTensor
>>>torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
#除了列表梭稚;數(shù)組,矩陣這些都可以創(chuàng)建Tensor
當(dāng)然一個(gè)空的張量empty tensor可以通過指定其大小來構(gòu)建絮吵,例如
>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
[torch.IntTensor of size 2x4]
#zero函數(shù)后面帶的_什么作用等會(huì)馬上有講到
剛才提到過弧烤,tensor和numpy中的多維矩陣和類似的,因此同樣可以采用索引和切片對(duì)其進(jìn)行操作蹬敲,例如
>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
6.0
>>> x[0][1] = 8
>>> print(x)
1 8 3
4 5 6
[torch.FloatTensor of size 2x3]
每一個(gè)tensor都會(huì)對(duì)應(yīng)一個(gè)storage暇昂,storage中包含了該張量所有的數(shù)據(jù)莺戒,例如
>>>torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
>>>print x.storage()
1.0
2.0
3.0
4.0
5.0
6.0
[torch.FloatStorage of size 6]
-
tensor(張量)修改
構(gòu)建完tensor以后可以利用函數(shù)等對(duì)其進(jìn)行修改,在pytorch中凡是帶有下劃線(underscore)的處理操作都是直接對(duì)原始tensor進(jìn)行操作急波,并且返回修改后的tensor从铲,沒有帶下劃線的操作,是返回新的tensor幔崖,例如
#首先仍然創(chuàng)建一個(gè)tensor
>>>x = torch.FloatTensor([[1, 2, -3], [4, -5, 6]])
>>> print x
1 2 -3
4 -5 6
[torch.FloatTensor of size 2x3]
#我們利用tensor自帶的屬性abs和abs_進(jìn)行對(duì)比處理
a1 = x.abs() #生成新的a1張量食店,x并沒有改變
a2 = x.abs_() #生成新的a2張量的同時(shí),x也已經(jīng)發(fā)生改變
當(dāng)然除了絕對(duì)值函數(shù)赏寇,pytorch中還提供了非常多的處理張量的函數(shù)吉嫩,比如add,acos等等嗅定,詳情見官網(wǎng)說明自娩。
2. torch.autograd
torch.autograd能夠在盡可能少改動(dòng)現(xiàn)有代碼的前提下,對(duì)所有函數(shù)實(shí)現(xiàn)自動(dòng)微分功能渠退,當(dāng)然需要做的僅僅是將張量tensor包裝成為Varibale忙迁,例如
#首先仍然創(chuàng)建一個(gè)tensor x
>>>x = torch.FloatTensor([[1, 2, -3], [4, -5, 6]])
#然后將其轉(zhuǎn)化為Variable
from torch.autograd import Variable
x_trans = Variable(x)
print x_trans
autograd中的三大利器:torch.autograd.backward,torch.autograd.Variable和torch.autograd.Function碎乃,下面分別說下三個(gè)類的功能是什么姊扔。
torch.autograd.backward
torch.autograd.backward(variables, grad_variables, retain_variables=False)
其中,torch.autograd.Variable
官方文檔解析:Wraps a tensor and records the operations applied to it.其實(shí)就是對(duì)tensor進(jìn)行封裝梅誓,而且能夠記住對(duì)其做的所有操作(這個(gè)厲害了恰梢。。梗掰。嵌言。)。torch.autograd.Function
3. torch.nn
包含了目前最著名的三個(gè)網(wǎng)絡(luò)及穗,分別是RNN摧茴,LSTM和GRU
4. torch.nn.functional
import torch.nn.functional as F
其中主要包含了卷積函數(shù),下采樣Pooling 函數(shù)埂陆,Non-linear activation functions苛白,損失函數(shù)(loss function),dropout焚虱,batch-Normalization 等购裙,對(duì)卷積和采樣函數(shù)舉例如下
- __Convolution __
>>> import torch.nn.functional as F
>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)
#最終輸出結(jié)果為度為20*33*48;要理解這個(gè)值是怎么計(jì)算出來著摔,需要對(duì)卷積神經(jīng)網(wǎng)絡(luò)中的卷積計(jì)算有了解缓窜,
#理解卷積計(jì)算層的三條秘訣:
##(1)局部關(guān)聯(lián),每個(gè)神經(jīng)元看做一個(gè)filter
##(2)窗口滑動(dòng),filter對(duì)局部進(jìn)行計(jì)算
##(3)涉及概念:窗口禾锤、長度和步長值
- pooling
>>> import torch.nn.functional as F
>>> input = Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
#kernel_size是窗口長度私股,stride是步長,采用均值方式
>>> print F.avg_pool1d(input,kernel_size=3,stride=2)
#輸出結(jié)果為
Variable containing:
(0 ,.,.) =
2 4 6
[torch.FloatTensor of size 1x1x3]
- 正則化和dropout
>>> from torch import nn
>>> m = nn.Dropout(p=0.2)
>>> input = autograd.Variable(torch.randn(20, 16))
>>> output = m(input)
#輸出結(jié)果為20*16,其中有些值被隨機(jī)的置為0(按列還是按行恩掷?)