def __init__(self, input_size, read_size, output_size, discourage_pop=True, hidden_size=10, n_args=2, **kwargs): """ Constructor for the LSTMSimpleStructController object. :type input_size: int :param input_size: The size of input vectors to this Controller :type read_size: int :param read_size: The size of vectors placed on the neural data structure :type output_size: int :param output_size: The size of vectors output from this Controller :type discourage_pop: bool :param discourage_pop: If True, then weights will be initialized to discourage popping :type hidden_size: int :param hidden_size: The size of state vectors :type n_args: int :param n_args: The number of struct instructions, apart from the value to push onto the struct, that will be computed by the controller. By default, this value is 2: the push strength and the pop strength """ super(LSTMSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.iteritems(): unused_init_param(param_name, arg_value, self) self._hidden = None self._cell_state = None # Create an LSTM Module object nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size + self._output_size self._lstm = nn.LSTMCell(nn_input_size, hidden_size) self._linear = nn.Linear(hidden_size, nn_output_size) # Initialize Module weights LSTMSimpleStructController.init_normal(self._lstm.weight_hh) LSTMSimpleStructController.init_normal(self._lstm.weight_ih) self._lstm.bias_hh.data.fill_(0) self._lstm.bias_ih.data.fill_(0) LSTMSimpleStructController.init_normal(self._linear.weight) self._linear.bias.data.fill_(0) if discourage_pop: self._linear.bias.data[0] = -1. # Discourage popping if n_args >= 4: self._linear.bias.data[2] = 1. # Encourage reading self._linear.bias.data[3] = 1. # Encourage writing
def __init__( self, input_size, read_size, output_size, n_args=2, # TODO: These network classes could be refactored. # TODO: Remove/move initialization out of constructor. custom_initialization=True, discourage_pop=True, **kwargs): """ Constructor for the LinearSimpleStruct object. :type input_size: int :param input_size: The size of input vectors to this Controller :type read_size: int :param read_size: The size of vectors placed on the neural data structure :type output_size: int :param output_size: The size of vectors output from this Controller :type n_args: int :param n_args: The number of struct instructions, apart from the value to push onto the struct, that will be computed by the controller. By default, this value is 2: the push strength and the pop strength :type discourage_pop: bool :param discourage_pop: If True, then weights will be initialized to discourage popping """ super(LinearSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.iteritems(): unused_init_param(param_name, arg_value, self) # Create a Linear Module object nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size + self._output_size self._linear = nn.Linear(nn_input_size, nn_output_size) if custom_initialization: LinearSimpleStructController.init_normal(self._linear.weight) self._linear.bias.data.fill_(0) if discourage_pop: self._linear.bias.data[0] = -1. # Discourage popping if n_args >= 4: self._linear.bias.data[2] = 1. # Encourage reading self._linear.bias.data[3] = 1. # Encourage writing
def __init__(self, input_size, read_size, output_size, n_args=2, discourage_pop=True, n_hidden_layers=2, non_linearity=nn.ReLU, **kwargs): """ Constructor for the DeepSimpleStructController object. :type input_size: int :param input_size: The size of input vectors to this Controller :type read_size: int :param read_size: The size of vectors placed on the neural data structure :type output_size: int :param output_size: The size of vectors output from this Controller :type n_args: int :param n_args: The number of struct instructions, apart from the value to push onto the struct, that will be computed by the controller. By default, this value is 2: the push strength and the pop strength :type discourage_pop: bool :param discourage_pop: If True, then weights will be initialized to discourage popping :type n_hidden_layers: int :param n_hidden_layers: How many feedforward layers :type non_linearity: Module :param non_linearity: Non-linearity to apply to hidden layers """ super(DeepSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.iteritems(): unused_init_param(param_name, arg_value, self) # Create a Multilayer NN nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size + self._output_size nn_hidden_size = int(ceil((nn_input_size + nn_output_size) / 2.0)) nn_sizes_list = [nn_input_size] + [nn_hidden_size] * n_hidden_layers self._network = nn.Sequential() for i in range(n_hidden_layers): self._network.add_module('lin' + str(i), nn.Linear(nn_sizes_list[i], nn_sizes_list[i + 1])) self._network.add_module('relu' + str(i), non_linearity()) self._network.add_module('out', nn.Linear(nn_sizes_list[-1], nn_output_size)) # Initialize Module weights self.discourage_pop = discourage_pop self._network.apply(self.init_weights)
def __init__(self, input_size, read_size, output_size, n_args=2, discourage_pop=True, n_hidden_layers=2, non_linearity=nn.ReLU, **kwargs): """ Constructor for the DeepSimpleStructController object. :type input_size: int :param input_size: The size of input vectors to this Controller :type read_size: int :param read_size: The size of vectors placed on the neural data structure :type output_size: int :param output_size: The size of vectors output from this Controller :type n_args: int :param n_args: The number of struct instructions, apart from the value to push onto the struct, that will be computed by the controller. By default, this value is 2: the push strength and the pop strength :type discourage_pop: bool :param discourage_pop: If True, then weights will be initialized to discourage popping :type n_hidden_layers: int :param n_hidden_layers: How many feedforward layers :type non_linearity: Module :param non_linearity: Non-linearity to apply to hidden layers """ super(DeepSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.iteritems(): unused_init_param(param_name, arg_value, self) # Create a Multilayer NN nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size + self._output_size nn_hidden_size = int(ceil((nn_input_size+nn_output_size)/2.0)) nn_sizes_list = [nn_input_size] + [nn_hidden_size]*n_hidden_layers self._network = nn.Sequential() for i in range(n_hidden_layers): self._network.add_module('lin'+str(i), nn.Linear(nn_sizes_list[i], nn_sizes_list[i+1])) self._network.add_module('relu'+str(i), non_linearity()) self._network.add_module('out', nn.Linear(nn_sizes_list[-1], nn_output_size)) # Initialize Module weights self.discourage_pop = discourage_pop self._network.apply(self.init_weights)
def __init__(self, input_size, read_size, output_size, custom_initialization=False, discourage_pop=False, hidden_size=16, n_args=2, **kwargs): super(PDAGRUSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.items(): unused_init_param(param_name, arg_value, self) self._hidden = None self._cell_state = None # Create an GRU Module object nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size * 2 + self._output_size self._gru = nn.GRUCell(nn_input_size, hidden_size) self._linear_nargs = nn.Linear(hidden_size, self._n_args) #self._sigmoid_nargs = nn.Sigmoid() self._sigmoid_nargs = Sigmaid.apply self._linear_v1 = nn.Linear(hidden_size, self._read_size) self._tanh_v1 = nn.Tanh() self._linear_v2 = nn.Linear(hidden_size, self._read_size) self._tanh_v2 = nn.Tanh() self._linear_o = nn.Linear(hidden_size, self._output_size) self._tanh_o = nn.Tanh() if custom_initialization: PDAGRUSimpleStructController.init_normal(self._gru.weight_hh) PDAGRUSimpleStructController.init_normal(self._gru.weight_ih) self._gru.bias_hh.data.fill_(0) self._gru.bias_ih.data.fill_(0) PDAGRUSimpleStructController.init_normal(self._linear.weight) self._linear.bias.data.fill_(0) if discourage_pop: self._linear.bias.data[0] = -1. # Discourage popping if n_args >= 4: self._linear.bias.data[2] = 1. # Encourage reading self._linear.bias.data[3] = 1. # Encourage writing
def trace(self, trace_x, *args): """ Draws a graphic representation of the neural data structure instructions produced by the Model's Controller at each time step for a single input. :type trace_x: Variable :param trace_x: An input string :return: None """ for arg in args: unused_init_param("num_steps", arg, self) self.eval() self.init_model(1, trace_x) max_length = trace_x.data.shape[1] self._controller.start_log(max_length) for j in xrange(max_length): self.forward() self._controller.stop_log() x_labels = ["x_" + str(i) for i in xrange(self._input_size)] y_labels = ["y_" + str(i) for i in xrange(self._output_size)] i_labels = ["Pop", "Push"] v_labels = ["v_" + str(i) for i in xrange(self._read_size)] labels = x_labels + y_labels + i_labels + v_labels import matplotlib.pyplot as plt plt.imshow(self._controller.log_data, cmap="hot", interpolation="nearest") plt.title("Trace") plt.yticks(range(len(labels)), labels) plt.xlabel("Time") plt.ylabel("Value") plt.show()
def trace(self, trace_x, *args): """ Draws a graphic representation of the neural data structure instructions produced by the Model's Controller at each time step for a single input. :type trace_x: Variable :param trace_x: An input string :return: None """ for arg in args: unused_init_param("num_steps", arg, self) self.eval() self.init_model(1, trace_x) max_length = trace_x.data.shape[1] self._controller.start_log(max_length) for j in xrange(max_length): self.forward() self._controller.stop_log() x_labels = ["x_" + str(i) for i in xrange(self._input_size)] y_labels = ["y_" + str(i) for i in xrange(self._output_size)] i_labels = ["Pop", "Push"] v_labels = ["v_" + str(i) for i in xrange(self._read_size)] labels = x_labels + y_labels + i_labels + v_labels plt.imshow(self._controller.log_data, cmap="hot", interpolation="nearest") plt.title("Trace") plt.yticks(range(len(labels)), labels) plt.xlabel("Time") plt.ylabel("Value") plt.show()
def trace_step(self, trace_x, num_steps=None, step=True): """ Steps through the neural network's computation. The controller will read an input and produce an output. At each time step, a summary of the controller's state and actions will be printed to the console. :type trace_x: Variable :param trace_x: A single input string :type num_steps: int :param num_steps: Please do not pass anything to this param :type step: bool :param step: If True, the user will need to press Enter in the console after each computation step :return: None """ if num_steps is not None: unused_init_param("num_steps", num_steps, self) if trace_x.data.shape[0] != 1: raise ValueError("You can only trace one input at a time!") self.eval() self.init_model(1, trace_x) x_end = self._input_size y_end = x_end + self._output_size push = y_end + 1 v_start = push + 1 max_length = trace_x.data.shape[1] self._controller.start_log(max_length) for j in xrange(max_length): print("\n-- Step {} of {} --".format(j, max_length)) self() i = self._controller.log_data[:x_end, j] o = self._controller.log_data[x_end:y_end, j].round(decimals=4) u = self._controller.log_data[y_end, j].round(decimals=4) d = self._controller.log_data[push, j].round(decimals=4) v = self._controller.log_data[v_start:, j].round(decimals=4) r = self._struct.read(1).data.numpy()[0].round(decimals=4) print("\nInput: " + str(i)) print("Output: " + str(o)) print("\nPop Strength: " + str(u)) print("\nPush Vector: " + str(v)) print("Push Strength: " + str(d)) print("\nRead Vector: " + str(r)) print("Struct Contents: ") self._struct.print_summary(0) if step: raw_input("\nPress Enter to continue\n") self._controller.stop_log()
def __init__(self, input_size, read_size, output_size, custom_initialization=True, discourage_pop=True, hidden_size=10, n_args=2, **kwargs): """ Constructor for the GRUSimpleStructController object. :type input_size: int :param input_size: The size of input vectors to this Controller :type read_size: int :param read_size: The size of vectors placed on the neural data structure :type output_size: int :param output_size: The size of vectors output from this Controller :type discourage_pop: bool :param discourage_pop: If True, then weights will be initialized to discourage popping :type hidden_size: int :param hidden_size: The size of the hidden state vector :type n_args: int :param n_args: The number of struct instructions, apart from the value to push onto the struct, that will be computed by the controller. By default, this value is 2: the push strength and the pop strength """ super(GRUSimpleStructController, self).__init__(input_size, read_size, output_size, n_args=n_args) for param_name, arg_value in kwargs.iteritems(): unused_init_param(param_name, arg_value, self) self._hidden = None # Create an GRU Module object nn_input_size = self._input_size + self._read_size nn_output_size = self._n_args + self._read_size + self._output_size self._GRU = nn.GRUCell(nn_input_size, hidden_size) self._linear = nn.Linear(hidden_size, nn_output_size) if custom_initialization: GRUSimpleStructController.init_normal(self._GRU.weight_hh) GRUSimpleStructController.init_normal(self._GRU.weight_ih) self._GRU.bias_hh.data.fill_(0) self._GRU.bias_ih.data.fill_(0) GRUSimpleStructController.init_normal(self._linear.weight) self._linear.bias.data.fill_(0) if discourage_pop: self._linear.bias.data[0] = -1. # Discourage popping if n_args >= 4: self._linear.bias.data[2] = 1. # Encourage reading self._linear.bias.data[3] = 1. # Encourage writing
def trace_step(self, trace_x, num_steps=None, step=True): """ Steps through the neural network's computation. The controller will read an input and produce an output. At each time step, a summary of the controller's state and actions will be printed to the console. :type trace_x: Variable :param trace_x: A single input string :type num_steps: int :param num_steps: Please do not pass anything to this param :type step: bool :param step: If True, the user will need to press Enter in the console after each computation step :return: None """ if num_steps is not None: unused_init_param("num_steps", num_steps, self) if trace_x.data.shape[0] != 1: raise ValueError("You can only trace one input at a time!") self.eval() self.init_model(1, trace_x) x_end = self._input_size y_end = x_end + self._output_size push = y_end + 1 v_start = push + 1 max_length = trace_x.data.shape[1] self._controller.start_log(max_length) for j in xrange(max_length): print "\n-- Step {} of {} --".format(j, max_length) self.forward() i = self._controller.log_data[:x_end, j] o = self._controller.log_data[x_end:y_end, j].round(decimals=4) u = self._controller.log_data[y_end, j].round(decimals=4) d = self._controller.log_data[push, j].round(decimals=4) v = self._controller.log_data[v_start:, j].round(decimals=4) r = self._struct.read(1).data.numpy()[0].round(decimals=4) print "\nInput: " + str(i) print "Output: " + str(o) print "\nPop Strength: " + str(u) print "\nPush Vector: " + str(v) print "Push Strength: " + str(d) print "\nRead Vector: " + str(r) print "Struct Contents: " self._struct.print_summary(0) if step: raw_input("\nPress Enter to continue\n") self._controller.stop_log()