PyTorch 入门实战
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_36556893/article/details/86480120
前言
博主學(xué)習(xí)了PyTorch,發(fā)現(xiàn)這個(gè)深度學(xué)習(xí)框架適合研究和使用。
當(dāng)然,博主熱衷于vs2017這款I(lǐng)DE。雖然博主同樣安裝了PyCharm和Eclipse,但是由于寫(xiě)C++Debug的習(xí)慣,還是選擇了vs2017(和vs2019)作為深度學(xué)習(xí)的集成開(kāi)發(fā)環(huán)境,希望可以幫助博主學(xué)習(xí)深度學(xué)習(xí)知識(shí)。
環(huán)境安裝
主要參考博主的這篇文章:vs2017 開(kāi)始自己的第一個(gè)PyTorch程序
在之后的相關(guān)教程里博主不會(huì)再給出安裝的具體步驟,因?yàn)榘错樞蚩吹脑挍](méi)有這樣的問(wèn)題嘿嘿~
PyTorch入門(mén)實(shí)戰(zhàn)
1.博客:PyTorch 入門(mén)實(shí)戰(zhàn)(一)——Tensor
2.博客:PyTorch 入門(mén)實(shí)戰(zhàn)(二)——Variable
3.博客:PyTorch 入門(mén)實(shí)戰(zhàn)(三)——Dataset和DataLoader
4.博客:PyTorch 入門(mén)實(shí)戰(zhàn)(四)——利用Torch.nn構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)
5.博客:PyTorch 入門(mén)實(shí)戰(zhàn)(五)——2013kaggle比賽 貓狗大戰(zhàn)的實(shí)現(xiàn)
目錄
前言
環(huán)境安裝
一、Tensor的創(chuàng)建和使用
二、Tensor放到GPU上執(zhí)行
三、Tensor總結(jié)
一、Tensor的創(chuàng)建和使用
1.概念和TensorFlow的是基本一致的,只是代碼編寫(xiě)格式的不同。我們聲明一個(gè)Tensor,并打印它,例如:
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([1, 2], [3, 4],[5, 6], [7, 8])
print(a)
print('{}'.format(a))
然后會(huì)發(fā)現(xiàn)報(bào)以下錯(cuò)誤:
new() received an invalid combination of arguments - got (list, list, list, list), but expected one of: ?* (torch.device device) ?* (torch.Storage storage) ?* (Tensor other) ?* (tuple of ints size, torch.device device) ?* (object data, torch.device device)?
?????????????????????????????
意思是接收到無(wú)效的參數(shù)組合。其實(shí)是少寫(xiě)了一對(duì)中括號(hào),這是初學(xué)者的常用錯(cuò)誤。
2.改成如下形式:
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(a)
print('{}'.format(a))
結(jié)果為:
??????????????????????????????????????????????????????????????
3.如果想查看的它的大小可以加一句話:
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a.size()))
結(jié)果為:
??????????????????????????????????????????????????????????????
即4行2列的矩陣
4.如果想生成一個(gè)全為0的矩陣,可以輸入如下代碼:
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a.size()))
b = torch.zeros((4, 2))
print(b)
結(jié)果為:
???????????????????????????????????????????????????????????
即4行2列數(shù)組元素全為0的矩陣
5.如果想生成不同類(lèi)型的數(shù)據(jù),可以改變torch.后面函數(shù)名稱(chēng),例如下面這樣:
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a))
b = torch.zeros((4, 2))
print(b)
c = torch.IntTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(c)
d = torch.LongTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(d)
e = torch.DoubleTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(e)
結(jié)果為:
??????????????????????????????????????????????????????????
6.如果想訪問(wèn)Tensor里的一個(gè)元素或者改變它,可以輸入如下代碼:
print(e[1, 1])
#改變?cè)刂?br /> e[1, 1] = 3
print(e[1, 1])
代碼變?yōu)?#xff1a;?
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a))
b = torch.zeros((4, 2))
print(b)
c = torch.IntTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(c)
d = torch.LongTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(d)
e = torch.DoubleTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(e)
print(e[1, 1])
#改變?cè)刂?br /> e[1, 1] = 3
print(e[1, 1])
結(jié)果為:
?????????????????????????????????????????????????????????????????
說(shuō)明原來(lái)4的位置數(shù)值變?yōu)榱?
7.最重要的是Tensor和Numpy之間的轉(zhuǎn)換,例如我們把e變?yōu)閚umpy類(lèi)型,添加以下代碼:
f = e.numpy()
print(f)
變?yōu)?#xff1a;
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a))
b = torch.zeros((4, 2))
print(b)
c = torch.IntTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(c)
d = torch.LongTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(d)
e = torch.DoubleTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(e)
print(e[1, 1])
#改變?cè)刂?br /> e[1, 1] = 3
print(e[1, 1])
#轉(zhuǎn)換為Numpy
f = e.numpy()
print(f)
結(jié)果為:?
?????????????????????????????????????????????????????????????
可以看到?jīng)]有tensor()了~
我們?cè)侔裦變?yōu)閠ensor類(lèi)型,輸入以下代碼:
g = torch.from_numpy(f)
print(g)
變?yōu)?#xff1a;
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a))
b = torch.zeros((4, 2))
print(b)
c = torch.IntTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(c)
d = torch.LongTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(d)
e = torch.DoubleTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(e)
print(e[1, 1])
#改變?cè)刂?br /> e[1, 1] = 3
print(e[1, 1])
#轉(zhuǎn)換為Numpy
f = e.numpy()
print(f)
#轉(zhuǎn)換為T(mén)ensor
g = torch.from_numpy(f)
print(g)
結(jié)果為:
??????????????????????????????????????????????????????????????????
可以看到又變成了Tensor類(lèi)型~
二、Tensor放到GPU上執(zhí)行
1.通過(guò)如下代碼判斷是否支持GPU:
if torch.cuda.is_available():
? ? h = g.cuda()
? ? print(h)
變?yōu)?/p>
import torch
#定義一個(gè)Tensor矩陣
a = torch.Tensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print('{}'.format(a))
b = torch.zeros((4, 2))
print(b)
c = torch.IntTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(c)
d = torch.LongTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(d)
e = torch.DoubleTensor([[1, 2], [3, 4],[5, 6], [7, 8]])
print(e)
print(e[1, 1])
#改變?cè)刂?br /> e[1, 1] = 3
print(e[1, 1])
#轉(zhuǎn)換為Numpy
f = e.numpy()
print(f)
#轉(zhuǎn)換為T(mén)ensor
g = torch.from_numpy(f)
print(g)
#將Tensor放在GPU上
if torch.cuda.is_available():
? ? h = g.cuda()
? ? print(h)
2.生成結(jié)果會(huì)慢一下,然后可以看到多了一個(gè)device=‘cuda:0’:
?????????????????????????????????????????????????
三、Tensor總結(jié)
1.Tensor和Numpy都是矩陣,區(qū)別是前者可以在GPU上運(yùn)行,后者只能在CPU上
2.Tensor和Numpy互相轉(zhuǎn)化很方便,類(lèi)型也比較兼容
3.Tensor可以直接通過(guò)print顯示數(shù)據(jù)類(lèi)型,而Numpy不可以,例如:dtype = torch.float64
————————————————
版權(quán)聲明:本文為CSDN博主「悲戀花丶無(wú)心之人」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_36556893/article/details/86480120
總結(jié)
以上是生活随笔為你收集整理的PyTorch 入门实战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pythorch基本信息查询
- 下一篇: pythorch学习笔记