培训和评估keras LSTM模型中的准确性不同

时间:2018-11-02 09:55:33

标签: python-3.x keras pycharm lstm

我正在使用keras训练LSTM模型。训练训练集的准确度为:最后一个时期为83%,但是使用相同的训练集评估或预测模型的准确性为:47%。我不知道问题所在。 另外,由于我将模型和权重保存在磁盘上,因此我在以下代码中注释了训练部分,但是在训练模型(例如model.fit(...)等)之前就用过它

import pandas as pd
import Preprocessing as pre
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.utils import shuffle
import pickle
import numpy as np
import sys
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.layers import LSTM
from keras.preprocessing.sequence import pad_sequences
from keras.models import model_from_json
from keras.preprocessing.text import Tokenizer

df = pd.read_csv('tweets.csv',header=None,encoding = "ISO-8859-1")
df=shuffle(df)
length=df.size

train=[]
test=[]
Y=[]
Y2=[]

count=450000
for a in range(450000):   #loading data
    # b=pre.preprocess_tweet(df[1][a])
    label=df[0][a]
    # train.append(b)
    Y.append(label)
    count-=1
    print("Loading training data...",  count)

# with open('training_data_no_stopwords.obj', 'wb') as fp:
#     pickle.dump(train, fp)
with open ('training_data.obj', 'rb') as fp:
    train = pickle.load(fp)

count=156884
for a in range(450000,606884):   #loading testin data
    # b = pre.preprocess_tweet(df[1][a])
    label=df[0][a]
    # test.append(b)
    Y2.append(label)
    count-=1
    print("Loading testing data...",  count)

# with open('testing_data_no_stopwords.obj', 'wb') as fp:
#     pickle.dump(test, fp)

with open ('testing_data.obj', 'rb') as fp:
    test = pickle.load(fp)

# vectorizer = CountVectorizer(analyzer = "word",tokenizer = None, preprocessor = None, stop_words = None, max_features = 3500)
# # # fit_transform() does two functions: First, it fits the model
# # # and learns the vocabulary; second, it transforms our training data
# # # into feature vectors. The input to fit_transform should be a list of
# # # strings.
#
# train = vectorizer.fit_transform(train)
# test = vectorizer.transform(test)
tokenizer = Tokenizer(split=' ')
tokenizer.fit_on_texts(train)
train = tokenizer.texts_to_sequences(train)
max_words = 134
train = pad_sequences(train, maxlen=max_words)
tokenizer.fit_on_texts(test)
test = tokenizer.texts_to_sequences(test)
test = pad_sequences(test, maxlen=max_words)

print ('Extracting features & training batches')

print("Training...")
embedding_size=32
model = Sequential()
model.add(Embedding(606884, 32, input_length=134))
model.add(Dropout(0.4))
model.add(LSTM(128))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# batch_size = 501
# num_epochs = 3
#
# model.fit(train, np.array(Y),  batch_size=batch_size, epochs=num_epochs ,validation_split=0.2,shuffle=True)

# # Save the weights
# model.save_weights('LSTM_model_weights.h5')
#
# # Save the model architecture
# with open('LSTM_model.json', 'w') as f:
#     f.write(model.to_json())

# Model reconstruction from JSON file
with open('LSTM_model.json', 'r') as f:
    model = model_from_json(f.read())

# Load weights into the new model
model.load_weights('LSTM_model_weights.h5')
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

scores = model.evaluate(train, np.array(Y))
print('Test accuracy:', scores[1])

1 个答案:

答案 0 :(得分:0)

也许可以尝试使用Stratified K-Fold进行训练。这可能是因为您的测试数据与训练数据有很大不同。另外,您可以尝试在训练测试拆分之前对数据进行改组。