【问题标题】:How to train a Keras Model with L1-norm reconstruction loss function?如何训练具有 L1 范数重建损失函数的 Keras 模型?
【发布时间】:2022-01-18 03:07:24
【问题描述】:

我目前正在使用 Kears 为 MNIST 数据集构建自动编码器,这是我的代码:

import all the dependencies
from keras.layers import Dense,Conv2D,MaxPooling2D,UpSampling2D
from keras import Input, Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt

encoding_dim = 15 
input_img = Input(shape=(784,))
# encoded representation of input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# decoded representation of code 
decoded = Dense(784, activation='sigmoid')(encoded)
# Model which take input image and shows decoded images
autoencoder = Model(input_img, decoded)

# This model shows encoded images
encoder = Model(input_img, encoded)
# Creating a decoder model
encoded_input = Input(shape=(encoding_dim,))
# last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

最后一步是编译步骤,但我需要使用 L1-norm 重建损失函数。从Keras losses description 看来他们没有这个功能。如何将 L1 范数重建损失函数应用于 autoencoder.compile() 函数?谢谢!

【问题讨论】:

    标签: python tensorflow machine-learning keras mnist


    【解决方案1】:

    在损失函数中,我们指的是预期误差值。因此,对于 L1 范数,您可以使用名称为 mean_absolute_errorMAE(平均绝对误差)。因此,您可以将代码的最后一行重写如下:

    autoencoder.compile(optimizer='adam', loss='mean_absolute_error')
    

    【讨论】:

    • 差点忘了MAE是L1 loss,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2020-02-29
    • 2019-09-18
    相关资源
    最近更新 更多