参考:网易云课堂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]]))'''