forked from ARenanse/Neural-Turing-Machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatch_RWV_Generation.py
62 lines (40 loc) · 1.95 KB
/
Batch_RWV_Generation.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import tensorflow as tf
import numpy as np
@tf.function
def ReadVector(M_t, w_t):
'''
Computes the Read Vector of EACH HEAD for the entire Batch at once.
M_t: Memory of size (Batch_size,N,M) at time t.
w_t: (Batch_size,N), Weighting generated by READ HEAD at time t for reading memory location
RETURNS:
r_t: (Batch_size,M) The Read Vector
'''
batch_size,N,M = M_t.shape[0], M_t.shape[1], M_t.shape[2]
#tol = 0.01
#assert (np.sum(w_t) >= 1.0 - tol) & (np.sum(w_t) <= 1.0 + tol)
#print(batch_size,N,M)
#print('w_t shape: ',w_t.shape)
r_t = tf.reshape( tf.matmul(tf.reshape(w_t,(batch_size,1,N)),M_t), (batch_size,M) )
#tf.print('ReadVector: ',tf.math.reduce_any(tf.math.is_nan( r_t )) )
#assert r_t.shape == (batch_size,M)
return r_t
@tf.function
def WriteOnMemory(M_prev, w_t, e_t, a_t):
'''
Computes the updated Memory Matrix for the each example in Batch at once
M_prev: Memory Matrix at the previous time step of size (Batch_size,N,M)
w_t: (Batch_size,N), Weighting generated by WRITE HEAD at time t for Writing to the memory locations.
e_t: (Batch_size,M), Erase vector generated by WRITE HEAD.
a_t: (Batch_size,M), Add vector generated by WRITE HEAD.
RETURNS:
M_t: (Batch_size,N,M), New Memory Matrix after Erasing/Adding/Combination of new instances.
'''
(batch_size,N,M) = M_prev.shape
M_hat_t = tf.multiply( M_prev, 1-tf.multiply(tf.reshape(w_t,(batch_size,N,1)),tf.reshape(e_t,(batch_size,1,M))))
#^Of shape [batch_size,N,M]
#tf.print('WriteOnMemory M_hat_t',tf.math.reduce_any(tf.math.is_nan( M_hat_t )) )
#assert M_hat_t.shape == M_prev.shape
M_t = M_hat_t + tf.multiply(tf.reshape(w_t,(batch_size,N,1)),tf.reshape(a_t,(batch_size,1,M)))
#tf.print('WriteOnMemory',tf.math.reduce_any(tf.math.is_nan( M_t )) )
#assert M_t.shape == M_prev.shape
return M_t