我如何理解排名>2的tf.keras.layers.Dense的内核?

2024-05-01 22:06:18 发布

您现在位置:Python中文网/ 问答频道 /正文

如何理解tf.keras.layers.Densefor rank>;的内核;2?

API官方文件规定:

Note: 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 0 of the kernel (using tf.tensordot). For example, if input has dimensions (batch_size, d0, d1), then we create a kernel with shape (d1, units), and the kernel operates along axis 2 of the input, on every sub-tensor of shape (1, 1, d1) (there are batch_size * d0 such sub-tensors). The output in this case will have shape (batch_size, d0, units).

我的理解是,对于大于2的秩(例如秩3),只创建一个内核,因此相同的内核应用于第二维度的所有切片,如上所述。 因此,这将意味着第二维度不同指标的输出彼此不独立(特别是在培训期间)

我的理解正确吗?如果是的话,有没有一种简单的方法来使用一堆内核,或者我必须实现张量乘法


Tags: andoftheinputsizetfbatchkernel
1条回答
网友
1楼 · 发布于 2024-05-01 22:06:18

是的,你的理解是正确的

要实现所需,需要定义自定义keras层。假设层的输入是形状(批次大小,d0,i0)。该层的大部分内容将类似于原始的Dense层(链接:github),除了

  1. build函数中,self.kernel的形状改为(d0,i0,units)。您可以从input_shape获取d0i0的值
  2. call函数中,要在inputsself.kernel之间执行指定的张量乘法,请将tf.einsum与以下等式一起使用:tf.einsum('abc,bcg->abg', inputs, self.kernel)

相关问题 更多 >