如何理解tf.keras.layers.Dense
for 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),只创建一个内核,因此相同的内核应用于第二维度的所有切片,如上所述。 因此,这将意味着第二维度不同指标的输出彼此不独立(特别是在培训期间)
我的理解正确吗?如果是的话,有没有一种简单的方法来使用一堆内核,或者我必须实现张量乘法
是的,你的理解是正确的
要实现所需,需要定义自定义keras层。假设层的输入是形状(批次大小,d0,i0)。该层的大部分内容将类似于原始的
Dense
层(链接:github),除了build
函数中,self.kernel
的形状改为(d0,i0,units)。您可以从input_shape
获取d0
和i0
的值李>call
函数中,要在inputs
和self.kernel
之间执行指定的张量乘法,请将tf.einsum
与以下等式一起使用:tf.einsum('abc,bcg->abg', inputs, self.kernel)
相关问题 更多 >
编程相关推荐