学习记录 --- Pytorch 张量创建


个人学习总结,持续更新中……

参考文献

[1] pytorch自动求导Autograd系列教程(一)

张量创建

torch.tensor(array_like)方法

torch.tensor()仅仅是python函数,函数原型是:

tensor(data, dtype=None, device=None, requires_grad=False) -> Tensor
参数:
data: (array_like): tensor的初始值. 可以是列表,元组,numpy数组,标量等;
dtype: tensor元素的数据类型
device: 指定CPU或者是GPU设备,默认是None
requires_grad:是否可以求导,即求梯度,默认是False,即不可导的

torch.tensor会从data中的数据部分做拷贝(而不是直接引用),根据原始数据类型生成相应的torch.LongTensor、torch.FloatTensor和torch.DoubleTensor。

import torch

'''
def tensor(data: Any,
           dtype: Optional[_dtype] = None,
           device: Union[_device, str, None] = None,
           requires_grad: _bool = False) -> Tensor
'''
import torch

a = torch.tensor(data=[[1., 2.], [3., 4.]], dtype=torch.float, device='cpu', requires_grad=True)
print(a)
'''
tensor([[1., 2.],
        [3., 4.]], requires_grad=True)
'''
print(a.dtype)
# torch.float32
print(a.type())
# torch.FloatTensor
print(a.device)
# cpu
print(a.requires_grad)
# True

torch.Tensor()

torch.Tensor(n, m)方法

torch.Tensor()是python类,更明确地说,是默认张量类型torch.FloatTensor()的别名,torch.Tensor([1,2])会调用Tensor类的构造函数__init__,生成单精度浮点类型的张量。
其他类似的有:torch.DoubleTensor(n, m)、torch.LongTensor(n, m)等。

最基础的Tensor()函数创建方法,参数为Tensor的每一维大小。

import torch

# 最基础的Tensor()函数创建方法,参数为Tensor的每一维大小
a = torch.Tensor(2, 2)
print(a)
'''
tensor([[0., 0.],
        [0., 0.]])
'''
print(a.dtype)
# torch.float32
print(a.type())
# torch.FloatTensor
print(a.device)
# cpu
print(a.requires_grad)
# False

torch.Tensor(array_like)

import torch
# 使用Python的list序列进行创建
a = torch.Tensor([[1, 2], [3, 4]],device='cpu')
print(a)
'''
tensor([[1., 2.],
        [3., 4.]])
'''
print(a.dtype)
# torch.float32
print(a.type())
# torch.FloatTensor
print(a.device)
# cpu
print(a.requires_grad)
# False

张量类型

Torch defines 10 tensor types with CPU and GPU variants which are as follows:

Data typedtypeCPU tensorGPU tensor
32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor
64-bit floating pointtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating point 1torch.float16 or torch.halftorch.HalfTensortorch.cuda.HalfTensor
16-bit floating point 2torch.bfloat16torch.BFloat16Tensortorch.cuda.BFloat16Tensor
32-bit complextorch.complex32
64-bit complextorch.complex64
128-bit complextorch.complex128 or torch.cdouble
8-bit integer (unsigned)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed)torch.int16 or torch.shorttorch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed)torch.int32 or torch.inttorch.IntTensortorch.cuda.IntTensor
64-bit integer (signed)torch.int64 or torch.longtorch.LongTensortorch.cuda.LongTensor
Booleantorch.booltorch.BoolTensortorch.cuda.BoolTensor
quantized 8-bit integer (unsigned)torch.quint8torch.ByteTensor
quantized 8-bit integer (signed)torch.qint8torch.CharTensor
quantized 32-bit integer (signed)torch.qfint32torch.IntTensor
quantized 4-bit integer (unsigned) 3torch.quint4x2torch.ByteTensor
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值