python - 为什么我的编码器和解码器的参数在我的自动编码器中不对称?
问题描述
我正在尝试使用 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
,我会得到镜像参数,但我不确定最常用的是什么?编码器和解码器是否应该具有镜像参数以获得更好的性能?
解决方案
正如您已经认为这里的问题是偏见。如果你以 Dense 12 和 Dense 13 之间的权重为例,你有1024*8 = 8192
正常的权重 +8
偏差(8200
总共)。
如果你取 Dense 17 到 18 之间的权重,你将得到8*1024 = 8192
正常的权重 +1024
偏差(9216
总共)。下一层的神经元总是有多少偏差。
希望这能回答你的问题。
推荐阅读
- c# - 有没有办法使用单个字符获取枚举的字符串值?
- angularjs - 在anjular js中处理来自api的分块响应
- jenkins - 如何在使用 Jenkins 执行的 SoapUI 项目中配置(过滤)日志?
- php - (PHP) 向 json 文件添加不正确的文本
- apache-spark - Spark 结构化流 - 限制?(源性能、不支持的操作、Spark UI)
- r - 无法使用 read.table 函数加载 r 中的所有变量
- ios - AVAssetImageGenerator.generateCGImagesAsynchronously 的帧不正确
- reactjs - 带有私有数据服务器的 Reactjs/Express 应用程序
- azure - 如何使用虚拟网络保护从应用服务到 Azure Sql 数据库的访问?
- c - alloca() 和 setjmp 的作用是什么?