【基础】-tensor(张量)

一.创建一个tensor

1.只分配空间

x1 = torch.Tensor(5, 3)          # 数据是随机的,只是分配了可见,没有初始化,所及数据是对应空间里的数据
print(x1)

输出:

tensor([[7.3470e-39, 8.9082e-39, 8.9082e-39],[1.0194e-38, 9.1837e-39, 4.6837e-39],[9.2755e-39, 1.0837e-38, 8.4490e-39],[9.9184e-39, 9.9184e-39, 9.0000e-39],[1.0561e-38, 1.0653e-38, 4.1327e-39]])

2.0-1分布

x2 = torch.rand(5, 3)            # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)

输出:

tensor([[0.0786, 0.0817, 0.5277],[0.3758, 0.9402, 0.2716],[0.0723, 0.3258, 0.7880],[0.9141, 0.7395, 0.1126],[0.9843, 0.5128, 0.9107]])

3.直接创建

x3 = torch.tensor([ [1,2,3,4],[5,6,7,8]])
print(x3)

输出:

tensor([[1, 2, 3, 4],[5, 6, 7, 8]])

4.同一个数值

x4 = torch.full((5, 3), 6)
print(x4)

输出:

tensor([[6., 6., 6.],[6., 6., 6.],[6., 6., 6.],[6., 6., 6.],[6., 6., 6.]])

5.在一个区间内按一定步长

x5 = torch.arange(0, 100, 10)     # 从0到99,按步长为10进行生成
print(x5)

输出:

tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

或者:
在一定区间内均匀生成多少份,步长自动计算

x6 = torch.linspace(0, 100, 10)   # 在0到100均匀生成10份
print(x6)

输出:

tensor([  0.0000,  11.1111,  22.2222,  33.3333,  44.4444,  55.5556,  66.6667,77.7778,  88.8889, 100.0000])

这里可见看出来使用torch.linspace包含end,0到100分成10个数,相对于在0到100的线段上均匀定9个点,100/9 = 11.111111······,所以,步长为11.11111.
如果生成11份,步长就为10了。
输出:

tensor([  0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.])

6.特殊矩阵

# 特殊矩阵
x7 = torch.zeros(5, 3)          # 全0矩阵
print(x7)x8 = torch.ones(5, 3)           # 全1矩阵
print(x8)x9 = torch.eye(3, 3)            # 单位阵
print(x9)x10 = torch.eye(5, 3)           # 当输入shape不是方阵时,一部分是单位阵,多出来的部分为0
print(x10)

输出:

tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]])tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.],[0., 0., 0.],[0., 0., 0.]])

7.生成一个与已知tensor的shape相同的tensor

x11 = torch.rand_like(x10)
print(x11)

输出:

tensor([[0.2761, 0.7960, 0.0800],[0.3035, 0.3525, 0.5574],[0.3570, 0.5426, 0.4790],[0.3504, 0.3996, 0.1984],[0.5939, 0.3260, 0.6721]])

8.从numpy转

x12 = np.random.rand(5, 3)
print(x12)
x12 = torch.from_numpy(x12)
print(x12)

输出:

[[0.14858865 0.18512316 0.97759539][0.96559993 0.75191884 0.1561388 ][0.71575248 0.88542421 0.29086326][0.67362585 0.00512253 0.34022816][0.69759491 0.25110932 0.71962754]]
tensor([[0.1486, 0.1851, 0.9776],[0.9656, 0.7519, 0.1561],[0.7158, 0.8854, 0.2909],[0.6736, 0.0051, 0.3402],[0.6976, 0.2511, 0.7196]], dtype=torch.float64)

二.tensor的属性

x2 = torch.rand(5, 3)            # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)
print(x2.size())   # (列,行)
print(x2.shape)    # (列, 行)
print(x2.dtype)    # 数据类型

输出:

tensor([[0.6725, 0.6270, 0.0352],[0.0420, 0.4865, 0.7263],[0.9950, 0.3957, 0.3868],[0.3802, 0.3337, 0.0465],[0.0089, 0.7211, 0.1279]])
torch.Size([5, 3])
torch.Size([5, 3])
torch.float32

三.tensor的操作

1.加

import torchx1 = torch.randint(0, 10, (5, 3), dtype=torch.float)
print(x1)x2 = torch.eye(5, 3)
print(x2)# 1
print(x1 + x2)
# 2
print(torch.add(x1, x2))
# 3
print(x1.add(x2))
# 4
sum = torch.Tensor(5, 3)     # 预先分配空间
torch.add(x1, x2, out=sum)       # 加的结果保存在sum里
print(sum)

输出:

tensor([[5., 5., 8.],[9., 3., 8.],[9., 8., 3.],[5., 5., 6.],[6., 9., 5.]])tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.],[0., 0., 0.],[0., 0., 0.]])tensor([[6., 5., 8.],[9., 4., 8.],[9., 8., 4.],[5., 5., 6.],[6., 9., 5.]])tensor([[6., 5., 8.],[9., 4., 8.],[9., 8., 4.],[5., 5., 6.],[6., 9., 5.]])tensor([[6., 5., 8.],[9., 4., 8.],[9., 8., 4.],[5., 5., 6.],[6., 9., 5.]])tensor([[6., 5., 8.],[9., 4., 8.],[9., 8., 4.],[5., 5., 6.],[6., 9., 5.]])
tensor1.add()和tensor1.add_()的区别

这个算是和python的特性一致,不加下划线的第一种就是普通的加法,不会改变tensor1的内容。而加了下划线,即第二种,加的结果会赋值给tensor1。
例子:

print(x1.add(x2))
print(x1)
print(x1.add_(x2))
print(x1)

输出:

tensor([[5., 8., 9.],[2., 4., 3.],[6., 2., 9.],[3., 1., 3.],[8., 5., 9.]])tensor([[4., 8., 9.],[2., 3., 3.],[6., 2., 8.],[3., 1., 3.],[8., 5., 9.]])tensor([[5., 8., 9.],[2., 4., 3.],[6., 2., 9.],[3., 1., 3.],[8., 5., 9.]])tensor([[5., 8., 9.],[2., 4., 3.],[6., 2., 9.],[3., 1., 3.],[8., 5., 9.]])

其余运算

其他的减法,乘法,除法均可使用:-,*,/。同时也可以使用

  • torch.sub() 减法
  • torch.mul() 乘法,只是简单的对应位置相乘,不是矩阵的乘法
  • torch.div() 除法,对应位置相除

或者

  • tensor1.sub(tensor2)
  • tensor1.mul(tensor2)
  • tensor.div(tensor2)
    上面三种加也可以加下划线,和add的例子相同。

其中,如果被除数为0,会出现这样的结果:

tensor([[9., inf, inf],[inf, 6., inf],[inf, inf, 0.],[nan, inf, inf],[inf, inf, inf]])

上面的三种运算中torch.sub()这种也都可以添加out=.

此外,还有很多其他的运算,基本都比较简单。

可能的一些错误

RuntimeError: expected backend CPU and dtype Float but got backend CPU and dtype Long

这种情况,根据提示信息就是数据的类型不对。
一般可以在创建tensor的时候添加dtype=torch.float类似的语句来改。也可以使用tensor1.float()来修改

查看tensor的数据类型时:使用tensor1.dtype

【基础篇】-tensor(张量)相关推荐

  1. TensorFlow2.4 开发 基础篇① 张量和变量 (1)

         TensorFlow2.4 开发 基础篇① 张量和变量(Tensor) 文章会不断更新,喜欢的小伙伴可以帮博主点个赞哟("'▽'") 文章目录 前言 1. 变量 1.1 ...

  2. 06_2_Pytorch的基础数据类型、CPU tensor类型和GPU tensor类型、判断数据类型、CPU或GPU张量之间的转换、数据类型转换、1-4维向量、Tensor张量、Variable等

    1.6.2.Pytorch的基础数据类型 1.6.2.1.Torch定义了的七种CPU tensor类型和八种GPU tensor类型 1.6.2.2.基础数据类型 1.6.2.3.Pytorch数据 ...

  3. 【系统认识张量(一)】基础篇:什么是张量?

    [系统认识张量(一)]基础篇:什么是张量? 文章目录 [系统认识张量(一)]基础篇:什么是张量? 前言 一.基础概念 1. 纤维.切片 2. 矩阵化 3. 秩一张量 二.张量的基础运算 1.张量的N模 ...

  4. 7.3 TensorFlow笔记(基础篇):加载数据之从队列中读取

    前言 整体步骤 在TensorFlow中进行模型训练时,在官网给出的三种读取方式,中最好的文件读取方式就是将利用队列进行文件读取,而且步骤有两步: 1. 把样本数据写入TFRecords二进制文件 2 ...

  5. 计算机视觉面试宝典--深度学习机器学习基础篇(四)

    计算机视觉面试宝典–深度学习机器学习基础篇(四) 本篇主要包含SVM支持向量机.K-Means均值以及机器学习相关常考内容等相关面试经验. SVM-支持向量机 支持向量机(support vector ...

  6. 深度学习基础篇(一)

    深度学习基础篇(一) Part I 深度学习基础 本书1-4章将带你了解一些基本概念:什么是深度学习,它可以用来做什么以及它如何工作.此外,熟悉使用深度学习解决数据问题的典型工作流程.如果还没怎么了解 ...

  7. pytorch学习笔记(一):Tensor(张量)

    文章目录 前言 1 创建Tensor 2 操作 2.1 算术操作 2.2 索引 2.3 改变形状 2.4 线性代数 3 广播机制 4 运算的内存开销 5 Tensor和NumPy相互转换 5.1 Te ...

  8. TensorFlow基础篇(六)——tf.nn.max_pool()和tf.nn.avg_pool()

    tf.nn.max_pool()和tf.nn.avg_pool()是TensorFlow中实现最大池化和平均池化的函数,在卷积神经网络中比较核心的方法. 有些和卷积很相似,可以参考TensorFlow ...

  9. Google机器学习速成课程 - 视频笔记整理汇总 - 基础篇核心部分

    Google机器学习速成课程 - 视频笔记整理 - 基础篇核心部分 课程网址: https://developers.google.com/machine-learning/crash-course/ ...

最新文章

  1. android ble5.0添加扫描过滤,bluetooth-lowenergy
  2. 大数据分析常用去重算法分析『HyperLogLog 篇』
  3. 【CyberSecurityLearning 42】日志记录规则
  4. 【已解决】Class not found: “com.bjpowernode.MyTest“
  5. CentOs7.2编译安装Nginx服务器
  6. 2种图像增强方法:图像点运算和图像灰度化处理
  7. (108)FPGA面试题-介绍STA静态时序分析及其作用
  8. 分享一个蛋疼的俄罗斯方块小游戏
  9. MainStoryboard.storyboard could not be opened
  10. f5 会话保持 负载均衡_f5会话保持的.doc
  11. deepl pro 2.0.0专业版
  12. 【知易行难】RS485组网连接示意图
  13. 【数据库认证】OCM准备及考试经验总结
  14. python 推箱子实验开发报告_推箱子游戏详细设计报告
  15. 从零搭建微信机器人(三):定时触发任务
  16. 智能车图像处理-阳光算法
  17. 养蛙火爆,大数据解读《旅行青蛙》崛起之谜
  18. mmap和mmap64
  19. 我以为我对数据库索引十分了解,直到我遇到了阿里面试官。
  20. 大一大学计算机期末试卷,大一大学计算机基础教程期末考试题

热门文章

  1. 好看的首页侧边多层导航
  2. 关于Linux群组问题
  3. php程序员学什么语言好就业_十几岁的女生学什么专业就业前景好?
  4. C语言怎么输入float的变量,c语言float怎么用
  5. easyexcel 实现表头批注
  6. 如何隐藏您在Apple App Store和Google Play上购买的应用程序
  7. 金蝶k3 cloud 7.x 学习 授权
  8. java keydown_Java SWT.KeyDown方法代码示例
  9. Odoo看板视图实践案例
  10. 你需要知道的 TCP 四次挥手