Keras - Autoencoder for Text Analysis

ぐ巨炮叔叔 提交于 2019-12-01 01:01:55

There are some issues with your question (e.g. what is weights, used in the Embedding & final Dense layers arguments?). Still, I think a simpler approach would be to disentangle the embedding and the autoencoding parts (they are independent), by building first a simple embedding model and then use its outputs (with predict) to feed your autoencoder. This way you don't have to define a custom loss (BTW, print statements in such functions are not a good idea).

Without knowing the details of your data, the following 2 models compile OK:

Embedding model (quick adaptation from the docs)

model = Sequential()
model.add(Embedding(1000, 64))
model.compile('rmsprop', 'mse')

Autoencoder:

input_i = Input(shape=(200,100))
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(100, activation='tanh')(decoder_h5)

autoencoder = Model(input_i,output)

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

After adapting the above models parameters to your case, this should work fine:

X_embedded = model.predict(X_train)
autoencoder.fit(X_embedded,X_embedded,epochs=10,
            batch_size=256, validation_split=.1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!