def test_layer_from_shape(self, layer_from_shape, get_output_shape): layer = layer_from_shape input_shapes = {layer: (4, 5, 6)} assert get_output_shape(layer, input_shapes) is input_shapes[layer] input_shapes = {None: (4, 5, 6)} layer.get_output_shape_for = Mock() assert (get_output_shape(layer, input_shapes) is layer.get_output_shape_for.return_value) layer.get_output_shape_for.assert_called_with(input_shapes[None])
def test_get_output_shape_without_arguments(self, layers, get_output_shape): l1, l2, l3 = layers output = get_output_shape(l3) # expected: l3.output_shape assert get_output_shape(l3) is l3.output_shape # l3.get_output_shape_for, l2[*].get_output_shape_for should not have # been called assert l3.get_output_shape_for.call_count == 0 assert l2[0].get_output_shape_for.call_count == 0 assert l2[1].get_output_shape_for.call_count == 0
def test_get_output_shape_input_is_a_mapping_no_key(self, layers, get_output_shape): l1, l2, l3 = layers output = get_output_shape(l3, {}) # expected: l3.output_shape assert get_output_shape(l3) is l3.output_shape # l3.get_output_shape_for, l2[*].get_output_shape_for should not have # been called assert l3.get_output_shape_for.call_count == 0 assert l2[0].get_output_shape_for.call_count == 0 assert l2[1].get_output_shape_for.call_count == 0
def test_get_output_shape_with_single_argument_fails(self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) # expected to fail: only gave one shape tuple for two input layers with pytest.raises(ValueError): output_shape = get_output_shape(l3, shp)
def test_get_output_shape_with_single_argument_fails( self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) # expected to fail: only gave one shape tuple for two input layers with pytest.raises(ValueError): output_shape = get_output_shape(l3, shp)
def test_embedding_2D_input(): import numpy as np import theano import theano.tensor as T from lasagne.layers import EmbeddingLayer, InputLayer, helper x = T.imatrix() batch_size = 2 seq_len = 3 emb_size = 5 vocab_size = 3 l_in = InputLayer((None, seq_len)) W = np.arange( vocab_size*emb_size).reshape((vocab_size, emb_size)).astype('float32') l1 = EmbeddingLayer(l_in, input_size=vocab_size, output_size=emb_size, W=W) x_test = np.array([[0, 1, 2], [0, 0, 2]], dtype='int32') # check output shape assert helper.get_output_shape( l1, (batch_size, seq_len)) == (batch_size, seq_len, emb_size) output = helper.get_output(l1, x) f = theano.function([x], output) np.testing.assert_array_almost_equal(f(x_test), W[x_test])
def test_get_output_shape_with_single_argument(self, layers, get_output_shape): l1, l2, l3 = layers shp = (3, 4, 5) output_shape = get_output_shape(l3, shp) # expected: l3.get_output_shape_for(l2.get_output_shape_for(shp)) assert output_shape is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with( l2.get_output_shape_for.return_value) l2.get_output_shape_for.assert_called_with(shp)
def test_get_output_shape_without_arguments(self, layers, get_output_shape): l1, l2, l3 = layers output_shape = get_output_shape(l3) # expected: l3.output_shape assert output_shape is l3.output_shape # l3.get_output_shape_for, l2.get_output_shape_for should not have been # called assert l3.get_output_shape_for.call_count == 0 assert l2.get_output_shape_for.call_count == 0
def test_get_output_shape_input_is_a_mapping(self, layers, get_output_shape): l1, l2, l3 = layers input_shapes = {l3: (4, 5, 6)} # expected: input_shapes[l3] assert get_output_shape(l3, input_shapes) is input_shapes[l3] # l3.get_output_shape_for, l2.get_output_shape_for should not have been # called assert l3.get_output_shape_for.call_count == 0 assert l2.get_output_shape_for.call_count == 0
def test_get_output_shape_input_is_a_mapping_no_key( self, layers, get_output_shape): l1, l2, l3 = layers output_shape = get_output_shape(l3, {}) # expected: l3.output_shape assert output_shape is l3.output_shape # l3.get_output_shape_for, l2.get_output_shape_for should not have been # called assert l3.get_output_shape_for.call_count == 0 assert l2.get_output_shape_for.call_count == 0
def test_get_output_shape_input_is_a_mapping_for_input_layer( self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l1: shp} output_shape = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for(l2.get_output_shape_for(shp)) assert output_shape is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with( l2.get_output_shape_for.return_value) l2.get_output_shape_for.assert_called_with(shp)
def test_get_output_shape_input_is_a_mapping_for_layer( self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l2: shp} output_shape = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for(shp) assert output_shape is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with(shp) # l2.get_output_shape_for should not have been called assert l2.get_output_shape_for.call_count == 0
def test_get_output_shape_input_is_a_mapping_for_layer(self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l2: shp} output_shape = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for(shp) assert output_shape is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with(shp) # l2.get_output_shape_for should not have been called assert l2.get_output_shape_for.call_count == 0
def test(precompute, order, learn_init, unroll_scan): in_l1 = L.InputLayer((5, 3, 12), name="input") in_l2 = L.InputLayer((5, 3, 13), name="input") n_in = 6 if order == "TND" else 10 step_l = L.RENStep((n_in, 25), 10, 25, name="cell", pre_compute_input=precompute, learn_init=learn_init) rec_l = L.RNNLayer((in_l1, in_l2), step_l, name="rec", in_order=order, unroll_scan=unroll_scan) r1 = theano.shared(np.random.randn(5, 3, 12).astype(theano.config.floatX)) r2 = theano.shared(np.random.randn(5, 3, 13).astype(theano.config.floatX)) out = h.get_output(rec_l, inputs={in_l1: r1, in_l2: r2}).eval() print("Predicted:", h.get_output_shape(rec_l)) print("Actual: ", out.shape) print("Min-max [{:.3f}, {:.3f}]".format(np.min(out), np.max(out)))
def test_get_output_shape_input_is_a_mapping_for_layer(self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l2[0]: shp} output = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for( # [shp, l2[1].get_output_shape_for(l1[1].shape)]) assert output is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with([ shp, l2[1].get_output_shape_for.return_value]) l2[1].get_output_shape_for.assert_called_with(l1[1].shape) # l2[0].get_output_shape_for should not have been called assert l2[0].get_output_shape_for.call_count == 0
def test_gru_return_shape(): num_batch, seq_len, n_features1, n_features2 = 5, 3, 10, 11 num_units = 6 x = T.tensor4() in_shp = (num_batch, seq_len, n_features1, n_features2) l_inp = InputLayer(in_shp) l_rec = GRULayer(l_inp, num_units=num_units) x_in = np.random.random(in_shp).astype('float32') output = helper.get_output(l_rec, x) output_val = output.eval({x: x_in}) assert helper.get_output_shape(l_rec, x_in.shape) == output_val.shape assert output_val.shape == (num_batch, seq_len, num_units)
def test_get_output_shape_input_is_a_mapping_for_layer( self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l2[0]: shp} output = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for( # [shp, l2[1].get_output_shape_for(l1[1].shape)]) assert output is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with( [shp, l2[1].get_output_shape_for.return_value]) l2[1].get_output_shape_for.assert_called_with(l1[1].shape) # l2[0].get_output_shape_for should not have been called assert l2[0].get_output_shape_for.call_count == 0
def test_recurrent_return_shape(): num_batch, seq_len, n_features1, n_features2 = 5, 3, 10, 11 num_units = 6 x = T.tensor4() in_shp = (num_batch, seq_len, n_features1, n_features2) l_inp = InputLayer(in_shp) l_rec = RecurrentLayer(l_inp, num_units=num_units) x_in = np.random.random(in_shp).astype('float32') l_out = helper.get_output(l_rec, x) f_rec = theano.function([x], l_out) f_out = f_rec(x_in) assert helper.get_output_shape(l_rec, x_in.shape) == f_out.shape assert f_out.shape == (num_batch, seq_len, num_units)
def test_get_output_shape_input_is_a_mapping_for_input_layer( self, layers, get_output_shape): l1, l2, l3 = layers shp = (4, 5, 6) input_shapes = {l1[0]: shp} output = get_output_shape(l3, input_shapes) # expected: l3.get_output_shape_for( # [l2[0].get_output_shape_for(shp), # l2[1].get_output_shape_for(l1[1].shape)]) assert output is l3.get_output_shape_for.return_value l3.get_output_shape_for.assert_called_with([ l2[0].get_output_shape_for.return_value, l2[1].get_output_shape_for.return_value, ]) l2[0].get_output_shape_for.assert_called_with(shp) l2[1].get_output_shape_for.assert_called_with(l1[1].shape)
def test_embedding(): import numpy as np import theano import theano.tensor as T from lasagne.layers import EmbeddingLayer, InputLayer, helper x = T.ivector() l_in = InputLayer((3, )) l1 = EmbeddingLayer(l_in, input_size=3, output_size=5, W=np.arange(3*5).reshape((3, 5)).astype('float32')) # check output shape assert helper.get_output_shape(l1, (2, )) == (2, 5) output = helper.get_output(l1, x) f = theano.function([x], output) x_test = np.array([0, 2]).astype('int32') output_correct = np.array( [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]], dtype='float32') np.testing.assert_array_almost_equal(f(x_test), output_correct)
def test_get_output_shape_without_arguments(self, layer, get_output_shape): assert get_output_shape(layer) == (3, 2)
def test_get_output_shape_input_is_tuple(self, layer, get_output_shape): shp = (4, 5, 6) assert get_output_shape(layer, shp) == shp
def test_get_output_shape_input_is_a_mapping(self, layer, get_output_shape): input_shapes = {layer: (4, 5, 6)} assert get_output_shape(layer, input_shapes) == input_shapes[layer]