keras 的 example 文件 babi_rnn.py 解析
生活随笔
收集整理的這篇文章主要介紹了
keras 的 example 文件 babi_rnn.py 解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該代碼的目的和?https://blog.csdn.net/zhqh100/article/details/105193991?類似
數據集也是同一個數據集,只不過這個是從?qa2_two-supporting-facts_train.txt 中獲取的文本,文本量會大一些
第一個示例
1 Mary moved to the bathroom.
2 Sandra journeyed to the bedroom.
3 Mary got the football there.
4 John went to the kitchen.
5 Mary went back to the kitchen.
6 Mary went back to the garden.
7 Where is the football? garden 3 6
單詞映射為:
{'.': 1, '?': 2, 'Daniel': 3, 'John': 4, 'Mary': 5, 'Sandra': 6, 'Where': 7, 'apple': 8, 'back': 9, 'bathroom': 10, 'bedroom': 11, 'discarded': 12, 'down': 13, 'dropped': 14, 'football': 15, 'garden': 16, 'got': 17, 'grabbed': 18, 'hallway': 19, 'is': 20, 'journeyed': 21, 'kitchen': 22, 'left': 23, 'milk': 24, 'moved': 25, 'office': 26, 'picked': 27, 'put': 28, 'the': 29, 'there': 30, 'to': 31, 'took': 32, 'travelled': 33, 'up': 34, 'went': 35}
上面的材料編碼后為:
[ 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 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 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 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 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 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 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 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 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 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 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 5 25 31 29 10 1 6 21 31 29 11 1 5 1729 15 30 1 4 35 31 29 22 1 5 35 9 31 29 22 1 5 35 9 31 29 16 1]
[ 7 20 29 15 2]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
這里把ans進行了one-hot編碼,所以 loss 用的是?categorical_crossentropy,而?babi_memnn.py 用的是?sparse_categorical_crossentropy,所以不用進行one-hot編碼
訓練數據shape
x.shape = (1000, 552)
xq.shape = (1000, 5)
y.shape = (1000, 36)
神經網絡結構:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 552) 0
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 5) 0
__________________________________________________________________________________________________
embedding_1 (Embedding) (None, 552, 50) 1800 input_1[0][0]
__________________________________________________________________________________________________
embedding_2 (Embedding) (None, 5, 50) 1800 input_2[0][0]
__________________________________________________________________________________________________
lstm_1 (LSTM) (None, 100) 60400 embedding_1[0][0]
__________________________________________________________________________________________________
lstm_2 (LSTM) (None, 100) 60400 embedding_2[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 200) 0 lstm_1[0][0]lstm_2[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 36) 7236 concatenate_1[0][0]
==================================================================================================
Total params: 131,636
Trainable params: 131,636
Non-trainable params: 0
__________________________________________________________________________________________________
——————————————————————
總目錄
keras的example文件解析
總結
以上是生活随笔為你收集整理的keras 的 example 文件 babi_rnn.py 解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keras 的 example 文件 b
- 下一篇: keras 的 example 文件 c