Esempio n. 1
0
def test_create_param_bad_callable_raises():
    from lasagne.utils import create_param

    with pytest.raises(TypeError):
        create_param(lambda x: {}, (1, 2, 3))
    with pytest.raises(ValueError):
        create_param(lambda x: np.array(1), (1, 2, 3))
Esempio n. 2
0
def test_create_param_broadcast_pattern():
    from lasagne.utils import create_param
    for shape in (10, 1, 20), (1, 2), (3, 1), (2, 3):
        bcast = tuple(s == 1 for s in shape)
        assert create_param(np.zeros, shape).broadcastable == bcast
        assert create_param(np.zeros(shape, np.float32),
                            shape).broadcastable == bcast
Esempio n. 3
0
def test_create_param_bad_callable_raises():
    from lasagne.utils import create_param

    with pytest.raises(TypeError):
        create_param(lambda x: {}, (1, 2, 3))
    with pytest.raises(ValueError):
        create_param(lambda x: np.array(1), (1, 2, 3))
Esempio n. 4
0
def test_create_param_broadcast_pattern():
    from lasagne.utils import create_param
    for shape in (10, 1, 20), (1, 2), (3, 1), (2, 3):
        bcast = tuple(s == 1 for s in shape)
        assert create_param(np.zeros, shape).broadcastable == bcast
        assert create_param(np.zeros(shape, np.float32),
                            shape).broadcastable == bcast
Esempio n. 5
0
def test_create_param_callable_returns_wrong_type():
    from lasagne.utils import create_param

    param = 'string'
    factory = Mock()
    factory.return_value = param
    with pytest.raises(TypeError):
        create_param(factory, (1, 2))
Esempio n. 6
0
def test_create_param_callable_returns_wrong_type():
    from lasagne.utils import create_param

    param = 'string'
    factory = Mock()
    factory.return_value = param
    with pytest.raises(TypeError):
        create_param(factory, (1, 2))
Esempio n. 7
0
def test_create_param_callable_returns_shared_bad_ndim_raises_error():
    from lasagne.utils import create_param

    array = np.array([[1, 2], [3, 4]])
    param = theano.shared(array)
    factory = Mock()
    factory.return_value = param
    with pytest.raises(ValueError):
        create_param(factory, (2, 3, 4))
Esempio n. 8
0
def test_create_param_callable_returns_shared_bad_ndim_raises_error():
    from lasagne.utils import create_param

    array = np.array([[1, 2], [3, 4]])
    param = theano.shared(array)
    factory = Mock()
    factory.return_value = param
    with pytest.raises(ValueError):
        create_param(factory, (2, 3, 4))
Esempio n. 9
0
def test_create_param_retain_ndarray_dtype():
    from lasagne.utils import create_param
    param = np.array([[1, 2, 3], [4, 5, 6]])

    param = param.astype('float64')
    result = create_param(param, (2, 3))
    assert (result.dtype == param.dtype)

    param = param.astype('int16')
    result = create_param(param, (2, 3))
    assert (result.dtype == param.dtype)
Esempio n. 10
0
def test_create_param_retain_ndarray_dtype():
    from lasagne.utils import create_param
    param = np.array([[1, 2, 3], [4, 5, 6]])

    param = param.astype('float64')
    result = create_param(param, (2, 3))
    assert (result.dtype == param.dtype)

    param = param.astype('int16')
    result = create_param(param, (2, 3))
    assert (result.dtype == param.dtype)
Esempio n. 11
0
def test_create_param_numpy_returns_shared():
    from lasagne.utils import create_param

    param = np.array([[1, 2, 3], [4, 5, 6]])
    result = create_param(param, (2, 3))
    assert (result.get_value() == param).all()
    assert isinstance(result, type(theano.shared(param)))
    assert (result.get_value() == param).all()
Esempio n. 12
0
def test_create_param_numpy_returns_shared():
    from lasagne.utils import create_param

    param = np.array([[1, 2, 3], [4, 5, 6]])
    result = create_param(param, (2, 3))
    assert (result.get_value() == param).all()
    assert isinstance(result, type(theano.shared(param)))
    assert (result.get_value() == param).all()
Esempio n. 13
0
 def add_param(self, init, in_shape, name, type):
     par = create_param(init, in_shape, name)
     self.params.append(par)
     if type == 'r':
         self.r_params.append(par)
     elif type == 'p':
         self.p_params.append(par)
     return par
Esempio n. 14
0
def test_create_param_callable_returns_return_value():
    from lasagne.utils import create_param

    array = np.array([[1, 2, 3], [4, 5, 6]])
    factory = Mock()
    factory.return_value = array
    result = create_param(factory, (2, 3))
    assert (result.get_value() == array).all()
    factory.assert_called_with((2, 3))
Esempio n. 15
0
def test_create_param_callable_returns_return_value():
    from lasagne.utils import create_param

    array = np.array([[1, 2, 3], [4, 5, 6]])
    factory = Mock()
    factory.return_value = array
    result = create_param(factory, (2, 3))
    assert (result.get_value() == array).all()
    factory.assert_called_with((2, 3))
Esempio n. 16
0
    def __init__(self, incoming, num_units,
                Wh=init.Orthogonal(),
                Wx=init.Orthogonal(),
                b=init.Constant(0.),
                nonlinearity=nonlinearities.rectify,
                **kwargs):

        super(SimpleRNN, self).__init__(incoming, **kwargs)
        self.nonlinearity = (nonlinearities.identity if nonlinearity is None
                             else nonlinearity)

        self.num_units = num_units

        num_inputs = int(np.prod(self.input_shape[2:]))

        self.Wx = utils.create_param(Wx, (num_inputs, num_units), name="Wx")
        self.Wh = utils.create_param(Wh, (num_units, num_units), name="Wh")
        self.b = utils.create_param(b, (num_units,), name="b") if b is not None else None
Esempio n. 17
0
def test_create_param_callable_returns_theano_expr():
    from lasagne.utils import create_param

    array = np.array([[1, 2, 3], [4, 5, 6]])
    param = theano.shared(array) * 2
    factory = Mock()
    factory.return_value = param
    result = create_param(factory, (2, 3))
    assert (result.eval() == array * 2).all()
    assert result is param
Esempio n. 18
0
def test_create_param_callable_returns_theano_expr():
    from lasagne.utils import create_param

    array = np.array([[1, 2, 3], [4, 5, 6]])
    param = theano.shared(array) * 2
    factory = Mock()
    factory.return_value = param
    result = create_param(factory, (2, 3))
    assert (result.eval() == array * 2).all()
    assert result is param
Esempio n. 19
0
def test_nonpositive_dims_raises_value_error():
    from lasagne.utils import create_param
    neg_shape = (-1, -1)
    zero_shape = (0, 0)
    pos_shape = (1, 1)
    spec = np.empty
    with pytest.raises(ValueError):
        create_param(spec, neg_shape)
    with pytest.raises(ValueError):
        create_param(spec, zero_shape)
    create_param(spec, pos_shape)
Esempio n. 20
0
def test_nonpositive_dims_raises_value_error():
    from lasagne.utils import create_param
    neg_shape = (-1, -1)
    zero_shape = (0, 0)
    pos_shape = (1, 1)
    spec = np.empty
    with pytest.raises(ValueError):
        create_param(spec, neg_shape)
    with pytest.raises(ValueError):
        create_param(spec, zero_shape)
    create_param(spec, pos_shape)
Esempio n. 21
0
    def add_param(self, spec, shape, name=None, **tags):
        """
        Register and possibly initialize a parameter tensor for the layer.

        When defining a layer class, this method is called in the constructor
        to define which parameters the layer has, what their shapes are, how
        they should be initialized and what tags are associated with them.
        This allows layer classes to transparently support parameter
        initialization from numpy arrays and callables, as well as setting
        parameters to existing Theano shared variables or Theano expressions.

        All registered parameters are stored along with their tags in the
        ordered dictionary :attr:`Layer.params`, and can be retrieved with
        :meth:`Layer.get_params()`, optionally filtered by their tags.

        Parameters
        ----------
        spec : Theano shared variable, expression, numpy array or callable
            initial value, expression or initializer for this parameter.
            See :func:`lasagne.utils.create_param` for more information.

        shape : tuple of int
            a tuple of integers representing the desired shape of the
            parameter tensor.

        name : str (optional)
            a descriptive name for the parameter variable. This will be passed
            to ``theano.shared`` when the variable is created, prefixed by the
            layer's name if any (in the form ``'layer_name.param_name'``). If
            ``spec`` is already a shared variable or expression, this parameter
            will be ignored to avoid overwriting an existing name.

        **tags (optional)
            tags associated with the parameter can be specified as keyword
            arguments. To associate the tag ``tag1`` with the parameter, pass
            ``tag1=True``.

            By default, the tags ``regularizable`` and ``trainable`` are
            associated with the parameter. Pass ``regularizable=False`` or
            ``trainable=False`` respectively to prevent this.

        Returns
        -------
        Theano shared variable or Theano expression
            the resulting parameter variable or parameter expression

        Notes
        -----
        It is recommended to assign the resulting parameter variable/expression
        to an attribute of the layer for easy access, for example:

        >>> self.W = self.add_param(W, (2, 3), name='W')  #doctest: +SKIP
        """
        # prefix the param name with the layer name if it exists
        if name is not None:
            if self.name is not None:
                name = "%s.%s" % (self.name, name)
        # create shared variable, or pass through given variable/expression
        param = utils.create_param(spec, shape, name)
        # parameters should be trainable and regularizable by default
        tags['trainable'] = tags.get('trainable', True)
        tags['regularizable'] = tags.get('regularizable', True)
        self.params[param] = set(tag for tag, value in tags.items() if value)

        return param
Esempio n. 22
0
    def __init__(self, incoming, num_units,
                Wxi=init.Orthogonal(),
                Wxf=init.Orthogonal(),
                Wxo=init.Orthogonal(),
                Wxg=init.Orthogonal(),
                Whi=init.Orthogonal(),
                Whf=init.Orthogonal(),
                Who=init.Orthogonal(),
                Whg=init.Orthogonal(),
                bi=init.Constant(0.),
                bf=init.Constant(0.),
                bo=init.Constant(0.),
                bg=init.Constant(0.),
                nonlinearity=nonlinearities.tanh,
                **kwargs):

        super(LSTM, self).__init__(incoming, **kwargs)
        self.nonlinearity = (nonlinearities.identity if nonlinearity is None
                             else nonlinearity)

        self.num_units = num_units

        num_inputs = int(np.prod(self.input_shape[2:]))

        self.Wxi = utils.create_param(Wxi, (num_inputs, num_units), name="Wxi")
        self.Wxf = utils.create_param(Wxf, (num_inputs, num_units), name="Wxf")
        self.Wxo = utils.create_param(Wxo, (num_inputs, num_units), name="Wxo")
        self.Wxg = utils.create_param(Wxg, (num_inputs, num_units), name="Wxg")
        self.Whi = utils.create_param(Wxi, (num_units, num_units), name="Whi")
        self.Whf = utils.create_param(Wxf, (num_units, num_units), name="Whf")
        self.Who = utils.create_param(Wxo, (num_units, num_units), name="Who")
        self.Whg = utils.create_param(Wxg, (num_units, num_units), name="Whg")
        self.bi = utils.create_param(bi, (num_units,), name="bi") if bi is not None else None
        self.bf = utils.create_param(bf, (num_units,), name="bf") if bf is not None else None
        self.bo = utils.create_param(bo, (num_units,), name="bo") if bo is not None else None
        self.bg = utils.create_param(bg, (num_units,), name="bg") if bg is not None else None
Esempio n. 23
0
def test_create_param_shared_returns_same():
    from lasagne.utils import create_param

    param = theano.shared(np.array([[1, 2, 3], [4, 5, 6]]))
    result = create_param(param, (2, 3))
    assert result is param
Esempio n. 24
0
def test_create_param_shared_bad_ndim_raises_error():
    from lasagne.utils import create_param

    param = theano.shared(np.array([[1, 2, 3], [4, 5, 6]]))
    with pytest.raises(ValueError):
        create_param(param, (2, 3, 4))
Esempio n. 25
0
def test_create_param_shared_returns_same():
    from lasagne.utils import create_param

    param = theano.shared(np.array([[1, 2, 3], [4, 5, 6]]))
    result = create_param(param, (2, 3))
    assert result is param
Esempio n. 26
0
def test_create_param_numpy_generic_returns_same():
    from lasagne.utils import create_param

    param = np.int_(2)
    result = create_param(param, ())
    assert result.get_value() == param
Esempio n. 27
0
def test_create_param_number_returns_same():
    from lasagne.utils import create_param

    param = 1
    result = create_param(param, ())
    assert result.get_value() == param
Esempio n. 28
0
def test_create_param_shared_bad_ndim_raises_error():
    from lasagne.utils import create_param

    param = theano.shared(np.array([[1, 2, 3], [4, 5, 6]]))
    with pytest.raises(ValueError):
        create_param(param, (2, 3, 4))
Esempio n. 29
0
def test_create_param_numpy_bad_shape_raises_error():
    from lasagne.utils import create_param

    param = np.array([[1, 2, 3], [4, 5, 6]])
    with pytest.raises(ValueError):
        create_param(param, (3, 2))
Esempio n. 30
0
def test_create_param_accepts_iterable_shape():
    from lasagne.utils import create_param
    factory = np.empty
    create_param(factory, [2, 3])
    create_param(factory, (x for x in [2, 3]))
Esempio n. 31
0
def test_create_param_numpy_bad_shape_raises_error():
    from lasagne.utils import create_param

    param = np.array([[1, 2, 3], [4, 5, 6]])
    with pytest.raises(ValueError):
        create_param(param, (3, 2))
Esempio n. 32
0
def test_create_param_bad_spec_raises():
    from lasagne.utils import create_param

    with pytest.raises(TypeError):
        create_param({}, (1, 2, 3))
Esempio n. 33
0
def test_create_param_numpy_generic_returns_same():
    from lasagne.utils import create_param

    param = np.int_(2)
    result = create_param(param, ())
    assert result.get_value() == param
Esempio n. 34
0
 def make_b(self, size, name):
     P = create_param(Constant(0.), (size, ), name=name)
     self.parameters[name] = P
     return P
Esempio n. 35
0
def test_create_param_number_returns_same():
    from lasagne.utils import create_param

    param = 1
    result = create_param(param, ())
    assert result.get_value() == param
Esempio n. 36
0
def test_create_param_bad_spec_raises():
    from lasagne.utils import create_param

    with pytest.raises(TypeError):
        create_param({}, (1, 2, 3))
Esempio n. 37
0
 def make_W(self, num_in, num_out, name):
     P = create_param(self.igor.default_initializer, (num_in, num_out), name=name)
     self.parameters[name] = P
     return P
Esempio n. 38
0
def test_create_param_accepts_iterable_shape():
    from lasagne.utils import create_param
    factory = np.empty
    create_param(factory, [2, 3])
    create_param(factory, (x for x in [2, 3]))