pytorch学习之旅——1

pytorch学习经历之torch.tensor与torch.Tensor

  • torch.Tensor与torch.tensor

torch.Tensor与torch.tensor

torch.Tensor是torch.tensor与torch.empty的一种混合。
当传入数据时,torch.Tensor使用全局默认的dtype——FloatTensor,而torch.tensor从数据中推断数据类型。

import torch
t1=torch.Tensor(2,3)
t2=torch.tensor([[1,2,3],[4,5,6]])
t3=torch.tensor([[1.,2.,3.],[1.,2.,3.]])
print('the value of t1:{},the dtype of t1:{}'.format(t1,t1.type()))
print('the value of t2:{},the dtype of t2:{}'.format(t2,t2.type()))
print('the value of t3:{},the dtype of t3:{}'.format(t3,t3.type()))
the value of t1:tensor([[0., 0., 0.],
        [0., 0., 0.]]),the dtype of t1:torch.FloatTensor
the value of t2:tensor([[1, 2, 3],
        [4, 5, 6]]),the dtype of t2:torch.LongTensor
the value of t3:tensor([[1., 2., 3.],
        [1., 2., 3.]]),the dtype of t3:torch.FloatTensor

此外,torch.tensor(1)返回固定值1,而torch.Tensor(1)返回大小为1的张量,并随机初始化值。

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