首页 > 解决方案 > 为什么我的编码器和解码器的参数在我的自动编码器中不对称?

问题描述

我正在尝试使用 Keras API 在 Tensorflow 中实现自动编码器。我的代码灵感来自 Keras 网站上的示例:https ://blog.keras.io/building-autoencoders-in-keras.html

目标是能够通过测量重建误差来检测数据集中的异常值。我的代码如下所示(我删除了一些层以使其更适合):

inputD = tf.keras.Input(shape=(1602,))
encoded = tf.keras.layers.Dense(1024, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal' )(inputD)
encoded = tf.keras.layers.Dense(8, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(encoded)
encoded = tf.keras.layers.Dense(4, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(encoded)
encoded = tf.keras.layers.Dense(3, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(encoded)

decoded = tf.keras.layers.Dense(4, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(encoded)
decoded = tf.keras.layers.Dense(8, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(decoded)
decoded = tf.keras.layers.Dense(1024, activation=tf.nn.leaky_relu, kernel_initializer='glorot_normal')(decoded)
decoded = tf.keras.layers.Dense(1602, activation='sigmoid', kernel_initializer='glorot_normal')(decoded)


autoencoder = tf.keras.Model(inputD, decoded)
adam = tf.keras.optimizers.Adam(lr=0.0001)
autoencoder.compile(optimizer=adam, loss='binary_crossentropy',  metrics=['mse'])

autoencoder.summary()

这导致以下模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 1602)              0         
_________________________________________________________________
dense_12 (Dense)             (None, 1024)              1641472   
_________________________________________________________________
dense_13 (Dense)             (None, 8)                 8200      
_________________________________________________________________
dense_14 (Dense)             (None, 4)                 36        
_________________________________________________________________
dense_15 (Dense)             (None, 3)                 15        
_________________________________________________________________
dense_16 (Dense)             (None, 4)                 16        
_________________________________________________________________
dense_17 (Dense)             (None, 8)                 40        
_________________________________________________________________
dense_18 (Dense)             (None, 1024)              9216      
_________________________________________________________________
dense_19 (Dense)             (None, 1602)              1642050   
=================================================================
Total params: 3,301,045
Trainable params: 3,301,045
Non-trainable params: 0

我不明白为什么我的参数不是对称的,我希望例如最后一层的权重矩阵的形状与输入层相同,但事实并非如此。这是正常的吗?

当我输入这个时,我认为这可能是因为隐藏层中的偏差。如果我设置use_bias=False,我会得到镜像参数,但我不确定最常用的是什么?编码器和解码器是否应该具有镜像参数以获得更好的性能?

标签: pythontensorflowkerasautoencoder

解决方案


正如您已经认为这里的问题是偏见。如果你以 Dense 12 和 Dense 13 之间的权重为例,你有1024*8 = 8192正常的权重 +8偏差(8200总共)。

如果你取 Dense 17 到 18 之间的权重,你将得到8*1024 = 8192正常的权重 +1024偏差(9216总共)。下一层的神经元总是有多少偏差。

希望这能回答你的问题。


推荐阅读