【问题标题】:3d input for Dense Layer Keras密集层 Keras 的 3d 输入
【发布时间】:2021-07-20 07:59:54
【问题描述】:

有没有 Keras Dense 层如何处理 3D 输入的示例。

文档解释如下:

如果层的输入的秩大于 2,则 Dense 计算输入和内核之间的点积 输入的最后一个轴和内核的轴 1(使用 tf.tensordot)。

但我无法理解内部matrix calculation

例如:

import tensorflow as tf
from tensorflow.keras.layers import Dense
sample_3d_input = tf.constant(tf.random.normal(shape=(4,3,2)))
dense_layer  = Dense(5)
op = dense_layer(sample_3d_input)

根据3D 形状(m,d0,d1) 输入的文档,Layer's weight_matrix (or) kernel 的形状在这种情况下将具有(d1, units) which is (2,5) 形状。但我不明白 op 是如何计算得到形状 (m,d0, units)

【问题讨论】:

    标签: tensorflow keras deep-learning neural-network tensorflow2.0


    【解决方案1】:

    您可以自行重现操作...

    sample_3d_input = tf.constant(tf.random.normal(shape=(4,3,2)))
    dense_layer  = Dense(5)
    
    op = dense_layer(sample_3d_input)
    W, b = dense_layer.get_weights()
    
    manual_op = tf.tensordot(sample_3d_input, W, axes=[[2],[0]]) + b
    

    比较:

    >>> tf.reduce_all(op == manual_op).numpy()
    ... True
    

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 2018-02-22
      • 2021-05-07
      • 2020-08-17
      • 2023-03-06
      • 1970-01-01
      • 2020-12-03
      • 2019-02-18
      相关资源
      最近更新 更多