【问题标题】:Classification with Keras Autoencoders使用 Keras 自动编码器进行分类
【发布时间】:2017-07-09 15:07:57
【问题描述】:

我正在尝试使用 Keras(带有 Tensorflow 后端)采用香草自动编码器,并在损失值收敛到特定值时停止它。在最后一个 epoch 之后,我想使用 sigmoid 函数来执行分类。你知道怎么做吗(或者至少给我指出正确的方向)?

下面的代码与http://wiseodd.github.io/techblog/2016/12/03/autoencoders/ 的普通自动编码器非常相似。 (我使用的是我自己的数据,但请随意使用链接中的 MNIST 示例来演示您在说什么。)

NUM_ROWS = len(x_train)
NUM_COLS = len(x_train[0])

inputs = Input(shape=(NUM_COLS, ))
h = Dense(64, activation='sigmoid')(inputs)
outputs = Dense(NUM_COLS)(h)

# trying to add last sigmoid layer
outputs = Dense(1)
outputs = Activation('sigmoid')

model = Model(input=inputs, output=outputs)

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train,
    batch_size=batch,
    epochs=epochs,
    validation_data=(x_test, y_test))

【问题讨论】:

  • 您好,您的 x_train 是什么,y_train 是什么?这看起来不像自动编码器
  • 请看链接中的示例。如果我让它在这方面发挥作用,我就能让它在我的数据上发挥作用。
  • 让我换个方式提出同样的问题:你想用自动编码器实现什么。您可以使用标准分类模型。

标签: machine-learning neural-network deep-learning keras autoencoder


【解决方案1】:

我对你的目标有一个解释,但是,你自己似乎没有一个非常清晰的形象。 如果您自己准备必要的数据集,我想您可以澄清一下。

一种可能的解决方案如下:

NUM_ROWS = len(x_train)
NUM_COLS = len(x_train[0])

inputs = Input(shape=(NUM_COLS, ))
h = Dense(64, activation='sigmoid')(inputs)
outputs = Dense(NUM_COLS)(h)

model = Model(input=inputs, output=outputs)

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, x_train,
    batch_size=batch,
    epochs=epochs,
    validation_data=(x_test, y_test))

h.trainable=False


# trying to add last sigmoid layer
outputs = Dense(1)(h)
outputs = Activation('sigmoid')

model2.fit(x_train, y_train,
    batch_size=batch,
    epochs=epochs,
    validation_data=(x_test, y_test))

【讨论】:

  • 你应用 sigmoid 两次是有原因的吗? h = Dense(64, activation='sigmoid')(inputs)outputs = Activation('sigmoid')
猜你喜欢
  • 2018-05-11
  • 2019-06-29
  • 1970-01-01
  • 2018-11-25
  • 2017-07-19
  • 2017-10-22
  • 2019-07-22
  • 2018-09-13
  • 1970-01-01
相关资源
最近更新 更多