简体   繁体   English

加载 keras model 的结果不同

[英]Results of loaded keras model are different

I am trying to build a LSTM model in order to detect sentiment of texts.我正在尝试构建一个 LSTM model 来检测文本的情绪。 (0 -> normal, 1 -> hateful)After I trained my model, I send some texts to my model for prediction. (0 -> 正常,1 -> 可恨)在我训练了 model 之后,我将一些文本发送到我的 model 进行预测。 The predicted results are as I expected.预测的结果和我预期的一样。 However, after I load my model as "h5" file, I cannot get same accuracies even if I send same texts.但是,在我将 model 加载为“h5”文件后,即使发送相同的文本,我也无法获得相同的准确性。 Here is my training codes:这是我的培训代码:


    texts = tweets['text']
    labels = tweets['label']

    labels = LabelEncoder().fit_transform(labels)
    labels = labels.reshape(-1, 1)

   
    X_train, X_test, Y_train, Y_test = train_test_split(texts, labels, test_size=0.20)

    tokenizer.fit_on_texts(X_train)
    sequences = tokenizer.texts_to_sequences(X_train)
    sequences_matrix = sequence.pad_sequences(sequences, maxlen=max_len)

    inputs = Input(name='inputs', shape=[max_len])
    layer = Embedding(max_words, 50, input_length=max_len)(inputs)
    layer = LSTM(64)(layer)
    layer = Dense(256, name='FC1')(layer)
    layer = Activation('relu')(layer)
    layer = Dropout(0.5)(layer)
    layer = Dense(1, name='out_layer')(layer)
    layer = Activation('sigmoid')(layer)
    model = Model(inputs=inputs, outputs=layer)

    earlyStopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, 
                                  restore_best_weights=False)

    model.summary()
    model.compile(loss='binary_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])
    model.fit(sequences_matrix, Y_train, batch_size=128, shuffle=True, epochs=10,
              validation_split=0.2, callbacks=[earlyStopping])

 
    model.save("ModelsDL/LSTM.h5")


    test_sequences = tokenizer.texts_to_sequences(X_test)
    test_sequences_matrix = sequence.pad_sequences(test_sequences, maxlen=max_len)

    accr = model.evaluate(test_sequences_matrix, Y_test)

    print('Test set\n  Loss: {:0.3f}\n  Accuracy: {:0.3f}'.format(accr[0], accr[1]))


    texts = ["hope", "feel relax", "feel energy", "peaceful day"]

    tokenizer.fit_on_texts(texts)
    test_samples_token = tokenizer.texts_to_sequences(texts)
    test_samples_tokens_pad = pad_sequences(test_samples_token, maxlen=max_len)

    print(model.predict(x=test_samples_tokens_pad))

    del model

The output of print(model.predict(x=test_samples_tokens_pad)) is: print(model.predict(x=test_samples_tokens_pad))的 output 是:

 [[0.0387207 ]
 [0.02622151]
 [0.3856796 ]
 [0.03749594]]

Text with "normal" sentiment results closer to 0.Also text with "hateful" sentiment results closer to 1.具有“正常”情绪的文本结果更接近 0。具有“仇恨”情绪的文本结果更接近 1。

As you see in the output, my results are consistent because they have "normal" sentiment.正如您在 output 中看到的那样,我的结果是一致的,因为它们具有“正常”的情绪。

However, after I load my model, I always encounter different results.但是,在我加载 model 后,我总是遇到不同的结果。 Here is my codes:这是我的代码:

texts = ["hope", "feel relax", "feel energy", "peaceful day"] # same texts

model = load_model("ModelsDL/LSTM.h5")
tokenizer.fit_on_texts(texts)
test_samples_token = tokenizer.texts_to_sequences(texts)
test_samples_tokens_pad = pad_sequences(test_samples_token, maxlen=max_len)

print(model.predict(x=test_samples_tokens_pad))

Output of print(model.predict(x=test_samples_tokens_pad)) : Output print(model.predict(x=test_samples_tokens_pad))

[[0.9838583 ]
 [0.99957573]
 [0.9999665 ]
 [0.9877912 ]]

As you notice, The same LSTM model treated the texts as if they had a hateful context.如您所见,相同的 LSTM model 将文本视为具有可恨的上下文。

What should I do for this problem?这个问题我该怎么办?

EDIT: I solved the problem.编辑:我解决了这个问题。 I saved the tokenizer which is used while model training.我保存了 model 训练时使用的分词器。 Then, I loaded that tokenizer before tokenizer.fit_on_texts(texts) for predicted texts.然后,我在tokenizer.fit_on_texts(texts)之前为预测文本加载了该标记器。

On your test train split code you need to give a random state to get similar results.For example;在您的测试列车拆分代码中,您需要提供一个随机的 state 以获得类似的结果。例如; X_train, X_test, Y_train, Y_test = train_test_split(texts, labels, test_size=0.20,random_state=15). X_train,X_test,Y_train,Y_test = train_test_split(文本,标签,test_size=0.20,random_state=15)。 Try every state like 1,2,3,4....Once you get the result you like then you can save it and use after with same random state.Hope it would solve your problem.尝试每一个 state 像 1,2,3,4....一旦你得到你喜欢的结果然后你可以保存它并使用相同的随机 state。希望它能解决你的问题。

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

相关问题 tensorflowjs 和 keras 在相同模型和张量上的不同结果 - Different results for tensorflowjs and keras on same model and tensor 在Google Cloud上训练不同的Keras模型的结果 - Results of training a Keras model different on Google Cloud 每个训练keras模型后的结果都不同 - The results after each training keras model are different 训练有素的Keras序列模型给出了不同的结果 - Trained and Loaded Keras Sequential Model is giving different result Keras 模型分类为每个模型重新编译返回不同的结果 - Keras Model classifying returning different results for each model recompiling Python:Keras 模型对于相同的数据和相同的模型返回不同的结果 - Python: Keras model returns different results for the same data and same model Keras model.predict给model.evalute提供了不同的结果 - Keras model.predict gives different results to model.evalute 混淆矩阵在 Keras model tf==2.3.0 中产生不同的结果 - Confusion Matrix produces different results in Keras model tf==2.3.0 用自定义层加载Keras中保存的模型,预测结果不一样? - Loading a saved model in Keras with a custom layer and prediction results are different? Keras LSTM - 为什么“相同”模型和相同权重的结果不同? - Keras LSTM - why different results with “same” model & same weights?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM