Pytorch tensor(0): Tensor Introduction

1. Instances of the torch.Tensor class

Pytorch中tensor是torch.Tensor类的实例

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一致

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

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对象的四种方法:

  1. torch.Tensor(data)
  2. torch.tensor(data)
  3. torch.as_tensor(data)
  4. torch.from_numpy(data)
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()会随之改变

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

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.
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • tensorflow开发API 架构 Modules app module: Generic entry poin...
    sennchi阅读 1,478评论 0赞 2
  • Tensor的创建除了构造器,还提供了工厂模式的创建方式:函数,同时提供了基本运算函数的封装。本主题就专门数理这些...
    杨强AT南京阅读 2,363评论 0赞 1
  • ——和蔡梦慰《黑牢诗篇》 鹰已不见剩余的天空阴云密布闪电刻下的字又迅即消失 唐宋的风雅被击落持枪的人站在诗经的每一...
    荷东原创阅读 1,095评论 1赞 6
  • 74d842f33fbc阅读 178评论 0赞 0
  • 从前有一只兔子 他很喜欢吃白菜 后来白菜被猪拱了 兔子就饿死了
    徐湘楠阅读 492评论 2赞 1

友情链接更多精彩内容