密集层 Keras 的 3d 输入

数据挖掘 深度学习 神经网络 喀拉斯 张量流 线性代数
2022-03-04 22:28:44

有没有 KerasDense层如何处理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)

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

1个回答

在常规的全连接层(密集)中,使用以下矩阵运算完成计算:

R=AW+B
所有矩阵都是向量(如果批量大小 = 1),例如W,它有大小(输入大小,输出大小)。

对于 3D 输入,TF 计算输出的方式只是将此公式仅应用于最后一个维度,考虑到所有其他维度类似于批量大小。

因此,在您输入大小为 (4,3,2) 的示例中,Tensorflow 将向量“展平”为 (12, 2) 矩阵,将结果计算为 2D 输入,这给出了 (12, 5)向量,然后“展开”向量以返回其 3 个维度 (4,3,5)。