图解TensorFlow op:tf.nn.space_to_depth

2024-04-01 03:58

本文主要是介绍图解TensorFlow op:tf.nn.space_to_depth,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

田海立@CSDN 2020-10-20

TensorFlow算子space_to_depth是depth_to_space的逆操作。本文用图文的方式来解释该算子运算的方式。

 

一、space_to_depth原型

space_to_depth是把space数据(width和height维)移到depth(Channel)维上,与depth_to_space刚好是反向的操作。对应到ML该操作是把width和height维上各取block_size都分给depth上。所以,对应有一个参数block_size,要求原Tensor的height和width都是block_size的整数倍

这样,新的Tensor

  • Width是 input_width / block_size;
  • Height是 input_height / block_size;
  • Depth是input_depth * block_size * block_size

原型如下:

tf.nn.space_to_depth(input, block_size, data_format='NHWC', name=None
)

因为这里严格区分了C与H/W各维度,如果数据格式不是NHWC时,需要指定data_format。

 

二、space_to_depth程序实现

以[1, 6, 4, 3] space_to_depth(block_size = 2)为例:

>>> 
>>> t = tf.range(4*6*3)
>>> t = tf.reshape(t, [1, 4, 6, 3])
>>> t
<tf.Tensor: shape=(1, 4, 6, 3), dtype=int32, numpy=
array([[[[ 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]]]], dtype=int32)>
>>> 

 

 

执行space_to_depth(block_size = 2)之后:

>>> 
>>> t = tf.nn.space_to_depth(t, 2)
>>> t
<tf.Tensor: shape=(1, 2, 3, 12), dtype=int32, numpy=
array([[[[ 0,  1,  2,  3,  4,  5, 18, 19, 20, 21, 22, 23],[ 6,  7,  8,  9, 10, 11, 24, 25, 26, 27, 28, 29],[12, 13, 14, 15, 16, 17, 30, 31, 32, 33, 34, 35]],[[36, 37, 38, 39, 40, 41, 54, 55, 56, 57, 58, 59],[42, 43, 44, 45, 46, 47, 60, 61, 62, 63, 64, 65],[48, 49, 50, 51, 52, 53, 66, 67, 68, 69, 70, 71]]]], dtype=int32)>
>>>

 

 

三、space_to_depth对数据的处理

space_to_depth操作对数据的处理就是:

  1. 选取[in_batch, block_size, block_size, in_depth]为一个Tensor;
  2. 对Step#1里的Tensor做reshape操作改变为[in_batch, 1, 1, in_channel * block_size * block_size];
  3. 按照先Width方向再Height方向的顺序同Step#1一样选择Tensor;
  4. 对Step#3里的Tensor同Step#3一样做reshape操作;
  5. 把Step#4和Step#4里得到的新Tensor拼接起来。

最后的Tensor的shape也就是[in_batch, in_height / block_size, in_width / block_size, in_channel * (block_size*block_size)]

 

上述的处理过程,一张图展示就是这样:

 

总结

本文分析了tf.nn.space_to_depth对Tensor的处理。维度上做变换;数据上把height和width的block_size*block_size数据做reshape,也就实现了depth上的扩充,然后再整体拼接在一起。

 

【其他相关话题】

depth_to_space是这一过程的逆过程,参看《图解TensorFlow op:tf.nn.depth_to_space》。

data_format也可以指定为其他格式,比如NCHW,也请自行分析,这里不再赘述。

这篇关于图解TensorFlow op:tf.nn.space_to_depth的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/866154

相关文章

pytorch之torch.flatten()和torch.nn.Flatten()的用法

《pytorch之torch.flatten()和torch.nn.Flatten()的用法》:本文主要介绍pytorch之torch.flatten()和torch.nn.Flatten()的用... 目录torch.flatten()和torch.nn.Flatten()的用法下面举例说明总结torch

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

龙蜥操作系统Anolis OS-23.x安装配置图解教程(保姆级)

《龙蜥操作系统AnolisOS-23.x安装配置图解教程(保姆级)》:本文主要介绍了安装和配置AnolisOS23.2系统,包括分区、软件选择、设置root密码、网络配置、主机名设置和禁用SELinux的步骤,详细内容请阅读本文,希望能对你有所帮助... ‌AnolisOS‌是由阿里云推出的开源操作系统,旨

图解TCP三次握手|深度解析|为什么是三次

写在前面 这篇文章我们来讲解析 TCP三次握手。 TCP 报文段 传输控制块TCB:存储了每一个连接中的一些重要信息。比如TCP连接表,指向发送和接收缓冲的指针,指向重传队列的指针,当前的发送和接收序列等等。 我们再来看一下TCP报文段的组成结构 TCP 三次握手 过程 假设有一台客户端,B有一台服务器。最初两端的TCP进程都是处于CLOSED关闭状态,客户端A打开链接,服务器端

图解可观测Metrics, tracing, and logging

最近在看Gophercon大会PPT的时候无意中看到了关于Metrics,Tracing和Logging相关的一篇文章,凑巧这些我基本都接触过,也是去年后半年到现在一直在做和研究的东西。从去年的关于Metrics的goappmonitor,到今年在排查问题时脑洞的基于log全链路(Tracing)追踪系统的设计,正好是对这三个话题的实践。这不禁让我对它们的关系进行思考:Metrics和Loggi

【CSS in Depth 2 精译_023】第四章概述 + 4.1 Flexbox 布局的基本原理

当前内容所在位置(可进入专栏查看其他译好的章节内容) 第一章 层叠、优先级与继承(已完结) 1.1 层叠1.2 继承1.3 特殊值1.4 简写属性1.5 CSS 渐进式增强技术1.6 本章小结 第二章 相对单位(已完结) 2.1 相对单位的威力2.2 em 与 rem2.3 告别像素思维2.4 视口的相对单位2.5 无单位的数值与行高2.6 自定义属性2.7 本章小结 第三章 文档流与盒模型(已

【CSS in Depth 2 精译_024】4.2 弹性子元素的大小

当前内容所在位置(可进入专栏查看其他译好的章节内容) 第一章 层叠、优先级与继承(已完结) 1.1 层叠1.2 继承1.3 特殊值1.4 简写属性1.5 CSS 渐进式增强技术1.6 本章小结 第二章 相对单位(已完结) 2.1 相对单位的威力2.2 em 与 rem2.3 告别像素思维2.4 视口的相对单位2.5 无单位的数值与行高2.6 自定义属性2.7 本章小结 第三章 文档流与盒模型(已

Nn criterions don’t compute the gradient w.r.t. targets error「pytorch」 (debug笔记)

Nn criterions don’t compute the gradient w.r.t. targets error「pytorch」 ##一、 缘由及解决方法 把这个pytorch-ddpg|github搬到jupyter notebook上运行时,出现错误Nn criterions don’t compute the gradient w.r.t. targets error。注:我用

win10不用anaconda安装tensorflow-cpu并导入pycharm

记录一下防止忘了 一、前提:已经安装了python3.6.4,想用tensorflow的包 二、在pycharm中File-Settings-Project Interpreter点“+”号导入很慢,所以直接在cmd中使用 pip install -i https://mirrors.aliyun.com/pypi/simple tensorflow-cpu下载好,默认下载的tensorflow

pytorch torch.nn.functional.one_hot函数介绍

torch.nn.functional.one_hot 是 PyTorch 中用于生成独热编码(one-hot encoding)张量的函数。独热编码是一种常用的编码方式,特别适用于分类任务或对离散的类别标签进行处理。该函数将整数张量的每个元素转换为一个独热向量。 函数签名 torch.nn.functional.one_hot(tensor, num_classes=-1) 参数 t