import numpy as np
import torch
1.转换
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.随机数
Numpy
种子np.random.seed(seed)
· numpy.random.rand(d0, d1, ..., dn)
产生均匀分布在[0,1)间的随机数,括号里指定size,默认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)
产生标准正态分布(均值为1,方差为0)随机数,默认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)
产生随机整数
a = np.random.randint(1 , 10 , (3 , 3))
"""
array([[1, 5, 7],
[3, 1, 3],
[5, 7, 9]])
"""
· numpy.random.random(size)
产生[0, 1)之间的浮点数
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)
产生浮点数,浮点数在高低阈值内均匀分布
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
返回一个张量,包含从给定参数means,std的离散正态分布中抽取随机数
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
#均值和标准差的形状不需要匹配,但是每个张量中元素的总数需要相同
torch.normal(mean=0.5, std=torch.arange(1., 6.)) #共享均值,默认为0
torch.normal(mean=torch.arange(1., 6.)) #共享标准差,默认为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
返回一个标准正态分布(均值为0、方差为1)随机数组成的张量
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
返回一个在高低阈值间均匀分布的随机整数张量
torch.randint(1,10,(3,3))
"""
tensor([[9, 1, 8],
[1, 2, 4],
[8, 4, 7]])
"""
· torch.rand(size) → Tensor
产生均匀分布在[0,1)间的随机张量
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中许多函数使用方法一致,且很多方法可以直接通过实例的下标调用,代码中使用库函数调用方式
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
#数组乘
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加速运算,传统的numpy库不支持GPU加速,因此在使用CUDA加速时,无法使用numpy转化以及numpy库的相关方法,但可以通过.cpu()
将目标张量移至CPU,再进行numpy转换。我在搭建网络时就曾遇到TypeError:can't convert CUDA tensor to numpy.