【问题标题】:How to fix loss not going down when training如何解决训练时损失不下降的问题
【发布时间】:2019-12-29 18:01:34
【问题描述】:

当我训练我的自动编码器时,无论我训练多少损失都不会改变。

#Importing training data
inp = open('train.csv',"rb")
X = pickle.load(inp)
X = X/255.0
X = np.array(X)
X = np.reshape(X,(-1,25425))


input_img =tf.keras.layers.Input(25425,)
encoded1 = tf.keras.layers.Dense(75,activation=tf.nn.relu)(input_img)
encoded2 = tf.keras.layers.Dense(50,activation=tf.nn.relu)(encoded1)
decoded = tf.keras.layers.Dense(25425, activation='sigmoid')(encoded2)
# The input of the autoencoder is the image (input_img), and the output is the decoder layer (decoded)
autoencoder = tf.keras.Model(input_img, decoded)

encoder = tf.keras.Model(input_img, encoded2)

encoded_input = tf.keras.layers.Input(shape=(50,))
# The decoded only consists of the last layer
decoder_layer = autoencoder.layers[-1](encoded_input)
# The input to the decoder is the vector of the encoder which will be fed (using encoded_input), the output is the last layer of the network (decoder_layer)
decoder = tf.keras.Model(encoded_input, decoder_layer)

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X, X, epochs=50, shuffle=True)

# Save the weights
autoencoder.save_weights('model_weights.h5')

# Save the model architecture
with open('model_architecture.json', 'w') as f:
    f.write(autoencoder.to_json())

我希望我可以让训练更好地发挥作用但是我的损失停留在 0.6932

【问题讨论】:

  • 输出维度从 50 大幅增加到 25425。另外,不要将 ReLU 与 sigmoid 一起使用。

标签: python tensorflow keras autoencoder


【解决方案1】:

自动编码器的任务是压缩输入大小,同时最小化信息丢失。您的解码器尝试做的是仅从 50 点重建 25425 点 - 超过 500 倍 压缩,这是非常不现实的。我还假设您的输入在(0,1) 之间进行了标准化,因为 sigmid 无法超出。

解决方案:更改解码器编码器的单元数,使input_dim/min_units 的比率“合理”(例如,5:1)。 min_units = 具有最少单元(编码器或解码器)的层的单元数 - 在您的情况下为 50。(注意25425 非常大 - 考虑通过下采样降低维度(例如 @ 987654328@),或使用卷积自动编码器)

关于 ReLU 和 sigmoid,两者都可以使用,但要小心在 ReLU 中通过例如爆炸梯度/激活。重量衰减 - 见this discussion

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 2018-03-30
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多