什么是pytorch?


Pytorch是基於python的科學計算包,為兩類受眾提供服務

  • 作為Numpy的替換,讓你可以使用GPU的算力
  • 作為一個深度學習計算平台提供最大的計算靈活性與速度

開始體驗pytorch的基礎功能

Tensor:

tensor與Numpy的高維數據概念類似,可以在GPU上進行計算

import torch

建立一個5*3的未初始化的tensor

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

out:
tensor(1.00000e-07 *
       [[-0.0000,  0.0000,  0.0000], [ 0.0000, 9.4713, 0.0000], [ 9.4201, 0.0000, 0.0000], [ 0.0000, -0.0000, 0.0000], [-0.0000, 0.0000, -0.0000]])

 建立一個隨機初始化的tensor

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

out:

tensor([[ 0.7816, 0.8146, 0.9424],

[ 0.0888, 0.5530, 0.9181],
[ 0.8362, 0.1937, 0.0084],
[ 0.2004, 0.2818, 0.8674],
[ 0.6464, 0.4978, 0.8994]])

 建立一個tensor用0填充並使用long類型

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

out:

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

 直接從數據創建tensor

torch.tensor([5.5,3])
print(x)

out:

tensor([ 5.5000, 3.0000])

我們也可以基於現有的tensor建立新的tensor,這樣新的tensor會復用之前的屬性,比如類型等

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

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

out:

tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=torch.float64)

tensor([[ 0.3648, 0.5067, 1.1720],
[-1.3361, 0.9999, 0.4133],
[-0.2699, 0.7601, -1.1138],
[-1.8955, -0.4079, 1.0827],
[-0.0156, 0.3810, 1.2646]])

獲得tensor尺寸

print(x.size())

out:

torch.Size([5, 3])

 

運算

y=torch.rand(5,3)
#torch.add(x,y)
print(x+y)

out:

tensor([[ 1.0363, 0.8469, 1.1758],
[ 1.5991, 0.8271, 1.2000],
[ 0.9036, 1.1352, 1.4293],
[ 1.3676, 0.8430, 0.7633],
[ 1.3159, 1.4635, 1.9067]])

 提供輸出變量作為參數

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

out:
tensor([[ 1.0363,  0.8469,  1.1758],
        [ 1.5991,  0.8271,  1.2000],
        [ 0.9036,  1.1352,  1.4293],
        [ 1.3676,  0.8430,  0.7633],
        [ 1.3159,  1.4635,  1.9067]])

同時提供了內建函數,內建函數會影響變量本身的值,如x.copy_(y),x.t_()會影響x的值

y.add_(x)
print(y)

out:
tensor([[ 1.0363,  0.8469,  1.1758],
        [ 1.5991,  0.8271,  1.2000],
        [ 0.9036,  1.1352,  1.4293],
        [ 1.3676,  0.8430,  0.7633],
        [ 1.3159,  1.4635,  1.9067]])

你可以使用標准Numpy的索引的所有寫法!!

print(x[:,1])
out:
tensor([ 0.2492,  0.7801,  0.5992,  0.8164,  0.6371])

調整形狀:如果你想重新調整tensor的維度,可以使用tensor.view

x=torch.randn(4,4)
y=torch.view(16)
z=torch.view(-1,8)
print(x.size(),y.size(),z.size())

out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

如果你有一個只包含一個元素的tensor,可以使用.item()來得到python形式的值

x=torch.randn(1)
print(x)
print(x.item())
out:
tensor([ 0.4210])
0.4210202693939209

 

Numpy橋

torch的tensor與Numpy數組之間的轉換是很輕松的

torch的tensor與Numpy的數組共享同一塊內存空間,改變一者會影響另一個

將torch tensor轉換為Numpy數組:

a=torch.ones(5)
print(a)
b=a.numpy()
print(b) out: tensor([
1., 1., 1., 1., 1.])

[1. 1. 1. 1. 1.]

觀察numpy數組b的變化

a.add_(1)
print(a)
print(b)

out:
tensor([ 2., 2., 2., 2., 2.])

 [2. 2. 2. 2. 2.]

 

將Numpy數組轉換為torch tensor:

觀察numpy數組如何引起torch tensor的變化

 

import numpy as np
a=no.ones(5)
b=torch.from_numpy(a)
np.add(a,1,out=a)
print(a) print(b)

out:
array([2., 2., 2., 2., 2.])
tensor([ 2., 2., 2., 2., 2.], dtype=torch.float64)

 

除了CharTensor外CPU上的tensor都可以轉換為numpy並返回

 

CUDA Tensor

if torch.cuda.is_available():
    device=torch.device("cuda")  #一個CUDA設備目標
    y=torch.ones_like(x,device=device)  #直接在GPU上建立變量
    x=x.to(device)
    z=x+y
    print(z)
    print(z.to("cpu",torch.double))

out:
tensor([[ 1.4325, -0.1697, 2.2435],
[ 0.6878, 0.9155, 1.4876]], device='cuda:0')

tensor([[ 1.4325, -0.1697, 2.2435],
[ 0.6878, 0.9155, 1.4876]], dtype=torch.float64)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM