【问题标题】:Keras autoencoder and getting the compressed feature vector representationKeras 自动编码器并获得压缩的特征向量表示
【发布时间】:2018-06-18 13:55:01
【问题描述】:

我在一个文件中每行有一个句子,句子不超过 30 个单词。我正在使用 Keras 构建一个自动编码器,我对此很陌生 - 所以我可能做错了一些事情。所以,帮帮我吧。

我正在尝试使用自动编码器来获取中间上下文向量 - 编码步骤之后的压缩特征向量。 词汇表只是我文件中不同单词的列表。 300是词嵌入矩阵的维度。 30 是每个句子最多可以包含的单词。 X_train 是 (#of sentence, 30) 数字矩阵,其中每个数字都只是字典中单词所在的位置。

print len(vocabulary)
model = Sequential()
model.add(Embedding(len(vocabulary), 300))
model.compile('rmsprop', 'mse')

input_i = Input(shape=(30, 300))
encoded_h1 = Dense(64, activation='tanh')(input_i)
encoded_h2 = Dense(32, activation='tanh')(encoded_h1)
encoded_h3 = Dense(16, activation='tanh')(encoded_h2)
encoded_h4 = Dense(8, activation='tanh')(encoded_h3)
encoded_h5 = Dense(4, activation='tanh')(encoded_h4)
latent = Dense(2, activation='tanh')(encoded_h5)
decoder_h1 = Dense(4, activation='tanh')(latent)
decoder_h2 = Dense(8, activation='tanh')(decoder_h1)
decoder_h3 = Dense(16, activation='tanh')(decoder_h2)
decoder_h4 = Dense(32, activation='tanh')(decoder_h3)
decoder_h5 = Dense(64, activation='tanh')(decoder_h4)

output = Dense(300, activation='tanh')(decoder_h5)

autoencoder = Model(input_i,output)

autoencoder.compile('adadelta','mse')

X_embedded = model.predict(X_train)
autoencoder.fit(X_embedded,X_embedded,epochs=10, batch_size=256, validation_split=.1)

print autoencoder.summary()

想法取自Keras - Autoencoder for Text Analysis

那么,在训练之后(如果我做得正确的话)我应该如何运行每个句子的编码部分来获得特征表示?帮助表示赞赏。谢谢!

【问题讨论】:

标签: tensorflow machine-learning keras autoencoder


【解决方案1】:

为编码器制作独立模型

encoder=Model(input_i,latent)

假设 mnist 数据的代码应该是这样的--

encoder.predict(x_train[0]) 

这样你会得到latent_space向量作为输出

【讨论】:

    【解决方案2】:

    为此,请通过model.pop() 参阅“弹出”最后一层。使用model.pop() 训练“弹出”最后一层后,然后使用model.predict(X_train) 获取表示。

    https://keras.io/getting-started/faq/#how-can-i-remove-a-layer-from-a-sequential-model

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2019-08-22
      • 2019-08-05
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2023-03-12
      • 2017-07-19
      • 2021-09-11
      相关资源
      最近更新 更多