【发布时间】:2019-04-03 09:28:04
【问题描述】:
我正在尝试使用 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 我会得到镜像参数,但我不确定最常用的是什么?编码器和解码器是否应该有镜像参数以获得更好的性能?
【问题讨论】:
标签: python tensorflow keras autoencoder