import numpy as np
import torch
1.轉(zhuǎn)換
data = np.array([[1,2,3],[4,5,6],[7,8,9]])
array2tensor = torch.from_numpy(data)
tensor2array = array2tensor.numpy()
"""
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.int32)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
"""
2.隨機(jī)數(shù)
Numpy
種子np.random.seed(seed)
· numpy.random.rand(d0, d1, ..., dn)
產(chǎn)生均勻分布在[0,1)間的隨機(jī)數(shù)蓝翰,括號里指定size漂坏,默認(rèn)float
a = np.random.rand(3, 3)
"""
array([[0.15334759, 0.92629601, 0.66620659],
[0.96422599, 0.71548214, 0.66290283],
[0.2524382 , 0.24994874, 0.56431885]])
"""
· numpy.random.randn(d0, d1, ..., dn)
產(chǎn)生標(biāo)準(zhǔn)正態(tài)分布(均值為1驯绎,方差為0)隨機(jī)數(shù)册着,默認(rèn)float
a = np.random.randn(3, 3)
"""
array([[ 1.11674106, -0.29341747, 1.3589762 ],
[ 0.53113603, 0.27664724, 1.00542435],
[ 0.7966614 , -0.87245422, -0.63765919]])
"""
· numpy.random.randint(low, high, size, dtype)
產(chǎn)生隨機(jī)整數(shù)
a = np.random.randint(1 , 10 , (3 , 3))
"""
array([[1, 5, 7],
[3, 1, 3],
[5, 7, 9]])
"""
· numpy.random.random(size)
產(chǎn)生[0, 1)之間的浮點(diǎn)數(shù)
a = np.random.random((3,3))
"""
array([[0.06250577, 0.34364639, 0.38829973],
[0.84117707, 0.36396527, 0.27810945],
[0.40451704, 0.79816697, 0.53218176]])
"""
· numpy.random.uniform(low, high, size)
產(chǎn)生浮點(diǎn)數(shù)兄纺,浮點(diǎn)數(shù)在高低閾值內(nèi)均勻分布
a = np.random.uniform(1 , 10 , (3 , 3))
"""
array([[3.78630803, 4.0347892 , 8.59961933],
[1.39894366, 4.79155566, 8.80261069],
[2.93894629, 7.76194383, 1.66707995]])
"""
Pytorch
種子torch.manual_seed (int or long)
· torch.normal(mean, std, out=None) → Tensor
· torch.normal(mean=0.0, std, out=None) → Tensor
· torch.normal(mean, std=1.0, out=None) → Tensor
返回一個張量,包含從給定參數(shù)means,std的離散正態(tài)分布中抽取隨機(jī)數(shù)
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
#均值和標(biāo)準(zhǔn)差的形狀不需要匹配,但是每個張量中元素的總數(shù)需要相同
torch.normal(mean=0.5, std=torch.arange(1., 6.)) #共享均值裸弦,默認(rèn)為0
torch.normal(mean=torch.arange(1., 6.)) #共享標(biāo)準(zhǔn)差泞坦,默認(rèn)為1
"""
tensor([0.0453, 2.2179, 3.6245, 4.8631, 5.3302, 6.0755, 6.7167, 7.8588, 9.1602,
9.6817])
tensor([ 0.5778, 2.7397, 0.1901, -4.2744, 2.4538])
tensor([1.2878, 1.3632, 2.1901, 3.8385, 6.1036])
"""
· torch.randn(size) → Tensor
返回一個標(biāo)準(zhǔn)正態(tài)分布(均值為0授嘀、方差為1)隨機(jī)數(shù)組成的張量
torch.randn(3, 3)
"""
tensor([[-0.4921, -0.9048, 1.0979],
[-0.4808, 0.7250, -0.0609],
[-1.7212, -0.7908, -0.2270]])
"""
· torch.randint(low=0, high, size) → Tensor
返回一個在高低閾值間均勻分布的隨機(jī)整數(shù)張量
torch.randint(1,10,(3,3))
"""
tensor([[9, 1, 8],
[1, 2, 4],
[8, 4, 7]])
"""
· torch.rand(size) → Tensor
產(chǎn)生均勻分布在[0,1)間的隨機(jī)張量
torch.rand((3,3))
"""
tensor([[0.7242, 0.2358, 0.8065],
[0.4882, 0.1418, 0.6659],
[0.0871, 0.8819, 0.5817]])
"""
3.方法
numpy和pytorch中許多函數(shù)使用方法一致巷折,且很多方法可以直接通過實例的下標(biāo)調(diào)用上鞠,代碼中使用庫函數(shù)調(diào)用方式
tensor = torch.FloatTensor([[-1,-2],[1,2]])
array = np.array([[-1.,-2.],[1.,2.]])
torch.sin(tensor)
np.sin(array)
torch.abs(tensor)
np.abs(array)
torch.max(tensor)
np.max(array)
...
"""
tensor([[-0.8415, -0.9093],
[ 0.8415, 0.9093]])
array([[-0.84147098, -0.90929743],
[ 0.84147098, 0.90929743]])
tensor([[1., 2.],
[1., 2.]])
array([[1., 2.],
[1., 2.]])
tensor(2.)
2.0
"""
4.計算
#矩陣乘
torch.mm(tensor,tensor)
np.matmul(array,array)
torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1])) #Expected 1-D argument self
#數(shù)組乘
torch.mul(tensor,tensor)
np.multiply(array,array)
"""
tensor([[-1., -2.],
[ 1., 2.]])
array([[-1., -2.],
[ 1., 2.]])
tensor(7)
tensor([[1., 4.],
[1., 4.]])
array([[1., 4.],
[1., 4.]])
"""
5.GPU
pytorch張量具有GPU支持岭佳,通過.cuda()
方法移至GPU加速運(yùn)算,傳統(tǒng)的numpy庫不支持GPU加速,因此在使用CUDA加速時,無法使用numpy轉(zhuǎn)化以及numpy庫的相關(guān)方法仅财,但可以通過.cpu()
將目標(biāo)張量移至CPU宁改,再進(jìn)行numpy轉(zhuǎn)換潭兽。我在搭建網(wǎng)絡(luò)時就曾遇到TypeError:can't convert CUDA tensor to numpy.