def test_returns_layer(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Rectifier) next_layer = layer.create_next_layer( size=5, activation=activation_functions.Rectifier) self.assertIsInstance(next_layer, Layer)
def test_biases_is_of_correct_type(self): layer = Layer(size=5, prev_size=2, activation=activation_functions.Sigmoid) biases = layer.biases() self.assertIsInstance(biases, np.ndarray) self.assertIn(biases.dtype, [np.float32, np.float64])
def test_weights_is_of_correct_type(self): layer = Layer(size=5, prev_size=2, activation=activation_functions.Sigmoid) weights = layer.weights() self.assertIsInstance(weights, np.ndarray) self.assertIn(weights.dtype, [np.float32, np.float64])
def test_with_single_hidden_layer(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Sigmoid) biases = [-2, 0.5, 9] layer.set_biases(biases=np.array(biases, float)) self.assertEqual(layer.biases().tolist(), [-2, 0.5, 9])
def test(self): layer = Layer(size=1, prev_size=3, activation=activation_functions.Sigmoid) layer.randomize() x = np.array([1, 9, -2], float) a, z = layer.feed(x) self.assertNotEqual(a[0], 0)
def test_raises_exception_for_erroneous_index(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Sigmoid) self.assertRaises(IndexError, lambda: layer.set_bias(row=3, new_value=2)) self.assertRaises(IndexError, lambda: layer.set_bias(row=30, new_value=2))
def test_with_single_hidden_layer(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Sigmoid) weights = [[0, 10], [1, 1], [-1, 0.5]] layer.set_weights(weights=np.array(weights, float)) self.assertEqual(layer.weights().tolist(), [[0, 10], [1, 1], [-1, 0.5]])
def test_feed_with_sigmoid(self): layer = Layer(size=2, prev_size=3, activation=activation_functions.Sigmoid) x = np.array([1, 9, 323], float) a, z = layer.feed(x) self.assertEqual(z[0], 0) self.assertEqual(z[1], 0) self.assertEqual(a[0], 0.5) self.assertEqual(a[1], 0.5)
def test_raises_exception_for_array_with_wrong_dimension(self): layer = Layer(size=5, prev_size=2, activation=activation_functions.Sigmoid) self.assertRaises(Layer.InvalidMatrixDimensions, lambda: layer.set_biases(biases=np.zeros((2, 2)))) self.assertRaises(Layer.InvalidMatrixDimensions, lambda: layer.set_biases(biases=np.zeros((4, 3))))
def test_init_empty_layers(self): self.assertRaises( Layer.BadArchitecture, lambda: Layer( size=0, prev_size=0, activation=activation_functions.Sigmoid)) self.assertRaises( Layer.BadArchitecture, lambda: Layer( size=1, prev_size=0, activation=activation_functions.Sigmoid)) self.assertRaises( Layer.BadArchitecture, lambda: Layer( size=0, prev_size=1, activation=activation_functions.Sigmoid))
def test_created_layer_has_correct_parameters(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Rectifier) next_layer = layer.create_next_layer( size=5, activation=activation_functions.Rectifier) self.assertTupleEqual(next_layer.biases().shape, (5, )) self.assertTupleEqual(next_layer.weights().shape, (5, 3)) next_layer = next_layer.create_next_layer( size=4, activation=activation_functions.Rectifier) self.assertTupleEqual(next_layer.biases().shape, (4, )) self.assertTupleEqual(next_layer.weights().shape, (4, 5))
def test_created_layer_has_correct_activation_function(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Rectifier) next_layer = layer.create_next_layer( size=5, activation=activation_functions.Rectifier) self.assertEqual(next_layer.get_activation(), activation_functions.Rectifier) next_layer = next_layer.create_next_layer( size=5, activation=activation_functions.Sigmoid) self.assertEqual(next_layer.get_activation(), activation_functions.Sigmoid)
def test_set_bias(self): layer = Layer(size=3, prev_size=2, activation=activation_functions.Sigmoid) layer.set_bias(row=0, new_value=2) layer.set_bias(row=1, new_value=-0.5) expected_b = np.array([2, -0.5, 0], float) self.assertEqual(layer.biases().tolist(), expected_b.tolist())
def test_set_weight(self): layer = Layer(size=2, prev_size=3, activation=activation_functions.Sigmoid) layer.set_weight(row=0, col=1, new_value=2) layer.set_weight(row=1, col=2, new_value=-0.5) expected_w = np.array([[0, 2, 0], [0, 0, -0.5]], float) self.assertEqual(layer.weights().tolist(), expected_w.tolist())
def build_network(self): tf.reset_default_graph() # This is essential for doing multiple runs!! num_inputs = self.layer_dimensions[0] self.inputs = tf.placeholder(tf.float32, shape=(None, self.time_lags + 1, num_inputs), name='Input') input_variables = self.inputs input_size = num_inputs self.layers = [] self.cells = [] self.drop_cells = [] # Build all of the modules #layer_size = [input, h1, h2, h3, output] # i er layer nr i og outsize er storrelsen paa layer nr i for layer_index, number_of_neurons in enumerate( self.layer_dimensions[1:]): layer = l.Layer(self, layer_index, input_variables, input_size, number_of_neurons, self.time_lags) act_func = self.get_activation_function(layer_index) num_units = number_of_neurons self.layers.append(layer) cell = tf.contrib.rnn.BasicRNNCell(num_units, activation=act_func) if (layer_index == 0 ): #indicates that it is the first hidden layer drop_cell = tf.contrib.rnn.DropoutWrapper( cell, input_keep_prob=self.keep_probability_for_dropout[0]) else: drop_cell = tf.contrib.rnn.DropoutWrapper( cell, input_keep_prob=self.keep_probability_for_dropout[1]) self.cells.append(cell) self.drop_cells.append(drop_cell) #input_variables = layer.output_variables #input_size = layer.output_size multi_layer_cell = tf.contrib.rnn.MultiRNNCell(self.drop_cells) self.output_variables, self.states = tf.nn.dynamic_rnn( multi_layer_cell, self.inputs, dtype=tf.float32) self.targets = tf.placeholder(tf.float32, shape=(None, self.time_lags + 1, layer.output_size), name='Target') self.configure_learning()
def test_rectifier(self): layer = Layer(size=2, prev_size=2, activation=activation_functions.Sigmoid) layer.set_activation(activation_functions.Rectifier) self.assertEqual(layer.get_activation().__class__, activation_functions.Rectifier.__class__) x = np.array([1, 9], float) a, z = layer.feed(x) self.assertEqual(a[0], 0) self.assertEqual(a[1], 0)
def test_biases_is_correct_vector(self): layer = Layer(size=5, prev_size=2, activation=activation_functions.Sigmoid) biases = layer.biases() self.assertTupleEqual(biases.shape, (5, )) layer = Layer(size=2, prev_size=3, activation=activation_functions.Sigmoid) biases = layer.biases() self.assertTupleEqual(biases.shape, (2, ))
def test_weights_is_correct_matrix(self): layer = Layer(size=5, prev_size=2, activation=activation_functions.Sigmoid) weights = layer.weights() self.assertTupleEqual(weights.shape, (5, 2)) layer = Layer(size=2, prev_size=3, activation=activation_functions.Sigmoid) weights = layer.weights() self.assertTupleEqual(weights.shape, (2, 3))