【问题标题】:How I can save my own trained word embedding model in the same format of Google word2vec and Glove saved using Keras?如何以与使用 Keras 保存的 Google word2vec 和 Glove 相同的格式保存我自己训练的词嵌入模型?
【发布时间】:2020-02-22 23:42:42
【问题描述】:

我在我的特定数据集上训练了自己的词嵌入模型,我想保存模型以供以后对未知数据集进行分类。但是,我尝试使用不同的格式保存模型,例如 (.txt,.mdl,.bin)。但是当我打开文件时,数据似乎有奇怪的字符。如何以与 Glove 和 word2vec 相同的格式保存我的模型,单词后跟它的向量..?

                ��������غe     ��������        `              �       �                                     TREE   ����������������                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      HEAP    X       0       �              model_weights   optimizer_weights              (                                     �       �       H        keras_version                                          @        backend                          
              H     
   model_config                             d                  `�a     h                         �      �       TREE   ����������������        p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             HEAP    X       0       �              embedding_1     flatten_1       dense_1        (                                     `      �       `        layer_names                             embedding_1flatten_1  dense_1            @        backend                          
                   H      h                                               GCOL                        2.2.5       

这是我从https://machinelearningmastery.com/use-word-embedding-layers-deep-learning-keras/#comment-507619 运行的代码:

from numpy import array
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.embeddings import Embedding
# define documents
docs = ['Well done!',
        'Good work',
        'Great effort',
        'nice work',
        'Excellent!',
        'Weak',
        'Poor effort!',
        'not good',
        'poor work',
        'Could have done better.']
# define class labels
labels = array([1,1,1,1,1,0,0,0,0,0])
# integer encode the documents
vocab_size = 50
encoded_docs = [one_hot(d, vocab_size) for d in docs]
print(encoded_docs)
# pad documents to a max length of 4 words
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)
# define the model
model = Sequential()
model.add(Embedding(vocab_size, 8, input_length=max_length))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# summarize the model
print(model.summary())
# fit the model
model.fit(padded_docs, labels, epochs=50, verbose=0)
# evaluate the model
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print('Accuracy: %f' % (accuracy*100))

【问题讨论】:

    标签: keras model save


    【解决方案1】:

    我只是用 Gensim 用的这个

    model.wv.save_word2vec_format('myModel.txt', binary=False)
    

    它给了我单词,然后是它的向量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 2020-11-05
      • 2016-06-11
      • 2018-02-04
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多