简体   繁体   English

如何使用Keras培训的嵌入式层?

[英]How to use a Keras trained Embedded layer?

My model is: 我的模型是:

        model = Sequential()
        model.add(Embedding(input_dim=vocab_size,
                            output_dim=1024, input_length=self.SEQ_LENGTH))

        model.add(LSTM(vocab_size))

        model.add(Dropout(rate=0.5))
        model.add(Dense(vocab_size - 1, activation='softmax'))

And I have it trained. 而且我训练有素。 But now during inference time, how can I use that embedding? 但是现在在推理期间,我如何使用该嵌入?

Your question is solved here . 您的问题在这里已解决。 As skeleton you can use this code: 作为骨架,您可以使用以下代码:

from tensorflow.python.keras.preprocessing.text import Tokenizer

tokenizer_obj = Tokenizer()
tokenizer_obj.fit_on_texts(your_dataset) 

...

max_length = max_number_words
X_test_tokens = tokenizer_obj.texts_to_sequences(X_test)
X_test_pad = pad_sequences(X_test_tokens, maxlen=max_length, padding='post')

score, acc = model.evaluate(X_test_pad, y_test, batch_size=128)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用训练有素的模型层在keras中创建另一个模型 - Use trained model layer in creating another model in keras Keras - 获得训练层的重量 - Keras - get weight of trained layer 在Keras中进行归一化或辍学训练时,如何预测? - How to do predict when trained with Normalization or dropout layer in Keras? 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras? 如何在 keras 和 python 中保存和使用经过训练的模型 - How to save and use a trained model in keras and python 如何在 Keras 中保存经过训练的模型以在应用程序中使用它? - How to save a trained model in Keras to use it in an application? 如何使用 Wiki:Fasttext.vec 和 Google News:Word2vec.bin 预训练文件作为 Keras 嵌入层的权重 - How to use Wiki: Fasttext.vec and Google News: Word2vec.bin pre trained files as weights for Keras Embedding layer 如何使用来自一个预先训练的MLP的最后一个隐藏层权重作为Keras的新MLP(转移学习)的输入? - How to use the last hidden layer weights from one pre-trained MLP as input to a new MLP (transfer learning) with Keras? 如何在keras中使用lambda层? - How to use lambda layer in keras? 如何使用从训练过的keras模型中提取的张量流模型 - How to use a tensorflow model extracted from a trained keras model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM