Example #1
0
def test_restruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(restruct(aref.ravel(), np.zeros((3, 5, 2))), aref), \
        "Numpy ndarray restruct"

    # Numeric (preserves floating points)
    assert restruct(np.array(2.2), 0) == 2.2, "Integer restruct"
    assert restruct(np.array(-5.1), 0.0) == -5.1, "Float restruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'b': np.ones(2),
        'c': np.linspace(0, 1, 6).reshape(3, 2)
    }
    dzeros = {
        'a': np.zeros((1, 5)),
        'b': np.zeros(2),
        'c': np.zeros((3, 2))
    }
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    for key, val in list(restruct(darray, dzeros).items()):
        assert np.allclose(dref[key], val), "Dict restruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    lzeros = [np.zeros((2, 2)), np.zeros(2), np.zeros((2, 2))]
    for idx, val in enumerate(restruct(larray, lzeros)):
        assert np.allclose(lref[idx], val), "List restruct"

    # Tuple
    for idx, val in enumerate(restruct(larray, tuple(lzeros))):
        assert np.allclose(lref[idx], val), "Tuple restruct"

    # Composed / mixed types
    lref = [np.eye(2), {'a': np.array([-1, 1]), 'b': np.arange(3)}, 7]
    lzeros = [np.zeros((2, 2)), {'a': np.zeros(2), 'b': np.zeros(3)}, 0]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(destruct(restruct(larray, lzeros)), destruct(lref)), "List of mixed types"

    dref = {
        'a': [3.5, 1.2, -33],
        'b': np.arange(6).reshape(2, 3),
        'c': 7
    }
    dzeros = {
        'a': [0, 0, 0],
        'b': np.zeros((2, 3)),
        'c': 0
    }
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(destruct(restruct(darray, dzeros)), destruct(dref)), "Dictionary of mixed types"
Example #2
0
def test_destruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(aref.ravel(), destruct(aref)), "Numpy ndarray destruct"

    # Numeric
    assert np.array(2.0) == destruct(2), "Integer destruct"
    assert np.array(-5.0) == destruct(-5.0), "Float destruct"

    # Dictionary
    dref = {"a": np.arange(5).reshape(1, 5), "c": np.linspace(0, 1, 6).reshape(3, 2), "b": np.ones(2)}
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    assert np.allclose(darray, destruct(dref)), "Dictionary destruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    assert np.allclose(larray, destruct(lref)), "List destruct"

    # Tuple
    assert np.allclose(larray, destruct(tuple(lref))), "Tuple destruct"

    # Composed / mixed types
    lref = [np.eye(2), {"a": np.array([-1, 1]), "b": np.arange(3)}, 7]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(larray, destruct(lref)), "List of mixed types"

    dref = {"a": [3.5, 1.2, -33], "b": np.arange(6).reshape(2, 3), "c": 7}
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(darray, destruct(dref)), "Dictionary of mixed types"
Example #3
0
def test_restruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(restruct(aref.ravel(), np.zeros((3, 5, 2))), aref), \
        "Numpy ndarray restruct"

    # Numeric (preserves floating points)
    assert restruct(np.array(2.2), 0) == 2.2, "Integer restruct"
    assert restruct(np.array(-5.1), 0.0) == -5.1, "Float restruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'b': np.ones(2),
        'c': np.linspace(0, 1, 6).reshape(3, 2)
    }
    dzeros = {'a': np.zeros((1, 5)), 'b': np.zeros(2), 'c': np.zeros((3, 2))}
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    for key, val in list(restruct(darray, dzeros).items()):
        assert np.allclose(dref[key], val), "Dict restruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    lzeros = [np.zeros((2, 2)), np.zeros(2), np.zeros((2, 2))]
    for idx, val in enumerate(restruct(larray, lzeros)):
        assert np.allclose(lref[idx], val), "List restruct"

    # Tuple
    for idx, val in enumerate(restruct(larray, tuple(lzeros))):
        assert np.allclose(lref[idx], val), "Tuple restruct"

    # Composed / mixed types
    lref = [np.eye(2), {'a': np.array([-1, 1]), 'b': np.arange(3)}, 7]
    lzeros = [np.zeros((2, 2)), {'a': np.zeros(2), 'b': np.zeros(3)}, 0]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(destruct(restruct(larray, lzeros)),
                       destruct(lref)), "List of mixed types"

    dref = {'a': [3.5, 1.2, -33], 'b': np.arange(6).reshape(2, 3), 'c': 7}
    dzeros = {'a': [0, 0, 0], 'b': np.zeros((2, 3)), 'c': 0}
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(destruct(restruct(darray, dzeros)),
                       destruct(dref)), "Dictionary of mixed types"
Example #4
0
def test_destruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(aref.ravel(), destruct(aref)), "Numpy ndarray destruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'c': np.linspace(0, 1, 6).reshape(3, 2),
        'b': np.ones(2)
    }
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    assert np.allclose(darray, destruct(dref)), "Dictionary destruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    assert np.allclose(larray, destruct(lref)), "List destruct"

    # Tuple
    assert np.allclose(larray, destruct(tuple(lref))), "Tuple destruct"

    # Numeric
    assert np.array(2.0) == destruct(2), "Integer destruct"
    assert np.array(-5.0) == destruct(-5.0), "Float destruct"
Example #5
0
    def __init__(self,
                 filter_shape,
                 coupling_history,
                 ncells,
                 lr=1e-4,
                 l2=0.0,
                 dt=1e-2):
        """GLM model class

        Parameters
        ----------
        filter_shape : tuple
            The dimensions of the stimulus filter, e.g. (nt, nx, ny)

        coupling_history : int
            How many timesteps to include in the coupling filter

        lr : float, optional
            Learning rate for RMSprop (Default: 1e-4)

        l2 : float, optional
            l2 regularization penalty on the weights (Default: 0.0)
        """
        self.dt = dt

        # initialize parameters
        self.theta_init = {
            'filter': np.random.randn(*(filter_shape + (ncells, ))) * 1e-6,
            'bias': np.zeros(ncells),
            'history': np.random.randn(coupling_history, ncells, ncells) * 1e-6
        }

        # initialize optimizer
        self.opt = rmsprop(destruct(self.theta_init).copy(), lr=lr)

        # add regularization
        if type(l2) is float:

            # same value for all keys
            self.l2 = {key: l2 for key in self.theta_init.keys()}

        elif type(l2) is dict:

            # default value is zero for every parameter
            self.l2 = {key: 0.0 for key in self.theta_init.keys()}

            # update with the given values
            self.l2.update(l2)

        else:
            raise ValueError(
                "l2 keyword argument must be a float or a dictionary")
Example #6
0
    def train_on_batch(self, X, y):
        """Updates the parameters on the given batch

        (with the corresponding regularization penalties)
        """
        # compute the objective and gradient
        objective, gradient = self.loss(X, y)

        # update objective and gradient with the l2 penalty
        for key in gradient.keys():
            objective += 0.5 * self.l2[key] * np.linalg.norm(
                self.theta[key].ravel(), 2)**2
            gradient[key] += self.l2[key] * self.theta[key]

        # pass the gradient to the optimizer
        self.opt(destruct(gradient))

        return objective, gradient
Example #7
0
def test_destruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(aref.ravel(), destruct(aref)), "Numpy ndarray destruct"

    # Numeric
    assert np.array(2.0) == destruct(2), "Integer destruct"
    assert np.array(-5.0) == destruct(-5.0), "Float destruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'c': np.linspace(0, 1, 6).reshape(3, 2),
        'b': np.ones(2)
    }
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    assert np.allclose(darray, destruct(dref)), "Dictionary destruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    assert np.allclose(larray, destruct(lref)), "List destruct"

    # Tuple
    assert np.allclose(larray, destruct(tuple(lref))), "Tuple destruct"

    # Composed / mixed types
    lref = [np.eye(2), {'a': np.array([-1, 1]), 'b': np.arange(3)}, 7]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(larray, destruct(lref)), "List of mixed types"

    dref = {'a': [3.5, 1.2, -33], 'b': np.arange(6).reshape(2, 3), 'c': 7}
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(darray, destruct(dref)), "Dictionary of mixed types"
Example #8
0
 def set_theta(self, theta):
     """Manually sets the values for the parameters"""
     self.opt.xk = destruct(theta).copy()