-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.py
executable file
·56 lines (46 loc) · 2.04 KB
/
cell.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
import tensorflow as tf
import numpy as np
class Cell(object):
def __init__(self, state, h_activation,
input=None, first=False, prevCell=None):
if first and input is not None:
# Concatenates the vector of inputs with the vector of
# activations
h_activation = tf.concat(0,
[tf.transpose(input), h_activation])
if first or prevCell is None:
# Generates the gating matrices and biases
print("Will initialize the first parameters...")
self.wf, self.wi, self.wc, self.wo, \
self.bf, self.bi, self.bc, self.bo = \
self.initialize_parameters(
state.get_shape().as_list()[0],
h_activation.get_shape().as_list()[0],
first)
else: # if not first and prevCell is not None:
print("Will reuse awkeufahweufhkawe...")
# TODO: Maybe create a class for these
self.wf, self.wi, self.wc, self.wo, \
self.bf, self.bi, self.bc, self.bo = \
prevCell.wf, prevCell.wi, prevCell.wc, \
prevCell.wo, \
prevCell.bf, prevCell.bi, prevCell.bc, prevCell.bo,
f = tf.sigmoid(tf.matmul(self.wf, h_activation) + self.bf)
i = tf.sigmoid(tf.matmul(self.wi, h_activation) + self.bi)
c = tf.tanh(tf.matmul(self.wc, h_activation) + self.bc)
o = tf.sigmoid(tf.matmul(self.wo, h_activation) + self.bo)
self.state_next = tf.add(tf.mul(f, state), tf.mul(i, c))
self.h_next = tf.mul(tf.tanh(self.state_next), o)
return
def initialize_parameters(self, n_s, n_h, first):
""" Obviously, this function can be simplified.
It is this way only to aid debugging. """
wf = tf.Variable(tf.truncated_normal([n_s, n_h], stddev=0.1))
wi = tf.Variable(tf.truncated_normal([n_s, n_h], stddev=0.1))
wc = tf.Variable(tf.truncated_normal([n_s, n_h], stddev=0.1))
wo = tf.Variable(tf.truncated_normal([n_s, n_h], stddev=0.1))
bf = tf.Variable(tf.constant(0.1, shape=[n_s, 1]))
bi = tf.Variable(tf.constant(0.1, shape=[n_s, 1]))
bc = tf.Variable(tf.constant(0.1, shape=[n_s, 1]))
bo = tf.Variable(tf.constant(0.1, shape=[n_s, 1]))
return wf, wi, wc, wo, bf, bi, bc, bo