keras 的 example 文件 babi_memnn.py 解析
該代碼功能是實現一個閱讀理解的神經網絡,就是給一段材料,提一個問題,然后看是否能給出答案;
首先這個代碼有一個bug,在Python2下應該可以運行,但是在Python3下會報錯,
有人提交了pull request,https://github.com/keras-team/keras/pull/13519/commits/3fc48bcd9a9cc931c43cf4e9e63ae35b61af8910,
但是官方對這個工程已經不咋用心了,所以至今還未合并,
可以把第37行
return [x.strip() for x in re.split(r'(\W+)?', sent) if x.strip()]
中的問號去掉
?
數據集是 Facebook 的 babi 數據集,官方地址為?https://research.fb.com/downloads/babi/
我這人有一個毛病,就是看到一個名稱,總喜歡搞懂這個名稱本身是什么意思,不過搜了一下確實沒有搜到,只是看到一個非官方猜測?https://www.quora.com/What-does-bAbI-stand-for?, babi 這個名稱的含義:
babi,官方的叫法其實是?bAbI, 發音,和意思,都是baby,大致就是嬰兒學習的意思,而把baby,改為 bAbI,就是geek們把 AI 嵌入到了 baby 這個單詞中,因為 AI 這兩個字母刻意大寫,而其余字符刻意小寫
把數據集 https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz?下載之后 ,解壓縮,打開?qa1_single-supporting-fact_train.txt 就可以大致明白是怎么回事了,
如第一個示例
1 Mary moved to the bathroom.
2 John went to the hallway.
3 Where is Mary? bathroom 1
1和2是材料,3是問題和答案
從這個數據集中,自己給每個單詞和標點符號搞了一個編碼:
{'.': 1, '?': 2, 'Daniel': 3, 'John': 4, 'Mary': 5, 'Sandra': 6, 'Where': 7, 'back': 8, 'bathroom': 9, 'bedroom': 10, 'garden': 11, 'hallway': 12, 'is': 13, 'journeyed': 14, 'kitchen': 15, 'moved': 16, 'office': 17, 'the': 18, 'to': 19, 'travelled': 20, 'went': 21}
可以看到總共是21個編碼,再加上補齊的pad,數值為0,那總共就是22個編碼
?
然后編碼每個材料,問題,和答案:
(['Mary', 'moved', 'to', 'the', 'bathroom', '.', 'John', 'went', 'to', 'the', 'hallway', '.'], ['Where', 'is', 'Mary', '?'], 'bathroom')
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 5 16 19 18 9 1 4 21 19 18 12 1]
[ 7 13 5 2]
9
0代碼啥也沒有,pad的字符
輸入的shape
inputs_train shape: (10000, 68)
queries_train shape: (10000, 4)
answers_train shape: (10000,)
神經網絡結構
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 68) 0
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 4) 0
__________________________________________________________________________________________________
sequential_1 (Sequential) multiple 1408 input_1[0][0]
__________________________________________________________________________________________________
sequential_3 (Sequential) (None, 4, 64) 1408 input_2[0][0]
__________________________________________________________________________________________________
dot_1 (Dot) (None, 68, 4) 0 sequential_1[1][0]sequential_3[1][0]
__________________________________________________________________________________________________
activation_1 (Activation) (None, 68, 4) 0 dot_1[0][0]
__________________________________________________________________________________________________
sequential_2 (Sequential) multiple 88 input_1[0][0]
__________________________________________________________________________________________________
add_1 (Add) (None, 68, 4) 0 activation_1[0][0]sequential_2[1][0]
__________________________________________________________________________________________________
permute_1 (Permute) (None, 4, 68) 0 add_1[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 4, 132) 0 permute_1[0][0]sequential_3[1][0]
__________________________________________________________________________________________________
lstm_1 (LSTM) (None, 32) 21120 concatenate_1[0][0]
__________________________________________________________________________________________________
dropout_4 (Dropout) (None, 32) 0 lstm_1[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 22) 726 dropout_4[0][0]
__________________________________________________________________________________________________
activation_2 (Activation) (None, 22) 0 dense_1[0][0]
==================================================================================================
Total params: 24,750
Trainable params: 24,750
Non-trainable params: 0
__________________________________________________________________________________________________
——————————————————————
總目錄
keras的example文件解析
總結
以上是生活随笔為你收集整理的keras 的 example 文件 babi_memnn.py 解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keras 的 example 文件 a
- 下一篇: keras 的 example 文件 b