pytorch tensor查找0_Pytorch tensor(0): Tensor Introduction

1. Instances of the torch.Tensor class

Pytorch中tensor是torch.Tensor类的实例

02418aa4fd9b

2. Tensor attributes

torch.Tensor 有以下属性:

torch.dtype:tensor数据格式

torch.device:CPU/GPU

torch.layout:tensor如何在内存中存储

Tensors contain data of a uniform type (dtype).

Tensor computations between tensors depend on the dtype and the device.

(1) tensor之间计算时需要dtype一致

02418aa4fd9b

(2) tensor之间计算时需要device一致

02418aa4fd9b

3. Creating tensors using data

These are the primary ways of creating tensor objects (instances of the torch.Tensor class), with data (array-like) in PyTorch

创建tensor对象的四种方法:

torch.Tensor(data)

torch.tensor(data)

torch.as_tensor(data)

torch.from_numpy(data)

02418aa4fd9b

image.png

这四种方法有什么区别

torch.Tensor() 大写T:constructor of the torch.Tensor class

torch.tensor() 小写t:factory function that constructs torch.Tensor objects and returns them to the caller

torch.as_tensor():factory function

torch.from_numpy():factory function

改变data数据值时,torch.Tensor()和torch.tensor()不会改变,torch.as_tensor()和torch.from_numpy()会随之改变

02418aa4fd9b

发生这种情况是因为torch.Tensor()和torch.tensor()复制了它们的输入数据,而torch.as_tensor()和torch.from_numpy()与原始输入对象共享了它们在内存中的输入数据。共享数据比复制数据更有效,并且使用更少的内存。

02418aa4fd9b

Share/Copt Data.png

torch.from_numpy()函数仅接受numpy.ndarrays,而torch.as_tensor()函数则接受包括其他PyTorch tensors在内的各种数组对象。

4. Best options for creating tensors in PyTorch

torch.tensor()和torch.as_tensor()是Pytorch中构建tensors的最好选择

torch.tensor() call is the sort of go-to call

torch.as_tensor() should be employed when tuning our code for performance.

Some things to keep in mind about memory sharing (it works where it can)

Since numpy.ndarray objects are allocated on the CPU, the as_tensor() function must copy the data from the CPU to the GPU when a GPU is being used.

由于numpy.ndarray对象是在CPU上分配的,因此在使用GPU时,as_tensor()函数必须将数据从CPU复制到GPU.

The memory sharing of as_tensor() doesn’t work with built-in Python data structures like lists.

as_tensor()的内存共享不适用于内置Python数据结构(如列表)

The as_tensor() call requires developer knowledge of the sharing feature. This is necessary so we don’t inadvertently make an unwanted change in the underlying data without realizing the change impacts multiple objects.

The as_tensor() performance improvement will be greater if there are a lot of back and forth operations between numpy.ndarray objects and tensor objects. However, if there is just a single load operation, there shouldn’t be much impact from a performance perspective.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值