def probability_plot(probabilities, selected_sentence, dataset, ploting_path, top_n_probabilities=20, max_length=120): # Pyplot options fig, ax = plt.subplots() ax.set_axis_off() tb = Table(ax, bbox=[0, 0, 1, 1]) ncols = probabilities.shape[0] width, height = 1.0 / (ncols + 1), 1.0 / (top_n_probabilities + 1) # Truncate the time selected_sentence = selected_sentence[:max_length] probabilities = probabilities[:max_length] # Sort the frequencies sorted_indices = np.argsort(probabilities, axis=1) probabilities = probabilities[ np.repeat(np.arange(probabilities.shape[0])[:, None], probabilities.shape[1], axis=1), sorted_indices][:, ::-1] # Truncate the probabilities probabilities = probabilities[:, :top_n_probabilities] for (i, j), _ in np.ndenumerate(probabilities): tb.add_cell(j + 1, i, height, width, text=unicode(str( conv_into_char(sorted_indices[i, j, 1], dataset)[0]), errors='ignore'), loc='center', facecolor=(1, 1 - probabilities[i, j, 0], 1 - probabilities[i, j, 0])) for i, char in enumerate(selected_sentence): tb.add_cell(0, i, height, width, text=unicode(char, errors='ignore'), loc='center', facecolor='green') ax.add_table(tb) plt.savefig(ploting_path)
def probability_plot(probabilities, selected_sentence, dataset, ploting_path, top_n_probabilities=20, max_length=120): # Pyplot options fig, ax = plt.subplots() ax.set_axis_off() tb = Table(ax, bbox=[0, 0, 1, 1]) ncols = probabilities.shape[0] width, height = 1.0 / (ncols + 1), 1.0 / (top_n_probabilities + 1) # Truncate the time selected_sentence = selected_sentence[:max_length] probabilities = probabilities[:max_length] # Sort the frequencies sorted_indices = np.argsort(probabilities, axis=1) probabilities = probabilities[ np.repeat(np.arange(probabilities.shape[0])[ :, None], probabilities.shape[1], axis=1), sorted_indices][:, ::-1] # Truncate the probabilities probabilities = probabilities[:, :top_n_probabilities] for (i, j), _ in np.ndenumerate(probabilities): tb.add_cell(j + 1, i, height, width, text=unicode(str(conv_into_char(sorted_indices[i, j, 1], dataset)[0]), errors='ignore'), loc='center', facecolor=(1, 1 - probabilities[i, j, 0], 1 - probabilities[i, j, 0])) for i, char in enumerate(selected_sentence): tb.add_cell(0, i, height, width, text=unicode(char, errors='ignore'), loc='center', facecolor='green') ax.add_table(tb) plt.savefig(ploting_path)
def plot(what, train_stream, compiled, args): # states epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0: args.visualize_length, 0:1] values = compiled(init_) layers = len(values) time = values[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for d in range(layers): # Change the subplot plt.subplot(layers, 1, d + 1) # print only 5 values of the hiddenstate for j in range(10): plt.plot(np.arange(time), values[d][:, 0, j]) # plt.plot( # np.arange(time), np.mean(np.abs(values[d][:, 0, :]), axis=1)) # Add ticks for xaxis plt.xticks(range(args.visualize_length), ticks) # Fancy options plt.grid(True) plt.title(what + "_of_layer_" + str(d)) plt.tight_layout() # Either plot on the current display or save the plot into a file if args.local: plt.show() else: plt.savefig( args.save_path + "/visualize_" + what + '_' + str(num) + ".png") logger.info("Figure \"visualize_" + what + '_' + str(num) + ".png\" saved at directory: " + args.save_path)
def plot(what, train_stream, compiled, args): # states epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0:args.visualize_length, 0:1] values = compiled(init_) layers = len(values) time = values[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for d in range(layers): # Change the subplot plt.subplot(layers, 1, d + 1) # print only 5 values of the hiddenstate for j in range(10): plt.plot(np.arange(time), values[d][:, 0, j]) # plt.plot( # np.arange(time), np.mean(np.abs(values[d][:, 0, :]), axis=1)) # Add ticks for xaxis plt.xticks(range(args.visualize_length), ticks) # Fancy options plt.grid(True) plt.title(what + "_of_layer_" + str(d)) plt.tight_layout() # Either plot on the current display or save the plot into a file if args.local: plt.show() else: plt.savefig(args.save_path + "/visualize_" + what + '_' + str(num) + ".png") logger.info("Figure \"visualize_" + what + '_' + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_generate(cost, hidden_states, updates, train_stream, valid_stream, args): use_indices = has_indices(args.dataset) output_size = get_output_size(args.dataset) # Get presoft and its computation graph filter_presoft = VariableFilter(theano_name="presoft") presoft = filter_presoft(ComputationGraph(cost).variables)[0] cg = ComputationGraph(presoft) # Handle the theano shared variables that allow carrying the hidden # state givens, f_updates = carry_hidden_state(updates, 1, reset=not(use_indices)) if args.hide_all_except is not None: pass # Compile the theano function compiled = theano.function(inputs=cg.inputs, outputs=presoft, givens=givens, updates=f_updates) epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): all_ = next(epoch_iterator) all_sequence = all_[0][:, 0:1] targets = all_[1][:, 0:1] # In the case of characters and text if use_indices: init_ = all_sequence[:args.initial_text_length] # Time X Features probability_array = np.zeros((0, output_size)) generated_text = init_ for i in range(args.generated_text_lenght): presoft = compiled(generated_text) # Get the last value of presoft last_presoft = presoft[-1:, 0, :] # Compute the probability distribution probabilities = softmax(last_presoft) # Store it in the list probability_array = np.vstack([probability_array, probabilities]) # Sample a character out of the probability distribution argmax = (args.softmax_sampling == 'argmax') last_output_sample = sample(probabilities, argmax)[:, None, :] # Concatenate the new value to the text generated_text = np.vstack( [generated_text, last_output_sample]) ploting_path = None if args.save_path is not None: ploting_path = os.path.join( args.save_path, 'prob_plot.png') # Convert with real characters whole_sentence = conv_into_char( generated_text[:, 0], args.dataset) initial_sentence = whole_sentence[:init_.shape[0]] selected_sentence = whole_sentence[init_.shape[0]:] logger.info(''.join(initial_sentence) + '...') logger.info(''.join(whole_sentence)) if ploting_path is not None: probability_plot(probability_array, selected_sentence, args.dataset, ploting_path) # In the case of sine wave dataset for example else: presoft = compiled(all_sequence) time_plot = presoft.shape[0] - 1 plt.plot(np.arange(time_plot), targets[:time_plot, 0, 0], label="target") plt.plot(np.arange(time_plot), presoft[:time_plot, 0, 0], label="predicted") plt.legend() plt.grid(True) plt.show()
def do(self, *args): # init is TIME X 1 # This is because in interactive mode, # self.main_loop.epoch_iterator is not accessible. if self.interactive_mode: # TEMPORARY HACK iterator = self.main_loop.data_stream.get_epoch_iterator() all_sequence = next(iterator)[0][:, 0:1] else: iterator = self.main_loop.epoch_iterator all_sequence = next(iterator)["features"][:, 0:1] init_ = all_sequence[:self.init_length] # Time X Features probability_array = np.zeros((0, self.output_size)) generated_text = init_ logger.info("\nGeneration:") for i in range(self.generation_length): presoft = self.generate(generated_text)[0] # Get the last value of presoft last_presoft = presoft[-1:, 0, :] if self.has_indices: # Compute the probability distribution probabilities = softmax(last_presoft) # Store it in the list probability_array = np.vstack([probability_array, probabilities]) # Sample a character out of the probability distribution argmax = (self.softmax_sampling == 'argmax') last_output_sample = sample(probabilities, argmax)[:, None, :] else: last_output_sample = last_presoft[:, None, :] # Concatenate the new value to the text generated_text = np.vstack([generated_text, last_output_sample]) # In the case of characters and text if self.has_indices: # Convert with real characters whole_sentence = conv_into_char(generated_text[:, 0], self.dataset) initial_sentence = whole_sentence[:init_.shape[0]] selected_sentence = whole_sentence[init_.shape[0]:] logger.info(''.join(initial_sentence) + '...') logger.info(''.join(whole_sentence)) if self.ploting_path is not None: probability_plot(probability_array, selected_sentence, self.dataset, self.ploting_path) # In the case of sine wave dataset for example else: time_plot = min([all_sequence.shape[0], generated_text.shape[0]]) plt.plot(np.arange(time_plot), all_sequence[:time_plot, 0, 0], label="target") plt.plot(np.arange(time_plot), generated_text[:time_plot, 0, 0], label="predicted") plt.legend() plt.show()
def visualize_gates_lstm(gate_values, hidden_states, updates, train_stream, valid_stream, args): in_gates = gate_values["in_gates"] out_gates = gate_values["out_gates"] forget_gates = gate_values["forget_gates"] # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state(updates, 1, not(has_indices(args.dataset))) generate_in = theano.function(inputs=ComputationGraph(in_gates).inputs, outputs=in_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) generate_out = theano.function(inputs=ComputationGraph(out_gates).inputs, outputs=out_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) generate_forget = theano.function(inputs=ComputationGraph(forget_gates).inputs, outputs=forget_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) # Generate epoch_iterator = valid_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0: args.visualize_length, 0:1] last_output_in = generate_in(init_) last_output_out = generate_out(init_) last_output_forget = generate_forget(init_) layers = len(last_output_in) time = last_output_in[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for i in range(layers): plt.subplot(3, layers, 1 + i) plt.plot(np.arange(time), np.mean( np.abs(last_output_in[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("in_gate of layer " + str(i)) plt.subplot(3, layers, layers + 1 + i) plt.plot(np.arange(time), np.mean( np.abs(last_output_out[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("out_gate of layer " + str(i)) plt.subplot(3, layers, 2 * layers + 1 + i) plt.plot(np.arange(time), np.mean( np.abs(last_output_forget[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("forget_gate of layer " + str(i)) if args.local: plt.show() else: plt.savefig( args.save_path + "/visualize_gates_" + str(num) + ".png") logger.info("Figure \"visualize_gates_" + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_generate(cost, hidden_states, updates, train_stream, valid_stream, args): use_indices = has_indices(args.dataset) output_size = get_output_size(args.dataset) # Get presoft and its computation graph filter_presoft = VariableFilter(theano_name="presoft") presoft = filter_presoft(ComputationGraph(cost).variables)[0] cg = ComputationGraph(presoft) # Handle the theano shared variables that allow carrying the hidden # state givens, f_updates = carry_hidden_state(updates, 1, reset=not(use_indices)) # Compile the theano function compiled = theano.function(inputs=cg.inputs, outputs=presoft, givens=givens, updates=f_updates) epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): all_ = next(epoch_iterator) all_sequence = all_[0][:, 0:1] targets = all_[1][:, 0:1] # In the case of characters and text if use_indices: init_ = all_sequence[:args.initial_text_length] # Time X Features probability_array = np.zeros((0, output_size)) generated_text = init_ for i in range(args.generated_text_lenght): presoft = compiled(generated_text) # Get the last value of presoft last_presoft = presoft[-1:, 0, :] # Compute the probability distribution probabilities = softmax(last_presoft) # Store it in the list probability_array = np.vstack([probability_array, probabilities]) # Sample a character out of the probability distribution argmax = (args.softmax_sampling == 'argmax') last_output_sample = sample(probabilities, argmax)[:, None, :] # Concatenate the new value to the text generated_text = np.vstack( [generated_text, last_output_sample]) ploting_path = None if args.save_path is not None: ploting_path = os.path.join( args.save_path, 'prob_plot.png') # Convert with real characters whole_sentence = conv_into_char( generated_text[:, 0], args.dataset) initial_sentence = whole_sentence[:init_.shape[0]] selected_sentence = whole_sentence[init_.shape[0]:] logger.info(''.join(initial_sentence) + '...') logger.info(''.join(whole_sentence)) if ploting_path is not None: probability_plot(probability_array, selected_sentence, args.dataset, ploting_path) # In the case of sine wave dataset for example else: presoft = compiled(all_sequence) time_plot = presoft.shape[0] - 1 plt.plot(np.arange(time_plot), targets[:time_plot, 0, 0], label="target") plt.plot(np.arange(time_plot), presoft[:time_plot, 0, 0], label="predicted") plt.legend() plt.grid(True) plt.show()
def visualize_gradients(hidden_states, updates, train_stream, valid_stream, args): # Get all the hidden_states filter_states = VariableFilter(theano_name_regex="hidden_state_.*") all_states = filter_states(hidden_states) all_states = sorted(all_states, key=lambda var: var.name[-1]) # Get all the hidden_cells filter_cells = VariableFilter(theano_name_regex="hidden_cell_.*") all_cells = filter_cells(hidden_states) all_cells = sorted(all_cells, key=lambda var: var.name[-1]) # Get the variable on which we compute the gradients filter_pre_rnn = VariableFilter(theano_name_regex="pre_rnn.*") wrt = filter_pre_rnn(ComputationGraph(hidden_states).variables) wrt = sorted(wrt, key=lambda var: var.name[-1]) len_wrt = len(wrt) # We have wrt = [pre_rnn] or [pre_rnn_0, pre_rnn_1, ...] # Assertion part assert len(all_states) == args.layers assert len(all_cells) == (args.layers * (args.rnn_type == "lstm")) if args.skip_connections: assert len_wrt == args.layers else: assert len_wrt == 1 # Comupute the gradients of states or cells if args.rnn_type == "lstm" and args.visualize_cells: states = all_cells else: states = all_states logger.info("The computation of the gradients has started") gradients = [] for i, state in enumerate(states): gradients.extend( tensor.grad(tensor.mean(tensor.abs_( state[-1, 0, :])), wrt[:i + 1])) # -1 indicates that gradient is gradient of the last time-step.c logger.info("The computation of the gradients is done") # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state(updates, 1, reset=not(has_indices(args.dataset))) # Compile the function logger.info("The compilation of the function has started") compiled = theano.function(inputs=ComputationGraph(states).inputs, outputs=gradients, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) logger.info("The function has been compiled") # Generate epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][ 0: args.visualize_length, 0:1] # [layers * len_wrt] [Time, 1, Hidden_dim] gradients = compiled(init_) if args.skip_connections: assert len(gradients) == (args.layers * (args.layers + 1)) / 2 else: assert len(gradients) == args.layers time = gradients[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) # One row subplot for each variable wrt which we are computing # the gradients for var in range(len_wrt): plt.subplot(len_wrt, 1, var + 1) for d in range(args.layers - var): plt.plot( np.arange(time), np.mean(np.abs(gradients[d][:, 0, :]), axis=1), label="layer " + str(d + var)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.yscale('log') axes = plt.gca() axes.set_ylim([5e-20, 5e-1]) plt.title("gradients plotting w.r.t pre_rrn" + str(var)) plt.legend() plt.tight_layout() if args.local: plt.show() else: plt.savefig( args.save_path + "/visualize_gradients_" + str(num) + ".png") logger.info("Figure \"visualize_gradients_" + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_presoft(cost, hidden_states, updates, train_stream, valid_stream, args): filter_presoft = VariableFilter(theano_name="presoft") presoft = filter_presoft(ComputationGraph(cost).variables)[0] # Get all the hidden_states filter_states = VariableFilter(theano_name_regex="hidden_state_.*") all_states = filter_states(hidden_states) all_states = sorted(all_states, key=lambda var: var.name[-1]) # Assertion part assert len(all_states) == args.layers logger.info("The computation of the gradients has started") gradients = [] for i in range(args.visualize_length - args.context): gradients.extend( tensor.grad(tensor.mean(tensor.abs_(presoft[i, 0, :])), all_states)) logger.info("The computation of the gradients is done") # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state(updates, 1, not(has_indices(args.dataset))) # Compile the function logger.info("The compilation of the function has started") compiled = theano.function(inputs=ComputationGraph(presoft).inputs, outputs=gradients, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) logger.info("The function has been compiled") # Generate epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][ 0: args.visualize_length, 0:1] hidden_state = compiled(init_) value_of_layer = {} for d in range(args.layers): value_of_layer[d] = 0 for i in range(len(hidden_state) / args.layers): for d in range(args.layers): value_of_layer[d] += hidden_state[d + i * args.layers] time = hidden_state[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for d in range(args.layers): plt.plot( np.arange(time), np.mean(np.abs(value_of_layer[d][:, 0, :]), axis=1), label="Layer " + str(d)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("hidden_state_of_layer_" + str(d)) plt.legend() plt.tight_layout() if args.local: plt.show() else: plt.savefig( args.save_path + "/visualize_presoft_" + str(num) + ".png") logger.info("Figure \"visualize_presoft_" + str(num) + ".png\" saved at directory: " + args.save_path)
def do(self, *args): # init is TIME X 1 # This is because in interactive mode, # self.main_loop.epoch_iterator is not accessible. if self.interactive_mode: # TEMPORARY HACK iterator = self.main_loop.data_stream.get_epoch_iterator() all_sequence = next(iterator)[0][:, 0:1] else: iterator = self.main_loop.epoch_iterator all_sequence = next(iterator)["features"][:, 0:1] init_ = all_sequence[:self.init_length] # Time X Features probability_array = np.zeros((0, self.output_size)) generated_text = init_ logger.info("\nGeneration:") for i in range(self.generation_length): presoft = self.generate(generated_text)[0] # Get the last value of presoft last_presoft = presoft[-1:, 0, :] if self.has_indices: # Compute the probability distribution probabilities = softmax(last_presoft) # Store it in the list probability_array = np.vstack( [probability_array, probabilities]) # Sample a character out of the probability distribution argmax = (self.softmax_sampling == 'argmax') last_output_sample = sample(probabilities, argmax)[:, None, :] else: last_output_sample = last_presoft[:, None, :] # Concatenate the new value to the text generated_text = np.vstack([generated_text, last_output_sample]) # In the case of characters and text if self.has_indices: # Convert with real characters whole_sentence = conv_into_char(generated_text[:, 0], self.dataset) initial_sentence = whole_sentence[:init_.shape[0]] selected_sentence = whole_sentence[init_.shape[0]:] logger.info(''.join(initial_sentence) + '...') logger.info(''.join(whole_sentence)) if self.ploting_path is not None: probability_plot(probability_array, selected_sentence, self.dataset, self.ploting_path) # In the case of sine wave dataset for example else: time_plot = min([all_sequence.shape[0], generated_text.shape[0]]) plt.plot(np.arange(time_plot), all_sequence[:time_plot, 0, 0], label="target") plt.plot(np.arange(time_plot), generated_text[:time_plot, 0, 0], label="predicted") plt.legend() plt.show()
def visualize_jacobian(hidden_states, updates, train_stream, valid_stream, args): # Get all the hidden_states all_states = [ var for var in hidden_states if re.match("hidden_state_.*", var.name)] all_states = sorted(all_states, key=lambda var: var.name[-1]) # Get all the hidden_cells all_cells = [var for var in hidden_states if re.match( "hidden_cell_.*", var.name)] all_cells = sorted(all_cells, key=lambda var: var.name[-1]) # Get the variable on which we compute the gradients variables = ComputationGraph(hidden_states).variables wrt = [ var for var in variables if (var.name is not None) and (re.match("pre_rnn.*", var.name))] wrt = sorted(wrt, key=lambda var: var.name[-1]) len_wrt = len(wrt) # We have wrt = [pre_rnn] or [pre_rnn_0, pre_rnn_1, ...] # Assertion part assert len(all_states) == args.layers assert len(all_cells) == (args.layers * (args.rnn_type == "lstm")) if args.skip_connections: assert len_wrt == args.layers else: assert len_wrt == 1 # Comupute the gradients of states or cells if args.rnn_type == "lstm" and args.visualize_cells: states = all_cells else: states = all_states logger.info("The computation of the gradients has started") gradients = [] for i, state in enumerate(states): gradients.append( tensor.grad(tensor.mean(tensor.abs_( state[-1])), state)) # -1 indicates that gradient is gradient of the last time-step.c logger.info("The computation of the gradients is done") # Handle the theano shared variables for the state state_vars = [theano.shared( v[0:1, :].zeros_like().eval(), v.name + '-gen') for v, _ in updates] givens = [(v, x) for (v, _), x in zip(updates, state_vars)] f_updates = [(x, upd) for x, (_, upd) in zip(state_vars, updates)] # Compile the function logger.info("The compilation of the function has started") compiled = theano.function(inputs=ComputationGraph(states).inputs, outputs=gradients, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) logger.info("The function has been compiled") import ipdb ipdb.set_trace() # Generate epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][ 0: args.visualize_length, 0:1] # [layers * len_wrt] [Time, 1, Hidden_dim] gradients = compiled(init_) time = gradients[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) # One row subplot for each variable wrt which we are computing # the gradients for var in range(len_wrt): plt.subplot(len_wrt, 1, var + 1) for d in range(args.layers - var): plt.plot( np.arange(time), np.mean(np.abs(gradients[d][:, 0, :]), axis=1), label="layer " + str(d + var)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.yscale('log') axes = plt.gca() axes.set_ylim([5e-20, 5e-1]) plt.title("gradients plotting w.r.t pre_rrn" + str(var)) plt.legend() plt.tight_layout() if args.local: plt.show() else: plt.savefig( args.save_path + "/visualize_jacobian_" + str(num) + ".png") logger.info("Figure \"visualize_jacobian_" + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_presoft(cost, hidden_states, updates, train_stream, valid_stream, args): filter_presoft = VariableFilter(theano_name="presoft") presoft = filter_presoft(ComputationGraph(cost).variables)[0] # Get all the hidden_states filter_states = VariableFilter(theano_name_regex="hidden_state_.*") all_states = filter_states(hidden_states) all_states = sorted(all_states, key=lambda var: var.name[-1]) # Assertion part assert len(all_states) == args.layers logger.info("The computation of the gradients has started") gradients = [] for i in range(args.visualize_length - args.context): gradients.extend( tensor.grad(tensor.mean(tensor.abs_(presoft[i, 0, :])), all_states)) logger.info("The computation of the gradients is done") # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state(updates, 1, not (has_indices(args.dataset))) # Compile the function logger.info("The compilation of the function has started") compiled = theano.function(inputs=ComputationGraph(presoft).inputs, outputs=gradients, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) logger.info("The function has been compiled") # Generate epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0:args.visualize_length, 0:1] hidden_state = compiled(init_) value_of_layer = {} for d in range(args.layers): value_of_layer[d] = 0 for i in range(len(hidden_state) / args.layers): for d in range(args.layers): value_of_layer[d] += hidden_state[d + i * args.layers] time = hidden_state[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for d in range(args.layers): plt.plot(np.arange(time), np.mean(np.abs(value_of_layer[d][:, 0, :]), axis=1), label="Layer " + str(d)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("hidden_state_of_layer_" + str(d)) plt.legend() plt.tight_layout() if args.local: plt.show() else: plt.savefig(args.save_path + "/visualize_presoft_" + str(num) + ".png") logger.info("Figure \"visualize_presoft_" + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_gradients(hidden_states, updates, train_stream, valid_stream, args): # Get all the hidden_states filter_states = VariableFilter(theano_name_regex="hidden_state_.*") all_states = filter_states(hidden_states) all_states = sorted(all_states, key=lambda var: var.name[-1]) # Get all the hidden_cells filter_cells = VariableFilter(theano_name_regex="hidden_cell_.*") all_cells = filter_cells(hidden_states) all_cells = sorted(all_cells, key=lambda var: var.name[-1]) # Get the variable on which we compute the gradients filter_pre_rnn = VariableFilter(theano_name_regex="pre_rnn.*") wrt = filter_pre_rnn(ComputationGraph(hidden_states).variables) wrt = sorted(wrt, key=lambda var: var.name[-1]) len_wrt = len(wrt) # We have wrt = [pre_rnn] or [pre_rnn_0, pre_rnn_1, ...] # Assertion part assert len(all_states) == args.layers assert len(all_cells) == (args.layers * (args.rnn_type == "lstm")) if args.skip_connections: assert len_wrt == args.layers else: assert len_wrt == 1 # Comupute the gradients of states or cells if args.rnn_type == "lstm" and args.visualize_cells: states = all_cells else: states = all_states logger.info("The computation of the gradients has started") gradients = [] for i, state in enumerate(states): gradients.extend( tensor.grad(tensor.mean(tensor.abs_(state[-1, 0, :])), wrt[:i + 1])) # -1 indicates that gradient is gradient of the last time-step.c logger.info("The computation of the gradients is done") # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state( updates, 1, reset=not (has_indices(args.dataset))) # Compile the function logger.info("The compilation of the function has started") compiled = theano.function(inputs=ComputationGraph(states).inputs, outputs=gradients, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) logger.info("The function has been compiled") # Generate epoch_iterator = train_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0:args.visualize_length, 0:1] # [layers * len_wrt] [Time, 1, Hidden_dim] gradients = compiled(init_) if args.skip_connections: assert len(gradients) == (args.layers * (args.layers + 1)) / 2 else: assert len(gradients) == args.layers time = gradients[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) # One row subplot for each variable wrt which we are computing # the gradients for var in range(len_wrt): plt.subplot(len_wrt, 1, var + 1) for d in range(args.layers - var): plt.plot(np.arange(time), np.mean(np.abs(gradients[d][:, 0, :]), axis=1), label="layer " + str(d + var)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.yscale('log') axes = plt.gca() axes.set_ylim([5e-20, 5e-1]) plt.title("gradients plotting w.r.t pre_rrn" + str(var)) plt.legend() plt.tight_layout() if args.local: plt.show() else: plt.savefig(args.save_path + "/visualize_gradients_" + str(num) + ".png") logger.info("Figure \"visualize_gradients_" + str(num) + ".png\" saved at directory: " + args.save_path)
def visualize_gates_lstm(gate_values, hidden_states, updates, train_stream, valid_stream, args): in_gates = gate_values["in_gates"] out_gates = gate_values["out_gates"] forget_gates = gate_values["forget_gates"] # Handle the theano shared variables that allow carrying the hidden state givens, f_updates = carry_hidden_state(updates, 1, not (has_indices(args.dataset))) generate_in = theano.function(inputs=ComputationGraph(in_gates).inputs, outputs=in_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) generate_out = theano.function(inputs=ComputationGraph(out_gates).inputs, outputs=out_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) generate_forget = theano.function( inputs=ComputationGraph(forget_gates).inputs, outputs=forget_gates, givens=givens, updates=f_updates, mode=Mode(optimizer='fast_compile')) # Generate epoch_iterator = valid_stream.get_epoch_iterator() for num in range(10): init_ = next(epoch_iterator)[0][0:args.visualize_length, 0:1] last_output_in = generate_in(init_) last_output_out = generate_out(init_) last_output_forget = generate_forget(init_) layers = len(last_output_in) time = last_output_in[0].shape[0] if has_indices(args.dataset): ticks = tuple(conv_into_char(init_[:, 0], args.dataset)) else: ticks = tuple(np.arange(time)) for i in range(layers): plt.subplot(3, layers, 1 + i) plt.plot(np.arange(time), np.mean(np.abs(last_output_in[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("in_gate of layer " + str(i)) plt.subplot(3, layers, layers + 1 + i) plt.plot(np.arange(time), np.mean(np.abs(last_output_out[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("out_gate of layer " + str(i)) plt.subplot(3, layers, 2 * layers + 1 + i) plt.plot(np.arange(time), np.mean(np.abs(last_output_forget[i][:, 0, :]), axis=1)) plt.xticks(range(args.visualize_length), ticks) plt.grid(True) plt.title("forget_gate of layer " + str(i)) if args.local: plt.show() else: plt.savefig(args.save_path + "/visualize_gates_" + str(num) + ".png") logger.info("Figure \"visualize_gates_" + str(num) + ".png\" saved at directory: " + args.save_path)