【问题标题】:Tensorflow CNN MNIST example, weight dimensionsTensorflow CNN MNIST 示例,权重维度
【发布时间】:2017-05-27 13:33:52
【问题描述】:

我刚开始使用 Tensorflow 进行编程,虽然我已经对一般的神经网络概念非常熟悉(这很奇怪,我知道,怪我的大学)。我一直在尝试更改this CNN example 的实现以使我自己的设计能够工作。我的问题是关于权重初始化:

weights = {
    # 5x5 conv, 1 input, 32 outputs (i.e. 32 filters)
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
    # 5x5 conv, 32 inputs, 64 outputs
    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
    # fully connected, 7*7*64 inputs, 1024 outputs
    'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
    # 1024 inputs, 10 outputs (class prediction)
    'out': tf.Variable(tf.random_normal([1024, n_classes]))
}

如果第二层有 32 个输入和 64 个输出,这是否意味着它只应用了 2 个过滤器? (似乎很少?)这是否意味着,要实现 5 个连续的 3x3 卷积层,我应该继续将之前的输出数量乘以该层中的过滤器数量,如下所示:

weights = {
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 20])),
    'wc2': tf.Variable(tf.random_normal([3, 3, 20, 41])),
    'wc3': tf.Variable(tf.random_normal([3, 3, 20*41, 41])),
    'wc4': tf.Variable(tf.random_normal([3, 3, 20*41*41, 62])),
    'wc5': tf.Variable(tf.random_normal([3, 3, 20*41*41*62, 83])),
    'out': tf.Variable(tf.random_normal([3, 3, 20*41*41*62*83, n_classes]))
}

感觉好像我做错了什么。

【问题讨论】:

    标签: tensorflow conv-neural-network


    【解决方案1】:

    是的,你做错了什么。

    您的输入矩阵是 [batch,height,width,depth],其中深度最初为 1。

    我们以wc1为例[3,3,1,20]。这意味着它将有 20 个不同的过滤器,每个过滤器将跨越 1 个深度并覆盖 3x3 的高度 x 宽度。每个过滤器将通过跨越所有深度的整个图像。由于有 20 种不同的过滤器将创建 [batch,height,width,20] 的输出张量

    从概念上讲,我们有机会将像素强度的深度改为前一个像素周围每 3x3 像素 20 个类。

    如果我们随后应用 [3, 3, 20, 41],我们将创建 41 个过滤器,其中每个过滤器的深度为 20,高度 x 宽度为 3x3,在所有高度和宽度上滑动以生成每个41 种不同的过滤器。结果是 [batch,height,width,41],即每像素 41 个类。

    你的下一个变换是 [3, 3, 20*41, 41] 这是错误的。没有 20*41 的深度,你有 41 的深度。

    这是您需要的更新:

    weights = {
        'wc1': tf.Variable(tf.random_normal([3, 3, 1, 20])),
        'wc2': tf.Variable(tf.random_normal([3, 3, 20, 41])),
        'wc3': tf.Variable(tf.random_normal([3, 3, 41, 41])),
        'wc4': tf.Variable(tf.random_normal([3, 3, 41, 62])),
        'wc5': tf.Variable(tf.random_normal([3, 3, 62, 83])),
        'out': tf.Variable(tf.random_normal([1, 1, 83, n_classes]))
    }
    

    取决于你是否在做 max_pooling,是否填充,将决定应用 wc5 后的输出形状。

    如果在 wc1 之后应用 [1,2,2,1] max_pool,则 [height,width] 从 [28,28] 减少到 [14,14]。

    如果在 wc2 之后还有另一个 [1,2,2,1] max_pool 则 [height,width] 从 [14,14] 减少到 [7,7]。

    7 不能被 2 整除。如果 wc3 应用时没有填充,则 [height,width] 从 [7,7] 减少到 [5,5]。对 wc4 -> [3,3] 和 wc5 -> [1,1] 做同样的事情。

    最后 out 将位于 [batch,1,1,83] 矩阵上,该矩阵会将其转换为 [batch,1,1,class] 矩阵!

    【讨论】:

    • 感谢您解释整个事情!你帮了我很多。
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2017-08-31
    • 2017-01-02
    • 2016-02-16
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多