【问题标题】:different prediction after load a model in keras在 keras 中加载模型后的不同预测
【发布时间】:2019-07-11 16:13:43
【问题描述】:

我有一个在 Keras 中构建的序列模型,经过训练后它给了我很好的预测,但是当我保存然后加载模型时,我没有在同一个数据集上获得相同的预测。为什么? 请注意,我检查了模型的权重,它们与模型的架构相同,使用 model.summary() 和 model.getWeights() 检查。我认为这很奇怪,我不知道如何处理这个问题。 我没有任何错误,但预测不同

  1. 我尝试使用 model.save() 和 load_model()

  2. 我尝试使用 model.save_weights(),然后重新构建模型,然后加载模型

我对这两个选项都有同样的问题。

def Classifier(input_shape, word_to_vec_map, word_to_index, emb_dim, num_activation):

    sentence_indices = Input(shape=input_shape, dtype=np.int32)
    emb_dim = 300  # embedding di 300 parole in italiano
    embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index, emb_dim)

    embeddings = embedding_layer(sentence_indices)   

    X = LSTM(256, return_sequences=True)(embeddings)
    X = Dropout(0.15)(X)
    X = LSTM(128)(X)
    X = Dropout(0.15)(X)
    X = Dense(num_activation, activation='softmax')(X)

    model = Model(sentence_indices, X)

    sequentialModel = Sequential(model.layers)    
    return sequentialModel

    model = Classifier((maxLen,), word_to_vec_map, word_to_index, maxLen, num_activation)
    ...
    model.fit(Y_train_indices, Z_train_oh, epochs=30, batch_size=32, shuffle=True)

    # attempt 1
    model.save('classificationTest.h5', True, True)
    modelRNN = load_model(r'C:\Users\Alessio\classificationTest.h5')

    # attempt 2
    model.save_weights("myWeight.h5")

    model = Classifier((maxLen,), word_to_vec_map, word_to_index, maxLen, num_activation)
    model.load_weights(r'C:\Users\Alessio\myWeight.h5') 

    # PREDICTION TEST
    code_train, category_train, category_code_train, text_train = read_csv_for_email(r'C:\Users\Alessio\Desktop\6Febbraio\2test.csv')

    categories, code_categories = get_categories(r'C:\Users\Alessio\Desktop\6Febbraio\2test.csv')

    X_my_sentences = text_train
    Y_my_labels = category_code_train
    X_test_indices = sentences_to_indices(X_my_sentences, word_to_index, maxLen)
    pred = model.predict(X_test_indices)

    def codeToCategory(categories, code_categories, current_code):

        i = 0;
        for code in code_categories:
            if code == current_code:
                return categories[i]
            i = i + 1 
        return "no_one_find"   

    # result
    for i in range(len(Y_my_labels)):
        num = np.argmax(pred[i])

    # Pretrained embedding layer
    def pretrained_embedding_layer(word_to_vec_map, word_to_index, emb_dim):
    """
    Creates a Keras Embedding() layer and loads in pre-trained GloVe 50-dimensional vectors.

    Arguments:
    word_to_vec_map -- dictionary mapping words to their GloVe vector representation.
    word_to_index -- dictionary mapping from words to their indices in the vocabulary (400,001 words)

    Returns:
    embedding_layer -- pretrained layer Keras instance
    """

    vocab_len = len(word_to_index) + 1                  # adding 1 to fit Keras embedding (requirement)

    ### START CODE HERE ###
    # Initialize the embedding matrix as a numpy array of zeros of shape (vocab_len, dimensions of word vectors = emb_dim)
    emb_matrix = np.zeros((vocab_len, emb_dim))

    # Set each row "index" of the embedding matrix to be the word vector representation of the "index"th word of the vocabulary
    for word, index in word_to_index.items():
        emb_matrix[index, :] = word_to_vec_map[word]

    # Define Keras embedding layer with the correct output/input sizes, make it trainable. Use Embedding(...). Make sure to set trainable=False. 
    embedding_layer = Embedding(vocab_len, emb_dim)
    ### END CODE HERE ###

    # Build the embedding layer, it is required before setting the weights of the embedding layer. Do not modify the "None".
    embedding_layer.build((None,))

    # Set the weights of the embedding layer to the embedding matrix. Your layer is now pretrained.
    embedding_layer.set_weights([emb_matrix])

    return embedding_layer

你有什么建议吗?

提前致谢。

Edit1:如果在同一个“页面”中使用保存和加载代码(我使用的是笔记本 jupyter),它可以正常工作。如果我更改“页面”,它将不起作用。会不会是跟tensorflow session 有关系?

Edit2:我的最终目标是加载一个模型,在 Keras 中训练,在 java 中使用 Deeplearning4J。因此,如果您知道将 keras 模型“转换”为其他在 DL4J 中可读的解决方案,无论如何都会有所帮助。

Edit3:添加函数 pretrained_embedding_layer()

Edit4:使用 gensim 读取 word2Vec 模型中的字典

from gensim.models import Word2Vec
model = Word2Vec.load('C:/Users/Alessio/Desktop/emoji_ita/embedding/glove_WIKI')

def getMyModels (model):
word_to_index = dict({})
index_to_word = dict({})
word_to_vec_map = dict({})
for idx, key in enumerate(model.wv.vocab):
    word_to_index[key] = idx
    index_to_word[idx] = key
    word_to_vec_map[key] = model.wv[key]
return word_to_index, index_to_word, word_to_vec_map

【问题讨论】:

  • 您可以尝试在加载保存的模型之前清除 tf 会话 from keras import backend as K K.clear_session()
  • 我收到“未初始化变量”的错误

标签: python tensorflow keras neural-network deeplearning4j


【解决方案1】:

您在加载模型时是否以相同的方式预处理数据?

如果是,您是否设置了预处理功能的种子? 如果你用 keras 建一个字典,句子的顺序是一样的吗?

【讨论】:

  • 对于 pretrained_embedding_layer,我使用 gensim 加载 wor2vec 模型,然后从 word2Vec 模型为 word_to_index、index_to_word、word_to_vec_map 创建字典。将我的预训练数据放在 json 文件中应该是一个解决方案吗?但是,如果我使用 load_model(),这个函数甚至应该是预训练的参数吗?
  • 您应该检查您的词典是否仍然相同。如果没有,请务必重现完全相同的步骤,并在需要时设置随机种子。如果您只想使用相同的集合进行测试,您可以使用 pickle 保存您的 X 集合并与您的模型同时加载。
【解决方案2】:

我之前也遇到过同样的问题,所以这里是你的解决方法。确保权重和摘要相同后,尝试打印随机种子并检查。如果它的值从一个会话更改为另一个会话,并且您尝试了 tensorflow 的种子,则意味着您需要禁用 PYTHONHASHSEED 环境变量。你可以在这里读更多关于它的内容: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED

要禁用它,请转到您的系统环境变量并添加 PYTHONHASHSEED 作为新变量(如果它不存在)。然后,将其值设置为 0 以禁用它。请注意,这样做是因为必须在运行解释器之前禁用它。

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 2018-06-30
    • 1970-01-01
    • 2022-12-04
    • 2017-07-16
    • 2018-04-15
    • 1970-01-01
    • 2019-01-24
    • 2021-07-31
    相关资源
    最近更新 更多