tensorflow 2.0 下 bilstm + attention 实现文本分类 demo

代码如下:
需要注意一下几点:
1)利用 keras 里面的 layer 或者 variable, 尽量取一个名字,不然多个相同的 layer 出来, 跑的时候会报错
2)Bidirectional 必须一个正向 一个 反向
3)CategoricalCrossentropy loss fun 的输入参数不能写反了,事实上,写反了,这个函数不会报错,只会训练不出来,因为函数内部有个类型转换的,这个笔误很难发现
4)@tf.function 负责将函数转化为图模型,里面的部分会在 gpu 上跑,会加快速度

解释下这里 attention 的用法:
一般的解释见:


image.png

实际使用的时候:
query 假定是待求的参数,可以理解为 一个判断文章是什么分类的问题,
然后 key = value 为 bi-lstm 每一步的隐状态, 最终综合的结果是各个隐状态的对于
该 query 的加权求和,并最后加一个 dense 层,变为最终的分类结果

query => atten_u
attention layer 使用以下函数计算 key 和 query 的相似度

image.png

最终使用的加权结果为:


image.png
#!/usr/bin/env python 
#-*- coding:utf-8 -*-

import os
import sys
import warnings
import pickle
import datetime
import tensorflow as tf 
import pandas as pd
import traceback
import time 
import json
import numpy as np 
from tensorflow import keras 
from tensorflow.keras import layers
from tensorflow.keras import Input 
from tensorflow.keras.layers import Dense 
from tensorflow.keras.layers import LSTM 
from tensorflow.keras.layers import Bidirectional 
from tensorflow.keras.layers import Dropout 
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import BatchNormalization
warnings.filterwarnings("ignore")


####################  helper function #########################
def one_hot_encode(raw_y, num_classes):
  index = np.array(raw_y)
  class_cnt = num_classes #np.max(index) + 1 
  out = np.zeros((index.shape[0], class_cnt))
  out[np.arange(index.shape[0]), index] = 1
  return out 

def load_sample(fn, max_seq_len, word_dict, num_classes):
  text_df = pd.read_csv(fn)
  raw_y = []
  raw_x = []
  for i in range(len(text_df)):
    label = text_df['label'][i]
    raw_y.append(int(label))

    text = text_df['text'][i]
    text_len = len(text)
    x = np.zeros(max_seq_len, dtype = np.int32)
    if text_len <= max_seq_len:
      for i in range(text_len):
        x[i] = word_dict[text[i]]
    else:
      for i in range(text_len - max_seq_len, text_len):
        x[i - text_len + max_seq_len] = word_dict[text[i]]
    raw_x.append(x)

  all_x = np.array(raw_x)
  all_y = one_hot_encode(raw_y, num_classes)
  return all_x, all_y 

def batch_iter(x, y, batch_size = 16):
  data_len = len(x)
  num_batch = (data_len + batch_size - 1) // batch_size
  indices = np.random.permutation(np.arange(data_len))
  x_shuff = x[indices]
  y_shuff = y[indices]
  for i in range(num_batch):
    start_offset = i*batch_size 
    end_offset = min(start_offset + batch_size, data_len)
    yield i, num_batch, x_shuff[start_offset:end_offset], y_shuff[start_offset:end_offset]


######################### model start #####################
class RnnAttentionLayer(layers.Layer):
  def __init__(self, attention_size, drop_rate):
    super().__init__()
    self.attention_size = attention_size
    self.dropout = Dropout(drop_rate, name = "rnn_attention_dropout")

  def build(self, input_shape):
    self.attention_w = self.add_weight(name = "atten_w", shape = (input_shape[-1], self.attention_size), initializer = tf.random_uniform_initializer(), dtype = "float32", trainable = True)
    self.attention_u = self.add_weight(name = "atten_u", shape = (self.attention_size,), initializer = tf.random_uniform_initializer(), dtype = "float32", trainable = True)
    self.attention_b = self.add_weight(name = "atten_b", shape = (self.attention_size,), initializer = tf.constant_initializer(0.1), dtype = "float32", trainable = True)    
    super().build(input_shape)

  def call(self, inputs, training):
    x = tf.tanh(tf.add(tf.tensordot(inputs, self.attention_w, axes = 1), self.attention_b))
    x = tf.tensordot(x, self.attention_u, axes = 1)
    x = tf.nn.softmax(x)
    weight_out = tf.multiply(tf.expand_dims(x, -1), inputs)
    final_out = tf.reduce_sum(weight_out, axis = 1) 
    drop_out = self.dropout(final_out, training = training)
    return drop_out

class RnnLayer(layers.Layer):
  def __init__(self, rnn_size, drop_rate):
    super().__init__()
    fwd_lstm = LSTM(rnn_size, return_sequences = True, go_backwards= False, dropout = drop_rate, name = "fwd_lstm")
    bwd_lstm = LSTM(rnn_size, return_sequences = True, go_backwards = True, dropout = drop_rate, name = "bwd_lstm")
    self.bilstm = Bidirectional(merge_mode = "concat", layer = fwd_lstm, backward_layer = bwd_lstm, name = "bilstm")
    #self.bilstm = Bidirectional(LSTM(rnn_size, activation= "relu", return_sequences = True, dropout = drop_rate))

  def call(self, inputs, training):
    outputs = self.bilstm(inputs, training = training)
    return outputs
 
class Model(tf.keras.Model):
  def __init__(self, num_classes, drop_rate, vocab_size, embedding_size, rnn_size, attention_size):
    super().__init__()
    self.embedding_layer = Embedding(vocab_size, embedding_size, embeddings_initializer = "uniform", name = "embeding_0")
    self.rnn_layer = RnnLayer(rnn_size, drop_rate)
    self.attention_layer = RnnAttentionLayer(attention_size, drop_rate)
    self.dense_layer = Dense(num_classes, activation = "softmax", kernel_regularizer=keras.regularizers.l2(0.001), name = "dense_1")

  def call(self, input_x, training):
    x = self.embedding_layer(input_x)
    x = self.rnn_layer(x, training = training)
    x = self.attention_layer(x, training = training)
    x = self.dense_layer(x)
    return x

def train(xy_train, xy_val, num_classes, vocab_size, nbr_epoches, batch_size):
  uniq_cfg_name = datetime.datetime.now().strftime("%Y")
  model_prefix = os.path.join(os.getcwd(), "model")
  if not os.path.exists(model_prefix):
    print("create model dir: %s" % model_prefix)
    os.mkdir(model_prefix)

  model_path = os.path.join(model_prefix, uniq_cfg_name)
  model = Model(num_classes, drop_rate = 0.05, vocab_size = vocab_size, embedding_size = 256, rnn_size = 128, attention_size = 128)
  if os.path.exists(model_path):
    model.load_weights(model_path)
    print("load weight from: %s" % model_path)
  
  optimizer = tf.keras.optimizers.Adam(0.01)
  loss_fn = tf.keras.losses.CategoricalCrossentropy()

  loss_metric = tf.keras.metrics.Mean(name='train_loss')
  accuracy_metric = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy')

  @tf.function 
  def train_step(input_x, input_y, training = True):
    with tf.GradientTape() as tape:
      raw_prob = model(input_x, training)
      #tf.print("raw_prob", raw_prob)
      pred_loss = loss_fn(input_y, raw_prob)
    gradients = tape.gradient(pred_loss, model.trainable_variables)
    if training:
      optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    # Update the metrics
    loss_metric.update_state(pred_loss)
    accuracy_metric.update_state(input_y, raw_prob)
    return raw_prob 

  for i in range(nbr_epoches):
    t0 = time.time()
    batch_train = batch_iter(xy_train[0], xy_train[1], batch_size = batch_size)
    loss_metric.reset_states()
    accuracy_metric.reset_states()

    for batch_no, batch_tot, data_x, data_y in batch_train:
      predict_prob = train_step(data_x, data_y, True)  
      #if batch_no % 10 == 0:
      #  print("[%d of %d]: loss: %0.3f acc %0.3f" % (batch_no, batch_tot, loss_metric.result(), accuracy_metric.result()))

    print("[train ep %d] [%s]: %0.3f  [%s]: %0.3f" %  (i, "loss", loss_metric.result() , "acc", accuracy_metric.result()))
    model.save_weights(model_path, overwrite=True)

    if (i + 1) % 5 == 0:
      loss_metric.reset_states()
      accuracy_metric.reset_states()
      batch_test = batch_iter(xy_val[0], xy_val[1], batch_size = batch_size)
      for _, _, data_x, data_y in batch_test:
        train_step(data_x, data_y, False)
      print("[***** ep %d] [%s]: %0.3f  [%s]: %0.3f" %  (i, "loss", loss_metric.result() , "acc", accuracy_metric.result()))

if __name__ == "__main__":
  try:
    cur_dir=os.getcwd()
    corps_meta_path = os.path.join(cur_dir, "corps_meta")
    corps_meta = pickle.load(open(corps_meta_path, "rb"))
    max_seq_len = min(64, corps_meta["max_seq_len"])
    num_classes = corps_meta["num_classes"] 
    word_dict = corps_meta["word_dict"] 
    index_dict = corps_meta["index_dict"]
    train_sample_path = os.path.join(cur_dir, "train.csv")
    test_sample_path = os.path.join(cur_dir, "test.csv")

    ### gen samples ###
    train_x, train_y = load_sample(train_sample_path, max_seq_len, word_dict, num_classes)
    test_x, test_y = load_sample(test_sample_path, max_seq_len, word_dict, num_classes)
    key, freq = np.unique(np.argmax(train_y, axis = 1), return_counts = True)
    train([train_x, train_y], [test_x, test_y], num_classes, len(word_dict), nbr_epoches = 100, batch_size = 256)
  except:
    traceback.print_exc()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351

推荐阅读更多精彩内容