pytorch入門2 常用函式

2021-10-23 14:47:36 字數 3566 閱讀 3319

import numpy as np

import torch

#獲取張量資料型別

a = torch.randn(2, 3) #返回-1到1之間的隨機數2行3列

print(a)

b = a.shape #返回a形狀 touch.size([2,3])

print(b)

c = a.size() #返回的值和shape是一樣的

d = a.size(1) #放回size(【2,3】)中第二個元素 即3,a.shape(1)返回的值一樣

a = torch.rand(2, 2, 3) #建立乙個3維張量,可以理解為有2層,每層有2行3列的張量

print(a)

print(a.shape) #返回的是torch.size([2,2,3])

print(a[0]) #相等於取了第一層的資料,就是返回了第一層2行3列的張量

print(list(a.shape)) #更改a.shape為列表

print(a.numel()) #放回所佔空間大小 2*2*3=12

a = np.array([2, 3, 3]) #我們匯入的資料往往是numpy型別

torch.from_numpy(a) #對a進行裝換為tensor型別

print(torch.linspace(0,10, steps=11)) #等分0到10輸出,共11個數

#切片a = torch.rand(5, 3, 28, 28) #4維張量,以傳入的**為例,相當於5張**,每張**有3層通道,每層通道是28*28畫素大小

print(a[0,0].shape) #相當於放回了第一張的第一層通道資料 所以結果是torch.size([28,28])

print(a[0,0,2,4]) #相當於輸出了第一張的第一層通道,第3行第5列的畫素值大小

print(a[:2,1:,-1:,:].shape) #第乙個相當於從0到2就是取前2張,第二個是從1開始到最後,因為這裡總共就3個通道所以輸出2,

# 第三個-1表示反向索引,則從最後乙個開始往右,返回1,4是取全部,所以這裡輸出是28 最後輸出結果為torch.size[2,2,1,28]

#維度變換

a = torch.rand(4,1,28,28) #4維張量

print(a.view(4,28*28).shape) #將(1,28,28)/後面3維合併為1維 輸出結果為torch.size([4,784])

#增加維度

print(a.unsqueeze(0).shape) #在最前面增加乙個維度 輸出 torch.size([1,4,1,28,28])

#拷貝print(a.repeat(4, 4, 1, 1).shape) #相當於第乙個維度拷貝了4次,第二維度拷貝了4次,後面2個都是1次,結尾為(【16,4,28,28】)

#轉置.t()

a = torch.randn(3,4) #獲取3行4列隨機數

print(a.t().shape) #輸出4行3列資料,僅適用於2維

#交換維度

a = torch.rand(4,1,28,28)

print(a.transpose(0,3).shape) #將第1個和第4個維度進行交換,輸出結果為torch.size([28, 1, 28, 4])

print(a.permute(0,2,3,1).shape) #將第1放在第1位置,第2放第4個位置,第3放第2,第4放第3位置,結果torch.size([4, 28, 28, 1])

#合併a = torch.rand(4, 32, 8)

b = torch.rand(5, 32, 8)

print(torch.cat([a, b], dim = 0).shape) #表示第乙個維度進行合併,輸出torch.size([9, 32, 8])

a = torch.rand(32, 8)

b = torch.rand(32, 8)

c = torch.stack([a, b],dim=0)

print(c.shape) #相當於2個平面疊加後高度為2的三維 結果為torch.size([2, 32, 8])

#拆分aa, bb = c.split([1, 1], dim=0)

print(aa.shape, bb.shape) #在0維度拆分長度各位1,所以結果為torch.size([1, 32, 8]) torch.size([1, 32, 8])

a = torch.rand(4, 32, 8)

aa, bb= a.chunk(2, dim=0)

print(aa.shape, bb.shape) #拆分0維度,直接對半分,放回結果torch.size([2, 32, 8]) torch.size([2, 32, 8])

#數**算

torch.add(a, b) #加 或直接使用+

torch.sub(a, b) #減 或直接使用-

torch.mul(a, b) #乘 或直接使用*

torch.div(a, b) #除 或直接使用/

a = a.pow(2) #2次方 或直接使用**2

a = a.sqrt() #開方

#矩陣相乘 用matmul

a = torch.tensor([

[3, 3],

[3, 3]

])b = torch.tensor([

[1, 1],

[1, 1]

])print(torch.matmul(a,b)) #結果為tensor([[6, 6], [6, 6]])

#4維矩陣相乘 只取後2維進行計算

a = torch.rand(4, 3, 28, 64)

b = torch.rand(4, 3, 64, 32)

print(torch.matmul(a, b).shape) #輸出結果torch.size([4, 3, 28, 32])

#範數 norm-p

a = torch.tensor([

[3., 3.],

[3., 3.]

])b = torch.tensor([

[1., 1.],

[1., 1.]

])print(a.norm(1), b.norm(1)) #求的ab的1範數,各元素絕對值之和,返回tensor(12.) tensor(4.)

print(a.norm(2), b.norm(2)) #求的ab的2範數,各元素平方和之後開根號,返回tensor(6.) tensor(2.)

print(a.norm(1,dim=0)) #在a的0維度上求1範數, a的0維度大小(【2,2】),0維度上即為,[3,3]的1範數返回tensor([6., 6.])

a.min() #求最小值

a.mean() #求均值

a.sum() #求累加

a.max() #求最大值

a.prod() #求累乘

a.argmax() #返回回最大值的索引

a.argmin() #返回最小值的索引

PyTorch入門(三)PyTorch常用操作

def bilinear kernel in channels,out channels,kernel size return a bilinear kernel tensor tensor in channels,out channels,kernel size,kernel size 返回雙線性...

pytorch常用函式

torch.cat torch.squeeze torch.unsqueeze torch.stack torch.sum torch.sum input dim,out none tensor input tensor 輸入張量 dim int 縮減的維度 out tensor,optional ...

pytorch 常用函式

pytorch 常用函式 參考 網易雲課堂pytorch 學習 建立tensor import from numpy import numpy as np import torch a np.array 2,3.3 torch.from numpy a out tensor 2.0000,3.300...