博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深度学习面试题23:批次张量和卷积核的简易定义方式
阅读量:4343 次
发布时间:2019-06-07

本文共 1350 字,大约阅读时间需要 4 分钟。

目录

  

  

  


 

直接定义的缺点

在tensorflow中假设有一批输入为:

其定义如下:

tf.constant([    [        [            [3, 1, -3],            [1, -1, 7]        ],        [            [-2, 2, -5],            [2, 7, 3]        ]    ],    [        [            [-1, 3, 1],            [-3, -8, 6]        ],        [            [4, 6, 8],            [5, 9, -5]        ]    ]], tf.float32)
View Code

这是一个4维张量,中括号的层次比较多,因此定义起来很容易写错;另外一批卷积核的定义方式和他不同,这也增加了定义的难度。

批次输入的张量的定义方式为:[batch, in_height, in_width, in_channels]

多个卷积核的定义方式为:[filter_height, filter_width, in_channels, out_channels]

 

 

简易定义的方式

 

对一批输入:

按照图中蓝色的顺序声明一个一维张量,然后按照[batch, in_height, in_width, in_channels],对一维张量reshape。

对一批卷积核:

按照图中红色的顺序声明一个一维张量,然后按照[filter_height, filter_width, in_channels, out_channels],对一维张量reshape。

这样的话不需要写很多行并且包含复杂中括号关系的代码,看着张量能快速写出对应声明,提高写代码的效率,另外出错的概率会降低

import tensorflow as tf# [batch, in_height, in_width, in_channels]input = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], tf.float32)input = tf.reshape(input,[2,2,2,2])print(input.shape)# [filter_height, filter_width, in_channels, out_channels]kernel = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], tf.float32)kernel = tf.reshape(kernel,[2,2,2,2])print(kernel.shape)print(tf.Session().run(tf.nn.conv2d(input,kernel,[1,1,1,1],"VALID")))
View Code

 

 

参考资料

《图解深度学习与神经网络:从张量到TensorFlow实现》_张平

 

 

 

转载于:https://www.cnblogs.com/itmorn/p/11250050.html

你可能感兴趣的文章
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>