六、TextBiRNN

原理講解

TextBiRNN是基于TextRNN的改進版本,將網(wǎng)絡(luò)結(jié)構(gòu)中的RNN層改成雙向(Biderectional)的RNN層擅这,希望不僅能考慮正向編碼信息,也能考慮反向編碼的信息景鼠。

網(wǎng)絡(luò)結(jié)構(gòu)

textBiRNN.png

本文實現(xiàn)

textBiRNN實現(xiàn).png

定義網(wǎng)絡(luò)結(jié)構(gòu)

from tensorflow.keras import Input ,Model
from tensorflow.keras.layers import Embedding , Dense ,Dropout,Bidirectional , LSTM


class TextBiRNN(object):
    def __init__(self , maxlen , max_features , embedding_dims , class_num = 5 , last_activate = 'softmax'):
        self.maxlen = maxlen
        self.max_features = max_features
        self.embedding_dims = embedding_dims
        self.class_num  = class_num
        self.last_activate = last_activate

    def get_model(self):
        input = Input((maxlen , ))
        embedding = Embedding(self.max_features , self.embedding_dims , input_length = self.maxlen)(input)
        x = Bidirectional(LSTM(128))(embedding)

        output = Dense(self.class_num , activation = self.last_actvation)(x)
        model = Model(inputs = input , outputs = output)
        return model
from tensorflow.keras.proprecessing import sequence
import random
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.utils import to_categorical
from utils import *

#路徑配置
data_dir = './processed_data'
vocab_file = './vocab/vocab.txt'
vocab_size = 40000

#神經(jīng)網(wǎng)絡(luò)配置
max_features = 40001
maxlen = 400
batch_size = 32
embedding_dims = 50
epochs = 10

print('數(shù)據(jù)預(yù)處理與加載數(shù)據(jù)')
#如果詞匯表不存在仲翎,重建
if not os.path.exists(vocab_file):
    build_vocab(data_dir , vocab_file , vocab_size)
#獲得 詞匯/類別 與id的字典銀蛇
categories , cat_to_id = read_category()
words , word_to_id = read_vocab(vocab_file)

#全部數(shù)據(jù)
x , y = read_files(data_dir)
data = list(zip(x,y))
del x,y

#亂序
random.shuffle(data)

#切分?jǐn)?shù)據(jù)集和測試集
train_data , test_data = train_test_split(data)

#對文本的詞id和類別id進行編碼
x_train = encode_sentences([content[0] for content in train_data], word_to_id)
y_train = to_categorical(encode_cate([content[1] for content in train_data], cat_to_id))
x_test = encode_sentences([content[0] for content in test_data], word_to_id)
y_test = to_categorical(encode_cate([content[1] for content in test_data], cat_to_id))

print('對序列做padding,保證是 samples*timestep 的維度')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

print('構(gòu)建模型...')
model = TextBiRNN(maxlen, max_features, embedding_dims).get_model()
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])

print('Train...')
early_stopping = EarlyStopping(monitor='val_accuracy', patience=2, mode='max')
history = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          callbacks=[early_stopping],
          validation_data=(x_test, y_test))

print('Test...')
result = model.predict(x_test)
import matplotlib.pyplot as plt
plt.switch_backend('agg')
%matplotlib inline

fig1 = plt.figure()
plt.plot(history.history['loss'],'r',linewidth=3.0)
plt.plot(history.history['val_loss'],'b',linewidth=3.0)
plt.legend(['Training loss', 'Validation Loss'],fontsize=18)
plt.xlabel('Epochs ',fontsize=16)
plt.ylabel('Loss',fontsize=16)
plt.title('Loss Curves :CNN',fontsize=16)
fig1.savefig('loss_cnn.png')
plt.show()
fig2=plt.figure()
plt.plot(history.history['accuracy'],'r',linewidth=3.0)
plt.plot(history.history['val_accuracy'],'b',linewidth=3.0)
plt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=18)
plt.xlabel('Epochs ',fontsize=16)
plt.ylabel('Accuracy',fontsize=16)
plt.title('Accuracy Curves : CNN',fontsize=16)
fig2.savefig('accuracy_cnn.png')
plt.show()
from tensorflow.keras.utils import plot_model
plot_model(model, show_shapes=True, show_layer_names=True)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末铛漓,一起剝皮案震驚了整個濱河市溯香,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌浓恶,老刑警劉巖玫坛,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異包晰,居然都是意外死亡湿镀,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門伐憾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勉痴,“玉大人,你說我怎么就攤上這事树肃≌裘” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵胸嘴,是天一觀的道長雏掠。 經(jīng)常有香客問我,道長筛谚,這世上最難降的妖魔是什么磁玉? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮驾讲,結(jié)果婚禮上蚊伞,老公的妹妹穿的比我還像新娘席赂。我一直安慰自己,他們只是感情好时迫,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布颅停。 她就那樣靜靜地躺著,像睡著了一般掠拳。 火紅的嫁衣襯著肌膚如雪癞揉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天溺欧,我揣著相機與錄音喊熟,去河邊找鬼。 笑死姐刁,一個胖子當(dāng)著我的面吹牛芥牌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播聂使,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼壁拉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了柏靶?” 一聲冷哼從身側(cè)響起弃理,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎屎蜓,沒想到半個月后痘昌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡梆靖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年控汉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片返吻。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡姑子,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出测僵,到底是詐尸還是另有隱情街佑,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布捍靠,位于F島的核電站沐旨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏榨婆。R本人自食惡果不足惜磁携,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望良风。 院中可真熱鬧谊迄,春花似錦闷供、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至粮呢,卻和暖如春婿失,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背啄寡。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工豪硅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人这难。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓舟误,卻偏偏與公主長得像葡秒,于是被迫代替她去往敵國和親姻乓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容