pytorch 常用函式

2021-10-03 10:47:13 字數 4691 閱讀 7752

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.3000], dtype=torch.float64)

a=np.ones([2,3])

torch.from_numpy(a)

'''tensor([[1., 1., 1.],

[1., 1., 1.]], dtype=torch.float64)'''

#import from list

torch.tensor([2,3.0])#out:tensor([2., 3.]) tensor 載入的資料

torch.floattensor([2,3.2])# tensor([2.0000, 3.2000])

torch.tensor([[2.,3.2],[1.0,22.3]]) #tensor([[ 2.0000, 3.2000],

#[ 1.0000, 22.3000]])

#uninitialized

#1、torch.empty()

#2、torch.floattensor(d1, d2, d3)

#3、 torch.inttensr(d1, d2, d3)

torch.empty(1)#輸出為1*1 未初始化的矩陣 out:tensor([0.])

torch.tensor(2,3)

'''tensor([[0., 0., 0.],

[0., 0., 0.]])'''

torch.inttensor(2,3)

'''tensor([[0, 0, 0],

[0, 0, 0]], dtype=torch.int32)'''

torch.floattensor(2,3)

'''tensor([[0., 0., 0.],

[0., 0., 0.]])'''

#set default type

torch.tensor([1.2,3]).type() # pytorch 預設tensor 'torch.floattensor'

torch.set_default_tensor_type(torch.doubletensor)

torch.tensor([1.2,3]).type()# type()是方法 'torch.doubletensor'

#rand /rand_like,randint

torch.rand(3,3)

'''tensor([[0.9379, 0.9233, 0.1059],

[0.9286, 0.3901, 0.3092],

[0.5107, 0.7357, 0.7802]])'''

a=torch.rand(3,3)

torch.rand_like(a)

torch.randint(1,10,[2,3]) #min,max,size

# tensor([[8, 5, 8],

# [1, 9, 6]])

#正態分佈

#n(0, 1)

#n(u, std)

torch.randn(3,3)#正態分佈

'''tensor([[8, 5, 8],

[1, 9, 6]])'''

torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))

'''tensor([-0.6110, -0.6861, -0.8501, 0.4197, -0.2862, 0.0783, -0.4763, 0.2206,

-0.1437, -0.0090]) 均值,方差 torch.arange 左閉右開區間'''

torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))

'''tensor([ 1.6395, -1.2369, 0.9526, -0.1463, 0.1491, -0.1538, 0.3134, -0.2822,

-0.0860, -0.0185])'''

#full

torch.full([2,3],7)

'''tensor([[7., 7., 7.],

[7., 7., 7.]]) 2維'''

torch.full(,7) #scalar標量 tensor(7.)

torch.full([1],7)# out:tensor([7.])

#arange /range

torch.arange(0,10)# tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

torch.arange(0,10,2) #tensor([0, 2, 4, 6, 8]) 左閉右開 步長

torch.range(0,10) # 左閉右開區間tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])

#linspace/logspace

torch.linspace(0,10,steps=4)#左閉右開數量tensor([ 0.0000, 3.3333, 6.6667, 10.0000])

torch.linspace(0,10,steps=10) #tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,8.8889, 10.0000])

torch.logspace(0,-1,steps=10)#logspace以 2,10,e 為底的指數tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,0.1000])

torch.logspace(0,1,steps=10) #tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948,7.7426, 10.0000])

#ones/zeros/eye

torch.ones(3,3) #3*3的全一矩陣

'''tensor([[1., 1., 1.],

[1., 1., 1.],

[1., 1., 1.]])'''

torch.zeros(3,3)#3*3 的全0矩陣

'''tensor([[0., 0., 0.],

[0., 0., 0.],

[0., 0., 0.]])'''

torch.eye(3,4) #單位矩陣

'''tensor([[1., 0., 0., 0.],

[0., 1., 0., 0.],

[0., 0., 1., 0.]])'''

torch.eye(3)

'''tensor([[1., 0., 0.],

[0., 1., 0.],

[0., 0., 1.]])'''

a=torch.zeros(3,3)

torch.ones_like(a)

'''tensor([[1., 1., 1.],

[1., 1., 1.],

[1., 1., 1.]])'''

#randperm random.shuffle 對索引隨機打散

a=torch.rand(2,3) #類似第一維為人名 a 人名 數學成績 ,b 人名 語文成績 兩個矩陣的行的數目一樣 隨機種子數目為行的數目 [0,1]為原始順序,[1,0]為反轉順序

b=torch.rand(2,2)

idx=torch.randperm(2)

idx1=idx#tensor([0, 1])

print(idx1)

idx2=idx1#tensor([1, 0])

print(idx2)

a #'''tensor([[0.6799, 0.1168, 0.6706], [0.2596, 0.6144, 0.6370]])'''

print(a[[0,1]]) #tensor([[0.1919, 0.0593, 0.7492],

#[0.7986, 0.2391, 0.8857]])

print(a[[1,0]])

print(b[idx]) #tensor([[0.2905, 0.9161],

#[0.6948, 0.3815]])

a,b'''

tensor([1, 0])

tensor([1, 0])

tensor([[0.4416, 0.2493, 0.6051],

[0.2301, 0.5072, 0.3799]])

tensor([[0.2301, 0.5072, 0.3799],

[0.4416, 0.2493, 0.6051]])

tensor([[0.4023, 0.0007],

[0.3239, 0.4355]])

(tensor([[0.4416, 0.2493, 0.6051],

[0.2301, 0.5072, 0.3799]]), tensor([[0.3239, 0.4355],

[0.4023, 0.0007]]))'''

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入門2 常用函式

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...

Pytorch常用函式功能使用(一)

import torch number 1 torch.randn 2,3 print number 1 print number 1.shape print number 1.view 1,1 print number 1.view 3,1 輸出 tensor 1.0506,0.5875,1.24...