Pytorch创建Tenser

从numpy导入

torch.from_numpy()

a = np.array([1,3.3])
print(torch.from_numpy(a))
'''
tensor([1.0000, 3.3000], dtype=torch.float64)
'''
b=np.ones([2,3])
print(torch.from_numpy(b))
'''
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
'''

从list导入

torch.tenser()
torch.FloatTenser()
使用小写的给现成的数据,使用大写的给shape
大写的也可以给数据,但是容易混淆

print(torch.tensor([2.,3.2]))  # 给现成数据
'''
tensor([2.0000, 3.2000])
'''
print(torch.tensor([[2.,3.2],[1.,22.369]]))  # 给现成数据
'''
tensor([[ 2.0000,  3.2000],
        [ 1.0000, 22.3690]])
'''
print(torch.FloatTensor([2.,3.2]))  # 给现成数据
'''
tensor([2.0000, 3.2000])
'''
print(torch.FloatTensor(2,3))  # 给shape
'''
tensor([[7.3157e+28, 1.8060e+28, 2.2855e+20],
        [3.2607e-12, 7.4086e+28, 7.0292e+28]])
'''

生成未初始化的数据

使用的时候,只作为容器,后续需要写进数据。因为这些生成的数很小或者很大,有可能造成问题。
torch.empty()
torch.Tenser()
torch.IntTenser()
torch.FloatTenser()

print(torch.empty(1))
'''
tensor([0.])
'''
print(torch.Tensor(2,3))
'''
tensor([[1.0653e-38, 1.0194e-38, 1.4013e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])
'''
print(torch.IntTensor(2,3))
'''
tensor([[7602297, 7274600,       1],
        [      0,       1,       0]], dtype=torch.int32)
'''
print(torch.FloatTensor(2,3))
'''
tensor([[4.8720e+13, 3.8536e-43, 4.8720e+13],
        [3.8536e-43, 2.0026e+13, 3.8536e-43]])
'''

默认Tesner类型

tensor.set_default_tensor_type()
强化学习一般用double,其他一般用float

print(torch.tensor([1.2,3]).type())
'''
torch.FloatTensor
'''
torch.set_default_tensor_type(torch.DoubleTensor) # 设置默认tensor类型
print(torch.tensor([1.2,3]).type())
'''
torch.DoubleTensor
'''

生成一定规则的tensor

随机初始化

torch.rand()
torch.rand_like()
torch.randint()

a = torch.rand(3,3)  # shape
print(a)
'''
tensor([[0.3886, 0.6188, 0.9964],
        [0.2606, 0.5931, 0.4254],
        [0.5090, 0.3213, 0.1154]])
'''
print(torch.rand_like(a))  # tensor
'''
tensor([[0.8519, 0.1525, 0.9367],
        [0.4119, 0.5464, 0.0481],
        [0.7441, 0.2370, 0.8354]])
'''
print(torch.randint(1,10,[3,3]))  # (min,max,shape)
'''
tensor([[8, 5, 2],
        [9, 6, 7],
        [4, 9, 3]])
'''

正态分布

a = torch.normal(mean=torch.full([10],0), std=torch.arange(1,0,-0.1))
'''
full:全部都是
均值为0
方差从1到0.1递减。每次减小0.1
'''
print(a)
'''
tensor([ 1.8971,  1.1551, -0.1725, -0.0967, -0.5709, -1.2672,  0.2139, -0.0383,
         0.4089, -0.0256])
'''

full

print(torch.full([2,3],7))# (shape,data)
'''
tensor([[7., 7., 7.],
        [7., 7., 7.]])
'''
print(torch.full([],7))
'''
tensor(7.)
'''
print(torch.full([1],7))
'''
tensor([7.])
'''

递增或者递减形成数列

print(torch.arange(0,10)) #(min, max)
'''
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
'''
print(torch.arange(0,10,2)) #(min, max, 间隔)
'''
tensor([0, 2, 4, 6, 8])
'''
print(torch.range(0, 10)) #(min, max)
'''
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
'''

等分

print(torch.linspace(0,10,steps=4)) # 0到10,分四个数
'''
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
'''
print(torch.linspace(0,-1,steps=10))
'''
tensor([ 0.0000, -0.1111, -0.2222, -0.3333, -0.4444, -0.5556, -0.6667, -0.7778,
        -0.8889, -1.0000])
'''

ones/zeros/eye

print(torch.ones(3,3))
'''
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
'''
print(torch.zeros(3,3))
'''
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
'''
print(torch.eye(3,4))
'''
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.]])
'''

乱序

print(torch.randperm(10)) #0-9打乱顺序
'''
tensor([8, 3, 2, 6, 5, 9, 0, 4, 7, 1])
'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值