tf.reshape与tf.contrib.layers.flatten [英] tf.reshape vs tf.contrib.layers.flatten

查看:46
本文介绍了tf.reshape与tf.contrib.layers.flatten的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在运行CNN来解决分类问题.我有3个转换层和3个池化层.P3是最后一个池化层的输出,其尺寸为: [Batch_size,4,12,48] ,我想将该矩阵展平为 [Batch_size,2304] 大小矩阵, 2304 = 4 * 12 * 48 .我一直在使用选项A"(请参见下文),但有一天我想尝试选项B",从理论上讲,我会得到相同的结果.但是,事实并非如此.我之前已经检查了以下线程

So I am running a CNN for a classification problem. I have 3 conv layers with 3 pooling layers. P3 is the output of the last pooling layer, whose dimensions are: [Batch_size, 4, 12, 48], and I want to flatten that matrix into a [Batch_size, 2304] size matrix, being 2304 = 4*12*48. I had been working with "Option A" (see below) for a while, but one day I wanted to try out "Option B", which would theoretically give me the same result. However, it did not. I have cheked the following thread before

是tf.contrib.layer.flatten(x)与tf.reshape(x,[n,1])一样吗?

但这只是增加了更多的困惑,因为尝试选项C"(取自上述线程)给出了新的不同结果.

but that just added more confusion, since trying "Option C" (taken from the aforementioned thread) gave a new different result.

P3 = tf.nn.max_pool(A3, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding='VALID')

P3_shape = P3.get_shape().as_list()

P = tf.contrib.layers.flatten(P3)                             <-----Option A

P = tf.reshape(P3, [-1, P3_shape[1]*P3_shape[2]*P3_shape[3]]) <---- Option B

P = tf.reshape(P3, [tf.shape(P3)[0], -1])                     <---- Option C

我更倾向于使用选项B",因为这是我在Dandelion Mane(

I am more inclined to go with "Option B" since that is the one I have seen in a video by Dandelion Mane (https://www.youtube.com/watch?v=eBbEDRsCmv4&t=631s), but I would like to understand why these 3 options are giving different results.

推荐答案

所有3个选项的重塑方式相同:

All 3 options reshape identically:

import tensorflow as tf
import numpy as np

p3 = tf.placeholder(tf.float32, [None, 1, 2, 4])

p3_shape = p3.get_shape().as_list()

p_a = tf.contrib.layers.flatten(p3)                                  # <-----Option A

p_b = tf.reshape(p3, [-1, p3_shape[1] * p3_shape[2] * p3_shape[3]])  # <---- Option B

p_c = tf.reshape(p3, [tf.shape(p3)[0], -1])                          # <---- Option C

print(p_a.get_shape())
print(p_b.get_shape())
print(p_c.get_shape())

with tf.Session() as sess:
    i_p3 = np.arange(16, dtype=np.float32).reshape([2, 1, 2, 4])
    print("a", sess.run(p_a, feed_dict={p3: i_p3}))
    print("b", sess.run(p_b, feed_dict={p3: i_p3}))
    print("c", sess.run(p_c, feed_dict={p3: i_p3}))

此代码产生相同的结果3次.您的不同结果是由其他原因引起的,而不是由重塑引起的.

This code yields the same result 3 times. Your different results are caused by something else and not by the reshaping.

这篇关于tf.reshape与tf.contrib.layers.flatten的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆