def cal_hidden_keras(self, test, layernum): if layernum == 0: acx = test else: acx = get_activations_single_layer(self.model, np.array(test), self.layerName(layernum - 1)) units = int( int(self.model.layers[layernum].trainable_weights[0].shape[1]) / 4) if len(acx.shape) < len(test.shape): acx = np.array([acx]) inp = keras.layers.Input(batch_shape=(None, acx.shape[1], acx.shape[2]), name="input") rnn, s, c = keras.layers.LSTM(units, return_sequences=True, stateful=False, return_state=True, name="RNN")(inp) states = keras.models.Model(inputs=[inp], outputs=[s, c, rnn]) for layer in states.layers: if layer.name == "RNN": layer.set_weights(self.model.layers[layernum].get_weights()) h_t_keras, c_t_keras, rnn = states.predict(acx) return rnn
def displayInfo(self, test): model = self._code_repr_model print("method name: %s" % (test)) conf = get_activations_single_layer(model, np.array([test]), self.layerName(-1)) print("current confidence: %.2f\n" % (conf)) if conf >= 0.5: return (1, conf) else: return (-1, 1 - conf)
def displayInfo(self, test): model = self.model text = self.fromIDToText(test) print("review content: %s" % (text)) conf = get_activations_single_layer(model, np.array([test]), self.layerName(-1)) print("current confidence: %.2f\n" % (conf)) if conf >= 0.5: return (1, conf) else: return (-1, 1 - conf)
def cal_hidden_state(self, test, layernum): if layernum == 0: acx = test else: acx = get_activations_single_layer(self.model, np.array([test]), self.layerName(layernum - 1)) units = int( int(self.model.layers[layernum].trainable_weights[0].shape[1]) / 4) # print("No units: ", units) # get weight W = self.model.layers[layernum].get_weights()[0] U = self.model.layers[layernum].get_weights()[1] b = self.model.layers[layernum].get_weights()[2] W_i = W[:, :units] W_f = W[:, units:units * 2] W_c = W[:, units * 2:units * 3] W_o = W[:, units * 3:] U_i = U[:, :units] U_f = U[:, units:units * 2] U_c = U[:, units * 2:units * 3] U_o = U[:, units * 3:] b_i = b[:units] b_f = b[units:units * 2] b_c = b[units * 2:units * 3] b_o = b[units * 3:] # calculate the hidden state value h_t = np.zeros((self.imagesize, units)) c_t = np.zeros((self.imagesize, units)) f_t = np.zeros((self.imagesize, units)) h_t0 = np.zeros((1, units)) c_t0 = np.zeros((1, units)) for i in range(0, self.imagesize): f_gate = hard_sigmoid( np.dot(acx[i, :], W_f) + np.dot(h_t0, U_f) + b_f) i_gate = hard_sigmoid( np.dot(acx[i, :], W_i) + np.dot(h_t0, U_i) + b_i) o_gate = hard_sigmoid( np.dot(acx[i, :], W_o) + np.dot(h_t0, U_o) + b_o) new_C = np.tanh(np.dot(acx[i, :], W_c) + np.dot(h_t0, U_c) + b_c) c_t0 = f_gate * c_t0 + i_gate * new_C h_t0 = o_gate * np.tanh(c_t0) c_t[i, :] = c_t0 h_t[i, :] = h_t0 f_t[i, :] = f_gate return [h_t, c_t, f_t]
def cal_hidden_state(self, x_mn, x_api, x_token): print(self.layerName(6)) acx = get_activations_single_layer(self._code_repr_model, x_mn, x_api, x_token, self.layerName(2)) units = int(int(self._code_repr_model.layers[6].trainable_weights[0].shape[1]) / 4) # print("No units: ", units) # lstm_layer = model.layers[1] W = self._code_repr_model.layers[6].get_weights()[0] U = self._code_repr_model.layers[6].get_weights()[1] b = self._code_repr_model.layers[6].get_weights()[2] W_i = W[:, :units] W_f = W[:, units: units * 2] W_c = W[:, units * 2: units * 3] W_o = W[:, units * 3:] U_i = U[:, :units] U_f = U[:, units: units * 2] U_c = U[:, units * 2: units * 3] U_o = U[:, units * 3:] b_i = b[:units] b_f = b[units: units * 2] b_c = b[units * 2: units * 3] b_o = b[units * 3:] # calculate the hidden state value h_t = np.zeros((6, units)) c_t = np.zeros((6, units)) f_t = np.zeros((6, units)) h_t0 = np.zeros((1, units)) c_t0 = np.zeros((1, units)) for i in range(0, 6): f_gate = hard_sigmoid(np.dot(acx[i, :], W_f) + np.dot(h_t0, U_f) + b_f) i_gate = hard_sigmoid(np.dot(acx[i, :], W_i) + np.dot(h_t0, U_i) + b_i) o_gate = hard_sigmoid(np.dot(acx[i, :], W_o) + np.dot(h_t0, U_o) + b_o) new_C = np.tanh(np.dot(acx[i, :], W_c) + np.dot(h_t0, U_c) + b_c) c_t0 = f_gate * c_t0 + i_gate * new_C h_t0 = o_gate * np.tanh(c_t0) c_t[i, :] = c_t0 h_t[i, :] = h_t0 f_t[i, :] = f_gate return h_t, c_t, f_t
def get_lstm_state(self, test): return get_activations_single_layer(self.lstm_state_model, np.array([test]), 'lstm_1')