【文档学习】PyTorch——torch包

29 篇文章 2 订阅
3 篇文章 0 订阅

本系列记录了博主学习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的操作。

参数:

  • precision —— 浮点输出的数字精度(默认4)
  • threshold —— Total number of array elements which trigger summarization rather than full repr (default = 1000).
  • edgeitems —— Number of array items in summary at beginning and end of each dimension (default = 3).
  • linewidth —— The number of characters per line for the purpose of inserting line breaks (default = 80). Thresholded matrices will ignore this parameter.
  • profile —— Sane defaults for pretty printing. Can override with any of the above options. (any one of default, short, full)

torch.set_flush_denormal(mode) → bool

源代码

使CPU上不正规的浮点数失效。

如果用户系统支持冲洗(flush)非正规数且成功编译冲洗非正规模式,返回True。set_flush_denormal()仅支持支持SSE3的x86架构。

参数: mode(bool)——控制是否生效冲洗非正规数模式。

例子:

>>> torch.set_flush_denormal(True)
True
>>> torch.tensor([1e-323], dtype=torch.float64)
tensor([ 0.], dtype=torch.float64)
>>> torch.set_flush_denormal(False)
True
>>> torch.tensor([1e-323], dtype=torch.float64)
tensor(9.88131e-324 *
       [ 1.0000], dtype=torch.float64)

运算

注意: Random sampling creation ops are listed under Random sampling and include: torch.rand() torch.rand_like() torch.randn() torch.randn_like() torch.randint() torch.randint_like() torch.randperm() You may also use torch.empty() with the In-place random sampling methods to create torch.Tensor s with values sampled from a broader range of distributions.

torch.tensor(data, dtype=None, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

torch.sparse_coo_tensor(indices, values, size=None, dtype=None, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

在这里插入图片描述

torch.as_tensor(data, dtype=None, device=None) → Tensor

源代码

在这里插入图片描述

在这里插入图片描述

torch.from_numpy(ndarray) → Tensor

源代码

在这里插入图片描述

torch.zeros(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

在这里插入图片描述

torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

torch.ones(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

在这里插入图片描述

torch.range(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

源代码

在这里插入图片描述

torch.squeeze(input, dim=None, out=None) → Tensor

源代码

返回一个张量,去除input中所有尺寸是1的维度。

例如,如果输入形如 ( A × 1 × B × C × 1 × D ) (A\times1\times B\times C\times 1\times D) (A×1×B×C×1×D),那么输出则形如 ( A × B × C × D ) (A\times B\times C\times D) (A×B×C×D)

dim给定时,squeeze操作只在给定的维度上进行。如果输入形如 ( A × 1 × B ) (A\times 1\times B) (A×1×B),那么squeeze(input, 0)将返回一个没有改变的张量,但是squeeze(input,1)将返回压缩过的张量,形如 ( A × B ) (A\times B) (A×B)

在这里插入图片描述
更多内容,欢迎加入星球讨论。
在这里插入图片描述

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值