PyTorch 入門

PyTorch 入門
什么是 PyTorch?
PyTorch 是一個(gè)基于 Python 的科學(xué)計(jì)算包,主要定位兩類人群:

NumPy 的替代品,可以利用 GPU 的性能進(jìn)行計(jì)算。
深度學(xué)習(xí)研究平臺(tái)擁有足夠的靈活性和速度
開始學(xué)習(xí)
Tensors (張量)
Tensors 類似于 NumPy 的 ndarrays ,同時(shí) Tensors 可以使用 GPU 進(jìn)行計(jì)算。

from future import print_function
import torch

構(gòu)造一個(gè)5x3矩陣,不初始化。

x = torch.empty(5, 3)
print(x)

輸出:

tensor(1.00000e-04 *
[[-0.0000, 0.0000, 1.5135],
[ 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000]])

構(gòu)造一個(gè)隨機(jī)初始化的矩陣:

x = torch.rand(5, 3)
print(x)

輸出:

tensor([[ 0.6291, 0.2581, 0.6414],
[ 0.9739, 0.8243, 0.2276],
[ 0.4184, 0.1815, 0.5131],
[ 0.5533, 0.5440, 0.0718],
[ 0.2908, 0.1850, 0.5297]])

構(gòu)造一個(gè)矩陣全為 0,而且數(shù)據(jù)類型是 long.

x = torch.zeros(5, 3, dtype=torch.long)
print(x)

輸出:

tensor([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]])

構(gòu)造一個(gè)張量,直接使用數(shù)據(jù):

x = torch.tensor([5.5, 3])
print(x)
輸出:

tensor([ 5.5000, 3.0000])
創(chuàng)建一個(gè) tensor 基于已經(jīng)存在的 tensor。

x = x.new_ones(5, 3, dtype=torch.double)

new_* methods take in sizes

print(x)

x = torch.randn_like(x, dtype=torch.float)

override dtype!

print(x)

result has the same size

輸出:

tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=torch.float64)
tensor([[-0.2183, 0.4477, -0.4053],
[ 1.7353, -0.0048, 1.2177],
[-1.1111, 1.0878, 0.9722],
[-0.7771, -0.2174, 0.0412],
[-2.1750, 1.3609, -0.3322]])
獲取它的維度信息:

print(x.size())
輸出:

torch.Size([5, 3])
注意

torch.Size
是一個(gè)元組,所以它支持左右的元組操作。

操作
在接下來的例子中,我們將會(huì)看到加法操作。
加法: 方式 1

y = torch.rand(5, 3)
print(x + y)
Out:

tensor([[-0.1859, 1.3970, 0.5236],
[ 2.3854, 0.0707, 2.1970],
[-0.3587, 1.2359, 1.8951],
[-0.1189, -0.1376, 0.4647],
[-1.8968, 2.0164, 0.1092]])
加法: 方式2

print(torch.add(x, y))
Out:

tensor([[-0.1859, 1.3970, 0.5236],
[ 2.3854, 0.0707, 2.1970],
[-0.3587, 1.2359, 1.8951],
[-0.1189, -0.1376, 0.4647],
[-1.8968, 2.0164, 0.1092]])
加法: 提供一個(gè)輸出 tensor 作為參數(shù)

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:

tensor([[-0.1859, 1.3970, 0.5236],
[ 2.3854, 0.0707, 2.1970],
[-0.3587, 1.2359, 1.8951],
[-0.1189, -0.1376, 0.4647],
[-1.8968, 2.0164, 0.1092]])
加法: in-place

adds x to y

y.add_(x)
print(y)
Out:

tensor([[-0.1859, 1.3970, 0.5236],
[ 2.3854, 0.0707, 2.1970],
[-0.3587, 1.2359, 1.8951],
[-0.1189, -0.1376, 0.4647],
[-1.8968, 2.0164, 0.1092]])
Note

注意 任何使張量會(huì)發(fā)生變化的操作都有一個(gè)前綴 ''。例如:
x.copy
(y)
,
x.t_()
, 將會(huì)改變
x
.
你可以使用標(biāo)準(zhǔn)的 NumPy 類似的索引操作

print(x[:, 1])
Out:

tensor([ 0.4477, -0.0048, 1.0878, -0.2174, 1.3609])
改變大?。喝绻阆敫淖円粋€(gè) tensor 的大小或者形狀,你可以使用
torch.view
:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
Out:

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一個(gè)元素 tensor ,使用 .item() 來獲得這個(gè) value 。

x = torch.randn(1)
print(x)
print(x.item())
Out:

tensor([ 0.9422])
0.9422121644020081
PyTorch windows 安裝教程:兩行代碼搞定 PyTorch 安裝

http://pytorchchina.com/2018/12/11/pytorch-windows-install-1/

PyTorch Mac 安裝教程

http://pytorchchina.com/2018/12/11/pytorch-mac-install/

PyTorch Linux 安裝教程

http://pytorchchina.com/2018/12/11/pytorch-linux-install/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容