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

torch.Tensor(data)

torch.tensor(data)

torch.as_tensor(data)

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.

pytorch tensor查找0_Pytorch tensor(0): Tensor Introduction相关推荐

  1. pytorch tensor查找0_Pytorch简单教程

    2019 年,ML 框架之争只剩两个实力玩家:PyTorch 和 TensorFlow.研究者大批涌向 PyTorch,而业界的首选仍然是 TensorFlow. Pytorch和TensorFlow ...

  2. pytorch tensor查找0_在PyTorch中Tensor的查找和筛选例子

    本文源码基于版本1.0,交互界面基于0.4.1 import torch 按照指定轴上的坐标进行过滤 index_select() 沿着某tensor的一个轴dim筛选若干个坐标 >>&g ...

  3. Pytorch基础(一) 初始tensor

    本文主要包括Pytorch中张量的基本操作 #!/usr/bin/env torch # -*- coding:utf-8 -*- # @Time : 2021/1/30, 12:07 # @Auth ...

  4. Tensor to img imge to tensor (pytorch的tensor转换)

    Tensor to img && imge to tensor 在pytorch中经常会遇到图像格式的转化,例如将PIL库读取出来的图片转化为Tensor,亦或者将Tensor转化为n ...

  5. 1、non-finite loss, ending training tensor(nan, device=‘cuda:0‘,2、‘LogSoftmaxBackward3、Function ‘MulB

    WARNING: non-finite loss, ending training tensor(nan, device='cuda:0', grad_ 错误1:WARNING: non-finite ...

  6. pytorch小知识点(三)-------Tensor的indices操作

    一.Tesnor 先简单介绍一下Tensor.Tensor是pytorch的核心,它是一个包含单一数据类型的多维矩阵. pyTorch定义了七种CPU tensor类型和八种GPU tensor类型: ...

  7. TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to

    问题描述: Traceback (most recent call last):File "D:\rotation-yolov5-master\detect.py", line 1 ...

  8. pytorch一行实现:计算同一tensor矩阵内每行之间的余弦相似度

    文章目录 0 输入数据 1 余弦相似度(Cosine Similarity) 2 torch.cosine_similarity 3 问题 4 分析与解决 4.1 答案 5 另外的实现方法 0 输入数 ...

  9. IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in

    使用python pytorch框架出现问题: IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or ...

最新文章

  1. Android实现手机手电筒
  2. 【转载】zookeeper学习
  3. 从GitHub中整理出来的15个最受欢迎的Python开源框架,你喜欢哪个
  4. 【渝粤题库】陕西师范大学800004 遥感概论
  5. linux内核启动以及文件系统的加载过程
  6. Laravel 在Centos上的安装
  7. [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)
  8. html按照字数分页,纯javascript实现分页(两种方法)
  9. powershell excel 导入 sqlserver
  10. linux svn切换分支,玩转SVN-分支
  11. 煤矿AI智能视频分析识别系统解决方案
  12. 使用selenium + pytest + allure做WBE UI自动化
  13. Matlab实现Hermite插值多项式
  14. oracle exclude table,EXPDP/IMPDP:关于EXCLUDE参数
  15. 十大游戏开发引擎优缺点对比
  16. 企业如何进行客户细分 客户细分的方法和类型
  17. javascript 基本用法
  18. Maven项目插件飘红解决方案
  19. 企业CDN缓存加速原理
  20. 20145212罗天晨 注入shellcode实验及Retuen-to-libc实验

热门文章

  1. WPF 禁止在文本框中输入数字
  2. SVG与CSS的特殊性
  3. 安装VS2008错误解决
  4. html的动画效果实现无限轮播,利用 CSS3 实现的无缝轮播功能代码
  5. android 本地 播放器,Android 本地播放器
  6. c语言求字符串转换成双精度_C语言实现把字符串中的数字转换成整数
  7. python非法变量名_数据库错误:ORA-01036:非法变量名/numb
  8. python数据类型可变和不可变_python可变数据类型vs不可变数据类型
  9. syslog工具_07 Docker 可视化管理和监控工具
  10. js几个页面生成pdf 然后批量打印_太好用了!这款免费PDF工具能够满足你的各种需求...