aqdydw 2019-06-27 08:08 采纳率: 0%
浏览 1529

keras下self-attention和Recall, F1-socre值实现问题?

麻烦大神帮忙看一下:
(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')#保存结果
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-29 05:58
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接