麻烦大神帮忙看一下:
(1)为何返回不了Precise, Recall, F1-socre值?
(2)为何在CNN前加了self-attention层,训练后的acc反而降低在0.78上下?
【研一小白求详解,万分感谢大神】
import os #导入os模块,用于确认文件是否存在
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.callbacks import Callback
from sklearn.metrics import f1_score, precision_score, recall_score
maxlen = 380#句子长截断为100
training_samples = 20000#在 200 个样本上训练
validation_samples = 5000#在 10 000 个样本上验证
max_words = 10000#只考虑数据集中前 10 000 个最常见的单词
def dataProcess():
imdb_dir = 'data/aclImdb'#基本路径,经常要打开这个
#处理训练集
train_dir = os.path.join(imdb_dir, 'train')#添加子路径
train_labels = []
train_texts = []
for label_type in ['neg', 'pos']:
dir_name = os.path.join(train_dir, label_type)
for fname in os.listdir(dir_name):#获取目录下所有文件名字
if fname[-4:] == '.txt':
f = open(os.path.join(dir_name, fname),'r',encoding='utf8')
train_texts.append(f.read())
f.close()
if label_type == 'neg':
train_labels.append(0)
else:train_labels.append(1)
#处理测试集
test_dir = os.path.join(imdb_dir, 'test')
test_labels = []
test_texts = []
for label_type in ['neg', 'pos']:
dir_name = os.path.join(test_dir, label_type)
for fname in sorted(os.listdir(dir_name)):
if fname[-4:] == '.txt':
f = open(os.path.join(dir_name, fname),'r',encoding='utf8')
test_texts.append(f.read())
f.close()
if label_type == 'neg':
test_labels.append(0)
else:
test_labels.append(1)
#对数据进行分词和划分训练集和数据集
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(train_texts)#构建单词索引结构
sequences = tokenizer.texts_to_sequences(train_texts)#整数索引的向量化模型
word_index = tokenizer.word_index#索引字典
print('Found %s unique tokens.' % len(word_index))
data = pad_sequences(sequences, maxlen=maxlen)
train_labels = np.asarray(train_labels)#把列表转化为数组
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', train_labels.shape)
indices = np.arange(data.shape[0])#评论顺序0,1,2,3
np.random.shuffle(indices)#把评论顺序打乱3,1,2,0
data = data[indices]
train_labels = train_labels[indices]
x_train = data[:training_samples]
y_train = train_labels[:training_samples]
x_val = data[training_samples: training_samples + validation_samples]
y_val = train_labels[training_samples: training_samples + validation_samples]
#同样需要将测试集向量化
test_sequences = tokenizer.texts_to_sequences(test_texts)
x_test = pad_sequences(test_sequences, maxlen=maxlen)
y_test = np.asarray(test_labels)
return x_train,y_train,x_val,y_val,x_test,y_test,word_index
embedding_dim = 100#特征数设为100
#"""将预训练的glove词嵌入文件,构建成可以加载到embedding层中的嵌入矩阵"""
def load_glove(word_index):#导入glove的词向量
embedding_file='data/glove.6B'
embeddings_index={}#定义字典
f = open(os.path.join(embedding_file, 'glove.6B.100d.txt'),'r',encoding='utf8')
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
# """转化为矩阵:构建可以加载到embedding层中的嵌入矩阵,形为(max_words(单词数), embedding_dim(向量维数)) """
embedding_matrix = np.zeros((max_words, embedding_dim))
for word, i in word_index.items():#字典里面的单词和索引
if i >= max_words:continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
return embedding_matrix
if __name__ == '__main__':
x_train, y_train, x_val, y_val,x_test,y_test, word_index = dataProcess()
embedding_matrix=load_glove(word_index)
#可以把得到的嵌入矩阵保存起来,方便后面fine-tune"""
# #保存
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation,Flatten
from keras.layers.recurrent import LSTM
from keras.layers import Embedding
from keras.layers import Bidirectional
from keras.layers import Conv1D, MaxPooling1D
import keras
from keras_self_attention import SeqSelfAttention
model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(SeqSelfAttention(attention_activation='sigmod'))
model.add(Conv1D(filters = 64, kernel_size = 5, padding = 'same', activation = 'relu'))
model.add(MaxPooling1D(pool_size = 4))
model.add(Dropout(0.25))
model.add(Bidirectional(LSTM(64,activation='tanh',dropout=0.2,recurrent_dropout=0.2)))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
val_targ = self.validation_data[1]
_val_f1 = f1_score(val_targ, val_predict)
_val_recall = recall_score(val_targ, val_predict)
_val_precision = precision_score(val_targ, val_predict)
self.val_f1s.append(_val_f1)
self.val_recalls.append(_val_recall)
self.val_precisions.append(_val_precision)
return
metrics = Metrics()
history = model.fit(x_train, y_train,
epochs=10,
batch_size=32,
validation_data=(x_val, y_val),
callbacks=[metrics])
model.save_weights('pre_trained_glove_model.h5')#保存结果