pytorch函数测试

  • 1、函数测试
    • 1.1、测试 .sum(dim=(m,n))
    • 1.2、测试.sum(dim=-1)

主要针对不知道函数结果的操作进行测试

1、函数测试

1.1、测试 .sum(dim=(m,n))

f = torch.arange(4 * 5 * 6).view(1,1, 4, 5, 6)
f.shape
Out[19]: torch.Size([1, 1, 4, 5, 6])
f
Out[20]:
tensor([[[[[  0,   1,   2,   3,   4,   5],[  6,   7,   8,   9,  10,  11],[ 12,  13,  14,  15,  16,  17],[ 18,  19,  20,  21,  22,  23],[ 24,  25,  26,  27,  28,  29]],[[ 30,  31,  32,  33,  34,  35],[ 36,  37,  38,  39,  40,  41],[ 42,  43,  44,  45,  46,  47],[ 48,  49,  50,  51,  52,  53],[ 54,  55,  56,  57,  58,  59]],[[ 60,  61,  62,  63,  64,  65],[ 66,  67,  68,  69,  70,  71],[ 72,  73,  74,  75,  76,  77],[ 78,  79,  80,  81,  82,  83],[ 84,  85,  86,  87,  88,  89]],[[ 90,  91,  92,  93,  94,  95],[ 96,  97,  98,  99, 100, 101],[102, 103, 104, 105, 106, 107],[108, 109, 110, 111, 112, 113],[114, 115, 116, 117, 118, 119]]]]])
g = torch.arange(6).view(1,1, 1, 1, 6)
g.shape
Out[22]: torch.Size([1, 1, 1, 1, 6])
g
Out[23]: tensor([[[[[0, 1, 2, 3, 4, 5]]]]])
k = f*g
k.shape
Out[25]: torch.Size([1, 1, 4, 5, 6])
k
Out[26]:
tensor([[[[[  0,   1,   4,   9,  16,  25],[  0,   7,  16,  27,  40,  55],[  0,  13,  28,  45,  64,  85],[  0,  19,  40,  63,  88, 115],[  0,  25,  52,  81, 112, 145]],[[  0,  31,  64,  99, 136, 175],[  0,  37,  76, 117, 160, 205],[  0,  43,  88, 135, 184, 235],[  0,  49, 100, 153, 208, 265],[  0,  55, 112, 171, 232, 295]],[[  0,  61, 124, 189, 256, 325],[  0,  67, 136, 207, 280, 355],[  0,  73, 148, 225, 304, 385],[  0,  79, 160, 243, 328, 415],[  0,  85, 172, 261, 352, 445]],[[  0,  91, 184, 279, 376, 475],[  0,  97, 196, 297, 400, 505],[  0, 103, 208, 315, 424, 535],[  0, 109, 220, 333, 448, 565],[  0, 115, 232, 351, 472, 595]]]]])n = k.sum(dim=(2,3))
n.shape
Out[31]: torch.Size([1, 1, 6])
n
Out[32]: tensor([[[   0, 1160, 2360, 3600, 4880, 6200]]])

1.2、测试.sum(dim=-1)

程序接上面程序执行
所以dim取值就是要消除的维度,dim取-1就是要消除最后一个维度。
对于上面的有5个维度的矩阵,维度索引 0,1,2,3,4。
先执行消除维度2,3,保留的维度0,1,4,然后再执行消除维度-1,即消除维度4,则剩下的维度是0,1.

o = n.sum(dim=-1)
o.shape
Out[34]: torch.Size([1, 1])
o
Out[35]: tensor([[18200]])

pytorch函数测试相关推荐

  1. pytorch 函数clamp

    pytorch 函数clamp clamp表示夹紧,夹住的意思,torch.clamp(input,min,max,out=None)-> Tensor 将input中的元素限制在[min,ma ...

  2. pytorch方法测试——卷积(二维)

    pytorch方法测试--卷积(二维) 测试代码: import torch import torch.nn as nnm = nn.Conv2d(2, 2, 3, stride=2) input = ...

  3. 经典笔试题:用C写一个函数测试当前机器大小端模式

    "用C语言写一个函数测试当前机器的大小端模式"是一个经典的笔试题,如下使用两种方式进行解答: 1. 用union来测试机器的大小端 1 #include <stdio.h&g ...

  4. Boost:用成员函数测试bind <void>

    Boost:用成员函数测试bind 实现功能 C++实现代码 实现功能 boost::bind模块,用成员函数测试bind C++实现代码 #include <boost/config.hpp& ...

  5. wnoise matlab,MATLAB中用wnoise函数测试去噪算法

    MATLAB中用wnoise函数测试去噪算法 sqrt_snr=3; init=231434; [x,xn]=wnoise(3,11,sqrt_snr,init); % WNOISE generate ...

  6. PyTorch安装测试训练建自己的数据集

    Pytorch安装测试训练建自己的数据集 前言 一.PyTorch是什么? 二.PyTorch环境搭建 1.设备要求 2.安装Pytorch 3.验证PyTorch 二.CIFAR10测试 1.关于C ...

  7. 深入浅出Pytorch函数——torch.zeros

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出Pytorch函数--torch.Tensor · 深入浅出Pytorch函数--torch.ones · 深入浅出 ...

  8. 深入浅出Pytorch函数——torch.arange

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange · 深入 ...

  9. 深入浅出Pytorch函数——torch.zeros_like

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出Pytorch函数--torch.Tensor · 深入浅出Pytorch函数--torch.ones · 深入浅出 ...

  10. Pytorch函数expand()详解

    Pytorch函数 .expand( ) 其将单个维度扩大成更大维度,返回一个新的tensor,具体看下例: import torcha = torch.Tensor([[1], [2], [3],[ ...

最新文章

  1. Azure正式对外发布容器服务,支持Swarm和Mesos
  2. java操作XML文件--读取内容
  3. 动态加载___import__动态加载技术
  4. Serilog 日志框架如何自动删除超过 N 天的日志 ?
  5. 搜索引擎关键词劫持之php篇(源码与分析)
  6. 基于python和opencv的人脸识别
  7. 使用一个for循环将N*N的二维数组的所有值置1
  8. oracle修改用户密码
  9. Android 资源(Resources)访问
  10. 计算机网络路由计算,计算机网络中的多播路由算法
  11. 七.项目管理基础知识
  12. 计算机桌面墙纸更换,电脑系统教程:win7桌面壁纸怎么换
  13. 2020强网杯部分题总结与复现
  14. 日积(Running)月累(ZSSURE):Learning How to Learn,学习习惯
  15. 2023最新猕猴桃影视系统源码/影视APP源码+安卓/苹果双端
  16. Matlab图片预处理——截取图片中有效部分保存在其余文件夹下
  17. shell一键安装lnmp
  18. 前端学习资料网址汇总
  19. 炫舞时代显示服务器出错,qq炫舞2各种常见bug大全 FAQ详解
  20. 淘宝/天猫如何获取sku API接口,item_sku - 获取sku详细信息

热门文章

  1. 计算机常规教学ppt,计算机基本PPT教学.ppt
  2. iMac上用bootcamp安装Win10遇到的问题总结
  3. oracle timesten tt的启动与停止
  4. vi打开GBK编码文件乱码问题
  5. 如何修改电脑微信的提示音(亲测有效)
  6. 分享一个TEXT文档加密/解密编辑器
  7. 树莓派2研究之:交叉编译小度WIFI驱动,让小派支持小度WIFI
  8. HTML中怎么适应所有浏览器,css如何自适应浏览器高度?
  9. Google Widevine及其工作原理
  10. Python路飞学城老男孩内部书籍,Python全栈开发实战pdf