TensorFlow中的高阶函数:tf.map_fn()
来源:互联网 发布:淘宝上买彩票 编辑:程序博客网 时间:2024/05/18 03:06
TensorFlow中的高阶函数:tf.map_fn()
在TensorFlow中,有一些函数被称为高阶函数(high-level function),和在python中的高阶函数意义相似,其也是将函数当成参数传入,以实现一些有趣的,有用的操作。其中tf.map_fn()
就是其中一个。我们这里介绍一下这个函数。
首先引入一个TF在应用上的问题:一般我们处理图片的时候,常常用到卷积,也就是tf.nn.conv2d()
,但是这个函数的输入格式如:(batch_size, image_height, image_width, image_channel)
,其中batch_size
为一个批次的大小,我们注意到,如果按照这种输入的话,我们只能对一张图片进行卷积操作。在需要对视频进行卷积操作的时候,因为视频的输入格式一般如:(batch_size, video_size, frame_height, frame_width, frame_channel)
,其中多出了一个视频的长度video_size
,这样我们就不能简单对视频进行卷积了。我们用一张图描述我们面临的问题和map_fn是如何帮我们解决的:
其中,我们的数据就是从clip #1
到clip #n
,我们需要对每一个clip应用相同的方法function,比如是卷积函数tf.nn.conv2d()
,从而得到batch_size个结果result #1
到result #n
。这个就是map_fn()的效果,而且,因为各个batch之间没有关联,所以可以并行快速高效地处理。我们再看看这个函数的用法,先贴出其API手册:
tf.map_fn
map_fn( fn, elems, dtype=None, parallel_iterations=10, back_prop=True, swap_memory=False, infer_shape=True, name=None)
其中的fn
是一个可调用的(callable)函数,就是我们图中的function,一般会使用lambda表达式表示。elems
是需要做处理的Tensors,TF将会将elems
从第一维展开,进行map处理。主要就是那么两个,其中dtype
为可选项,但是比较重要,他表示的是fn
函数的输出类型,如果fn
返回的类型和elems
中的不同,那么就必须显式指定为和fn
返回类型相同的类型。下面给出一个视频卷积的例子:
batch = data.get_batch() # batch with the shape of (batch_size, video_size, frame_height, frame_width, frame_channel)cnn = tf.map_fn(fn=lambda inp: tf.nn.conv2d(inp, kernel, stride, padding='SAME'), elems=batch, dtype=tf.float32)
这样我们就对每一个batch的(batch-wise)进行了卷积处理了,大家不妨试试吧!
- TensorFlow中的高阶函数:tf.map_fn()
- tensorflow中的tf.nn这类函数
- 关于Tensorflow中的tf.train.batch函数
- tensorflow 中的tf.gradients 与 tf.stop_gradient() 函数
- tensorflow高阶教程:tf.dynamic_rnn
- tensorflow高阶教程:tf.dynamic_rnn
- 【Tensorflow】tf.placeholder函数
- 【Tensorflow】tf.reshape 函数
- 【Tensorflow】tf.argmax函数
- Tensorflow tf.placeholder函数
- 【Tensorflow】tf.Graph()函数
- 【Tensorflow】tf.placeholder函数
- 【Tensorflow】tf.concat函数
- TensorFlow中的tf.nn.softmax_cross_entropy_with_logits 交叉熵 损失函数
- tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
- tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
- TensorFlow-tf.nn.conv2d 函数
- 【Tensorflow】tf.nn.relu函数
- numpy数据类型
- Eclipse在线安装黑色主题(DarkTheme)
- 神经网络为什么要归一化?
- 欢迎使用CSDN-markdown编辑器
- GitLab: You are not allowed to push code to protected branches on this project.
- TensorFlow中的高阶函数:tf.map_fn()
- centos6.X系统非root用户启动的程序无法创建超过1024个进程的问题
- 关于windowIsTranslucent影响生命周期
- Ext的ajax操作
- isa指针
- hibernate,以及sql执行顺序、数据锁
- 九周第五次课 2017.12.15 MariaDB安装、Apache安装
- 浅谈MD5加密算法(JAVA实现)
- 小程序类似糗事百科一样左右滑的效果,每个页面有下拉加载