微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在异常检测自动编码器中使用哪种损耗函数以及选择哪种输出形状?

如何解决在异常检测自动编码器中使用哪种损耗函数以及选择哪种输出形状?

我正在尝试构建用于在Python中进行异常检测的自动编码器模型。我的标签(anomaly_label)是0(正常)或1(异常)。我试图了解要使用哪个损失函数;如果我没看错,因为我只有两个值,并且我的标签不是一字编码的(整数列),那么最好选择“ sparse_categorical_crossentropy”或“ binary_crossentropy”。我说的对吗?

我的第二个问题是关于输出层的大小,当我使用'sparse_categorical_crossentropy'时,将其设置为类数(在这种情况下为2),但是当我使用'binary_crossentropy'时,将其设置为1。正确吗? ?

以下是我的模型架构:

def get_autoencoder(loss_func):

    out_size = 0
    num_classes = 2
    if loss_func == 'sparse_categorical_crossentropy':
        out_size = num_classes
    if loss_func == 'binary_crossentropy':
        out_size = 1

    # input layer
    inp = Input(shape=(78,)) # layer 1
    
    # encoding layers
    enc = Dense(512)(inp) # layer 2
    enc = Dropout(0.5)(enc)
    enc = LeakyReLU(0.01)(enc)
    enc = Dense(256)(enc) # layer 3
    enc = Dropout(0.5)(enc)
    enc = LeakyReLU(0.01)(enc)
    enc = Dense(128)(enc) # layer 4
    enc = Dropout(0.5)(enc)
    enc = LeakyReLU(0.01)(enc)
    
    # bottleneck layer
    mid = Dense(64)(enc) # layer 5
    mid = Dropout(0.5)(mid)
    mid = LeakyReLU(0.01)(mid)
    
    # decoding layers
    dec = Dense(128)(mid) # layer 6
    dec = Dropout(0.5)(dec)
    dec = LeakyReLU(0.01)(dec)
    dec = Dense(256)(dec) # layer 7
    dec = Dropout(0.5)(dec)
    dec = LeakyReLU(0.01)(dec)
    dec = Dense(512)(dec) # layer 8
    dec = Dropout(0.5)(dec)
    dec = LeakyReLU(0.01)(dec)
    
    # output layer
    out = Dense(out_size,activation='sigmoid')(dec)
    
    # create new model
    autoencoder = Model(inp,out)
    autoencoder.compile(loss=loss_func,optimizer='rmsprop')
    return autoencoder

如果有人可以帮助我了解我在做什么是对还是错,以及如果是后者,如何纠正我的模型,我将不胜感激。谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。