def _rnn(self, X, h0, mask=None): ##################################### # X: sequences inputs (included bias) # init: prev_states # W: concatenated [W_update, W_reset] # mask: mask inputs (optional) prev_states = h0 nb_units = self.num_units # hidden connection of all gates and states update hid_connection = K.dot(prev_states, self.W_hid) # hidden to hidden connection hid_gate = _slice_x(hid_connection, slice(None, nb_units * 2)) X_gate = _slice_x(X, slice(None, nb_units * 2)) b_gate = 0 if self.b_init is None else _slice_x( self.b, slice(None, nb_units * 2)) # states hid_states = _slice_x(hid_connection, slice(nb_units * 2, None)) X_states = _slice_x(X, slice(nb_units * 2, None)) b_states = 0 if self.b_init is None else _slice_x( self.b, slice(nb_units * 2, None)) # new gates _ = self.gate_activation(X_gate + hid_gate + b_gate) update_values = _slice_x(_, slice(None, nb_units)) reset_values = _slice_x(_, slice(nb_units, nb_units * 2)) # calculate new gates new_states = self.activation(X_states + reset_values * hid_states + b_states) # final new states next_states = (new_states * update_values + prev_states * (1 - update_values)) # mask the next state if mask is not None: next_states = K.switch(mask, next_states, prev_states) return next_states
def _rnn(self, X, h0, c0, mask=None): ##################################### # X: sequences inputs (included bias) # init: prev_states # W: concatenated [W_update, W_reset] # mask: mask inputs (optional) prev_states = h0 prev_memory = c0 nb_units = self.num_units # hidden to hidden connection bias = 0 if self.b_init is None else self.b _ = X + K.dot(prev_states, self.W_hid) + bias hid_input = _slice_x(_, slice(None, nb_units)) hid_forget = _slice_x(_, slice(nb_units, nb_units * 2)) hid_hidden = _slice_x(_, slice(nb_units * 2, nb_units * 3)) hid_output = _slice_x(_, slice(nb_units * 3, None)) # peepholes connection if hasattr(self, 'peepholes'): hid_input += prev_memory * _slice_x(self.peepholes, slice(None, nb_units)) hid_forget += prev_memory * _slice_x(self.peepholes, slice(nb_units, nb_units * 2)) # calculate new gates input_gate = self.gate_activation(hid_input) forget_gate = self.gate_activation(hid_forget) new_memory = self.activation(hid_hidden) # next cell memory next_memory = (forget_gate * prev_memory + input_gate * new_memory) # output gate if hasattr(self, 'peepholes'): hid_output += next_memory * _slice_x(self.peepholes, slice(nb_units * 2, None)) output_gate = self.gate_activation(hid_output) # new hidden state next_states = output_gate * self.activation(next_memory) # mask the next state if mask is not None: next_states = K.switch(mask, next_states, prev_states) next_memory = K.switch(mask, next_memory, prev_memory) return next_states, next_memory
def test_basic_ops_value(self): np.random.seed(12082518) x = K.variable(np.random.randn(8, 8)) y = K.variable(np.random.randn(8, 8)) z = K.variable(np.random.randint(0, 2, size=(8, 8)), dtype=np.bool) w = K.variable(np.random.randint(0, 2, size=(8, 8)), dtype=np.bool) self.assertEqual(round(np.sum(K.eval(K.relu(x, alpha=0.12))) * 10000), 276733) self.assertEqual(round(np.sum(K.eval(K.elu(x, alpha=0.12))) * 10000), 289202) self.assertEqual(np.sum(K.eval(K.softmax(x))), 8.0) self.assertEqual(round(np.sum(K.eval(K.softplus(x))) * 10000), 554564) self.assertEqual(round(np.sum(K.eval(K.softsign(x))) * 100000), 211582) self.assertEqual(round(np.sum(K.eval(K.sigmoid(x))) * 10000), 330427) self.assertEqual(round(np.sum(K.eval(K.hard_sigmoid(x))) * 10000), 330836) self.assertEqual(round(np.sum(K.eval(K.tanh(x))) * 100000), 290165) self.assertEqual(round(np.sum(K.eval(K.square(x))) * 10000), 744492) self.assertEqual(round(np.sum(K.eval(K.sqrt(x))) * 10000), 300212) self.assertEqual(round(np.sum(K.eval(K.abs(x))) * 10000), 559979) self.assertEqual(np.sum(K.eval(K.sign(x))), 6.0) self.assertEqual(round(np.sum(K.eval(K.inv(x))) * 1000), 495838) self.assertEqual(round(np.sum(K.eval(K.exp(x))) * 1000), 122062) self.assertEqual(round(np.sum(K.eval(K.log(K.abs(x)))) * 10000), -344491) self.assertEqual(np.sum(K.eval(K.round(x))), 5.0) self.assertEqual(round(np.sum(K.eval(K.pow(x, 8))) * 100), 398153) self.assertEqual( round(np.sum(K.eval(K.clip(x, -0.12, 0.12))) * 1000000), 620529) # TODO: pygpu (libgpuarray) still not support diag # self.assertEqual(round(np.sum(K.eval(K.diag(x))) * 100000), 325289) self.assertEqual(np.sum(K.eval(K.eye(12, 8))), 8.0) self.assertEqual(np.sum(K.eval(K.eq(z, w))), 38) self.assertEqual(np.sum(K.eval(K.neq(z, w))), 26) self.assertEqual(np.sum(K.eval(K.gt(x, y))), 33) self.assertEqual(np.sum(K.eval(K.ge(x, y))), 33) self.assertEqual(np.sum(K.eval(K.lt(x, y))), 31) self.assertEqual(np.sum(K.eval(K.le(x, y))), 31) self.assertEqual(round(np.sum(K.eval(K.switch(z, x, y))) * 100000), 139884)
def _rnn(self, X, h0, mask=None): bias = 0. if self.b_init is None else self.b next_states = self.activation(X + K.dot(h0, self.W_hid) + bias) if mask is not None: next_states = K.switch(mask, next_states, h0) return next_states