torch学习第一天--tensor创建

标量表示

a = torch.tensor(1.2)

标量的shape和size都为空
torch学习第一天--tensor创建_第1张图片

向量表示

b= torch.tensor([1.0,2.])
c = torch.FloatTensor(3,3)
for i in [b,c]:
    print(i.shape,i.size(),i.dim(),i.numel())

torch学习第一天--tensor创建_第2张图片

从numpy中创建tensor

import numpy as np

a = np.array([1,2,3])
b = torch.from_numpy(a)
print(b)
print("====")
a = np.ones([2,3])
b = torch.from_numpy(a)
print(b)

torch学习第一天--tensor创建_第3张图片

从list创建tensor

a = torch.tensor([1,2,3.])
print(a)

torch学习第一天--tensor创建_第4张图片

未初始化方法

a = torch.empty(2,3)
print(a)
print("======")
a = torch.FloatTensor(2,3,4)
print(a)

torch学习第一天--tensor创建_第5张图片

随机初值

a = torch.rand(2,3) #rand_like
print(a)
a = torch.randint(3,10,(3,4)) #最小值,最大值,size
print(a)
a = torch.rand(3,4)
print(a)
a = torch.full((3,4),7)
print(a)
a = torch.full((),7)
print(a)
a = torch.arange(2,5)
print(a)
a = torch.ones(2,3) #zeros eye
print(a)
a = torch.randperm(10) #随机打散
print(a)

torch学习第一天--tensor创建_第6张图片

你可能感兴趣的:(pytorch,学习,numpy,python)