1. 程式人生 > 實用技巧 >pytorch 基礎內容

pytorch 基礎內容

一些基礎的操作:

import torch as th

a=th.rand(3,4) 
b=th.rand(4)
a+b
tensor([[1.0636, 0.3395, 0.6914, 0.7105], [0.6596, 0.2712, 0.6118, 0.1983], [0.4344, 0.8021, 0.8393, 0.2529]])
a=th.ones(3,4)*5
b=th.ones(4)
a/b

tensor([[5., 5., 5., 5.],
[5., 5., 5., 5.],
[5., 5., 5., 5.]])

a=th.full([2,2],3)
a.pow(2)

tensor([[9., 9.],

[9., 9.]])

a.sqrt()
tensor([[1.7321, 1.7321],    [1.7321, 1.7321]])
a=th.exp(th.ones(2,2))

tensor([[2.7183, 2.7183],
[2.7183, 2.7183]])

th.log(a)

tensor([[1., 1.],
[1., 1.]])

grad=th.rand(2,3)*15
grad.max()
grad.median()

tensor(12.9437)

tensor(5.4597)

grad.clamp(0,10)

tensor([[ 5.4597, 10.0000, 2.1924],

[ 3.2563, 10.0000, 10.0000]])

import torch
import numpy as np
a= torch.tensor([1., 2., 3.])  #通過list初始化一個tensor
#print(a)
a.shape
b = torch.Tensor(2, 3)  #注意是大寫Tensor,輸入的是維度資訊
#print(b)
c = np.array([1., 2., 3.])
c = torch.from_numpy(c) #將numpy型別的資料轉換為tensor型別
#print(c)
c.type() #tensor的型別
b.dim() #tensor有幾個維度
b.numel() #tensor的元素個數 d=torch.randn(3,2,1) f = torch.rand_like(d) #和輸入的tensor的維度一致的隨機tensor f = torch.randn(3, 3) #0均值,方差為1的隨機正態分佈 f = torch.normal(mean=torch.full([10], 0), std=torch.arange(1, 0, -0.1))# 返回一個張量,包含從給定引數means,std的離散正態分佈中抽取隨機數。 均值means是一個張量,包含每個輸出元素相關的正態分佈的均值。 std是一個張量,包含每個輸出元素相關的正態分佈的標準差。 f = torch.full([2, 3], 7) #用後面的值填充一個tensor f = torch.arange(1,10) #得到int的tensor,並且不包含最後一個元素,左閉右開 #f = torch.range(1, 10, 2) #預設的浮點數, 同時左閉右閉 f = torch.randperm(2) #生產n個從1~n隨機順序的整數值

# pytorch 索引和切片
import torch as th

a=th.randn(4, 3, 28, 28)
a[0].shape  #將a理解為4張圖片,每張圖片3個通道,每個通道的維度為28*28,a[0]就是取第一個圖片
a[0,0].shape #取0圖的0通道
a[0,0,2,4]  #取0圖0通道的2,4位置畫素大小

a.shape
a[:2].shape  #表示第0,1張圖片。 :為開區間的取值範圍
a[:2,-1:,:,:].shape #-1表示last N. python中可以正著編號,也可以從後向前編號(負數)

a[:,:,::2,::2].shape #::2, 表示間隔為2取值,select by steps

a.index_select(2,th.arange(8)).shape #select by specific index 按照索引進行取值,第一個引數為選擇維度,後面為選擇的索引

x=th.randn(3,4)
mask=x.ge(0.5)  #進行mask標記
th.masked_select(x,mask)  #選取mask的元素
# pytorch Tensor維度變換
import torch as th\

#1 view /reshape
a=th.rand(4,1,28,28)
a.view(4, 28*28).shape #必須保持合理的邏輯

#2 squeeze (去除掉維度為1的dim) / unsqueeze (在指定位置新增維度)
b=th.randn(1,2,3,5)
b.unsqueeze(0).shape #torch.Size([1, 2, 3, 5])
b.unsqueeze(-1).shape #torch.Size([1, 2, 3, 5, 1])

b.squeeze().shape #torch.Size([2, 3, 5]) ,當然也可以指定削減那個維度

#3 expand / repeat   
# Expand: broadcasting 擴充套件維度,在需要時才擴充套件資料, 推薦使用
# Repeat: memory copied

b=th.rand(1, 32, 1, 1)
b.expand(4, 32, 14, 14).shape #注意,expand, 擴充套件前後的維度數要一致,並且是1->N 才可以。 2->N 會報錯

b=th.rand(1, 32, 1, 1)
b.expand(-1, 32, 14, 12).shape # -1表示該維度不進行擴充套件  torch.Size([1, 32, 14, 12])

b=th.rand(1, 32, 1, 1)
b.repeat(4, 32, 1, 1).shape  #第一個維度拷貝4次, 第二個維度拷貝32次  torch.Size([4, 1024, 1, 1])

#4 .t() 矩陣轉置操作, 只能使用於2D tensor
b=th.rand(2,3)
b.t().shape   #torch.Size([3, 2])

# transpose(需要轉置的維度)
b=th.rand(1,2,3,4)
#b.transpose(1,3).shape #torch.Size([1, 4, 3, 2])
#轉置之後的維度恢復要十分注意
a = b.transpose(1,3).contiguous().view(1, 4*3*2).view(1, 4, 3, 2).transpose(1, 3)
# view只能用在contiguous的variable上。如果在view之前用了transpose, permute等,需要用contiguous()來返回一個contiguous copy。
th.all(th.eq(a, b)) 

# permute 對原來的維度進行重新排序交換,會打亂記憶體,需要用contiguous()
b.permute(0,2,3,1).shape  #將原來的第2維度,放到1,...