【文档学习】PyTorch——torch包

本系列记录了博主学习PyTorch过程中的笔记。本文记录的是torch包的相关内容,官方网址。只记录了一部分博主用到的,持续更新。更新于2019.03.21。

torch包包括了用于多维张量的数据结构,并定义了对于它们的数学计算。此外,这个包也提供了多种有效的张量及任意类型的并行化用法,以及其他有用的方法。

这个包对应于CUDA,允许用户在NVIDIA GPU(运算能力>=3.0)上运行张量计算。

张量(Tensors)

torch.is_tensor(obj)

源代码

如果obj是一个PyTorch张量,返回True。

参数: objobject)——用于测试的目标

torch.is_storage(obj)

源代码

如果obj是一个存储对象(storage object)则返回True。

参数: objobject)——用于测试的目标

torch.set_defaul_dtype(d)

源代码

将默认的浮点数据类型(gloating point type)设成d。这个类型将作为默认浮点数据类型用于在torch.tensor()内的类型推断。

初始默认浮点数据类型是torch.float32

参数: dtorch.dtype)——将被作为默认的浮点数据类型

例子:

>>> torch.tensor([1.2, 3]).dtype           # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.tensor([1.2, 3]).dtype           # a new floating point tensor
torch.float64

torch.get_default_type() → torch.dtype

源代码

获取当前默认的浮点torch.dtype

例子:

>>> torch.get_default_dtype()  # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.get_default_dtype()  # default is now changed to torch.float64
torch.float64
>>> torch.set_default_tensor_type(torch.FloatTensor)  # setting tensor type also affects this
>>> torch.get_default_dtype()  # changed to torch.float32, the dtype for torch.FloatTensor
torch.float32

torch.set_default_tensor_type(t)

源代码

将默认的torch.Tensor类型设成浮点张量类型t。这个类型将同样被用于torch.tensor()浮点数类型的类型推断。

初始默认浮点张量类型是torch.FloatTensor

参数: t(type或string)——浮点张量类型或其名字

例子:

>>> torch.tensor([1.2, 3]).dtype    # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_tensor_type(torch.DoubleTensor)
>>> torch.tensor([1.2, 3]).dtype    # a new floating point tensor
torch.float64

torch.numel(input) → int

源代码

返回一个输入张量中的所有元素的个数。

参数: 输入(Tensor)——输入的张量

例子:

>>> a = torch.randn(1, 2, 3, 4, 5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)

源代码

从NumPy中取来的用于显示items的操作。

参数:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值