-
Notifications
You must be signed in to change notification settings - Fork 8
/
Chapter-5-01-Sequence-to-Sequence.py
34 lines (27 loc) · 1.29 KB
/
Chapter-5-01-Sequence-to-Sequence.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
# =============================================================================
# Examples from Eugene Charniak's Introduction to Deep Learning 2018 MIT Press
# =============================================================================
#CHAPTER 5
#Code fragments to give general idea of sequence to sequence network
#Exercise is left to reader to implement
import tensorflow as tf
vfSz=10
embedSz=10
with tf.variable_scope("enc"):
F=tf.Variable(tf.random_normal((vfSz,embedSz),stddev=.1))
embs=tf.nn.embedding_lookup(F,encIn)
embs=tf.nn.dropout(embs,keepPrb)
cell=tf.contrib.rnn.GRUCell(rnnSz)
initState=cell.zero_state(bSz,tf.float32)
encOut,encState=tf.nn.dynamic_rnn(cell,embs,initial_state=initState)
with tf.variable_scope("dec"):
E=tf.Variable(tf.random_normal((veSz,embedSz),stddev=.1))
embs=tf.nn.embedding_lookup(E,decIn)
embs=tf.nn.dropout(embs,keepPrb)
cell=tf.contrib.rnn.GRUCell(rnnSz)
decOut,_=tf.nn.dynamic_rnn(cell,embs,initial_state=encState)
W=tf.Variable(tf.random_normal([rnnSz,veSz],stddev=.1))
b=tf.Variable(tf.random_normal([veSz],stddev=.1))
logits=tf.tensordot(decOut,W,axes=[[2],[0]])+b
loss=tf.contrib.seq2seq.sequence_loss(logits, ans, tf.ones([bSz,wSz]))
# =============================================================================