Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的LSTM和预训练word2vec的精确度很低?_Python_Deep Learning_Lstm_Tf.keras_Word Embedding - Fatal编程技术网

Python 为什么我的LSTM和预训练word2vec的精确度很低?

Python 为什么我的LSTM和预训练word2vec的精确度很低?,python,deep-learning,lstm,tf.keras,word-embedding,Python,Deep Learning,Lstm,Tf.keras,Word Embedding,我正在研究一个评论分类模型,其中只有两个类别0(负面)和1(正面)。我正在使用预先训练过的word2vec,它来自于谷歌的LSTM。问题是我得到了大约50%的准确度,根据本文,准确度应该在83%左右。我尝试了许多不同的超参数组合,但仍然获得了可怕的精度。我还尝试改变数据预处理技术并尝试了词干分析,但没有解决问题 这是我的密码 X, y = read_data() X = np.array(clean_text(X)) #apply data preprocessing tokenizer =

我正在研究一个评论分类模型,其中只有两个类别0(负面)和1(正面)。我正在使用预先训练过的word2vec,它来自于谷歌的LSTM。问题是我得到了大约50%的准确度,根据本文,准确度应该在83%左右。我尝试了许多不同的超参数组合,但仍然获得了可怕的精度。我还尝试改变数据预处理技术并尝试了词干分析,但没有解决问题

这是我的密码

X, y = read_data()
X = np.array(clean_text(X)) #apply data preprocessing  
tokenizer = Tokenizer()
tokenizer.fit_on_texts(X)

#converts text to sequence and add padding zeros
sequence = tokenizer.texts_to_sequences(X)
X_data = pad_sequences(sequence, maxlen = length, padding = 'post')

X_train, X_val, y_train, y_val = train_test_split(X_data, y, test_size = 0.2)

#Load the word2vec model
word2vec = KeyedVectors.load_word2vec_format(EMBEDDING_FILE, binary=True)

word_index = tokenizer.word_index
nb_words = min(MAX_NB_WORDS, len(word_index))+1

embedding_matrix = np.zeros((nb_words, EMBEDDING_DIM))
null_words = []
for word, i in word_index.items():
    if word in word2vec.wv.vocab:
        embedding_matrix[i] = word2vec.word_vec(word)
    else:
        null_words.append(word)

embedding_layer = Embedding(embedding_matrix.shape[0], # or len(word_index) + 1
                            embedding_matrix.shape[1], # or EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=701,
                            trainable=False)

model = Sequential()
model.add(embedding_layer)
model.add(LSTM(100))
model.add(Dropout(0.4))
model.add(Dense(2, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(X_train, y_train, batch_size=32, epochs=2, validation_data=(X_val, y_val), workers = -1, verbose=1)

score, acc = model.evaluate(X_val, y_val, batch_size=64)
我还尝试了其他优化器,如AdaMax和MSLE损失函数,无论我如何增加历元或更改批量大小,精度都不会变得更好。如果问题不在模型和预处理上,我真的很困惑,它会在哪里?谢谢

我注意到的几件事

为什么要使用
trainable=False
限制您的模型,使模型无法微调嵌入。使用固定的嵌入集学习问题比使用可训练的嵌入集困难。因此,请尝试设置
trainable=True

embedding_layer = Embedding(embedding_matrix.shape[0], # or len(word_index) + 1
                            embedding_matrix.shape[1], # or EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=701,
                            trainable=False)
第二个问题是,您使用的是2个单位,具有sigmoid激活和
二进制交叉熵。这种组合不起作用。你有两个选择

model = Sequential()
...
model.add(Dense(2, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
选择1 如果选择此选项,请注意标签需要是
[样本大小,1]
形状

选择2
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])