密集层 Keras 的 3d 输入

3d input for Dense Layer Keras

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

文档解释如下:

If the input to the layer has a rank greater than 2, then Dense computes the dot product between the inputs and the kernel along the last axis of the inputs and axis 1 of the kernel (using 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)

形状的

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

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