-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathChapter-5-02-Attention.py
44 lines (35 loc) · 1.08 KB
/
Chapter-5-02-Attention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# =============================================================================
# Examples from Eugene Charniak's Introduction to Deep Learning 2018 MIT Press
# =============================================================================
#CHAPTER 5 P105 - Attention Example
import tensorflow as tf
bsZ=2
wSz=3
rnnSz=3
#Pretend Encoder Output
eo =( (( 1, 2, 3, 4 ),
( 1, 1, 1, 1 ),
( 1, 1, 1, 1 ),
( -1, 0,-1, 0)),
(( 1, 2, 3, 4 ),
( 1, 1, 1, 1 ),
( 1, 1, 1, 1 ),
( -1, 0,-1, 0 )) )
#Pretend Encoder Out
encOut=tf.constant(eo,tf.float32)
#Pretend Attention - these weights could be learned
AT=( (.6,.25,.25),
(.2,.25,.25),
(.1,.25,.25),
(.1,.25,.25) )
#Pretend Attention Weights
wAT=tf.constant(AT,tf.float32)
encAT=tf.tensordot(encOut,wAT,[[1],[0]])
sess=tf.Session()
print sess.run(encAT)
decAT=tf.transpose(encAT,[0,2,1])
print sess.run(decAT)
#P108 - GRU Example
#cell=tf.contrib.GRUCell(rnnSz)
#encOutSmall,encStateS=tf.nn.dynamic_rnn(cell,smallerEmbs,...)
#encOutLarge,encStateL=tf.nn.dynamic_rnn(cell,largerEmbs,...)