Ejemplo n.º 1
0
 def __call__(self, shape):
     if len(shape) != 2:
         raise InitializationError("Works only with 2D matrices but shape "
                                   "was: {}".format(shape))
     if self.enforce_square and shape[0] != shape[1]:
         raise InitializationError("Matrix needs to be square, but was {}"
                                   "".format(shape))
     weights = np.eye(shape[0], shape[1], dtype=np.float) * self.scale
     weights += self.rnd.randn(*shape) * self.std
     return weights
Ejemplo n.º 2
0
 def __call__(self, shape):
     if len(shape) != 2:
         raise InitializationError("Works only with 2D matrices but shape "
                                   "was: {}".format(shape))
     a = self.rnd.randn(*shape)
     u, _, v = np.linalg.svd(a, full_matrices=False)
     q = u if u.shape == shape else v
     return (self.scale * q).reshape(shape)
Ejemplo n.º 3
0
    def __call__(self, shape):
        if len(shape) != 2:
            raise InitializationError("Works only with 2D matrices but shape "
                                      "was: {}".format(shape))
        if shape[0] != shape[1]:
            raise InitializationError("Matrix needs to be square, but was {}"
                                      "".format(shape))

        N = shape[1]
        if self.scale is None:
            scale = {
                'linear': np.exp(1 / (2 * N)),
                'rel': np.sqrt(2) * np.exp(1.2 / (max(N, 6) - 2.4))
            }[self.act_func]
        else:
            scale = self.scale

        return scale * self.rnd.randn(*shape) / N
Ejemplo n.º 4
0
    def __call__(self, shape):
        self._assert_atleast2d(shape)
        if shape[0] != shape[1]:
            raise InitializationError("Matrix should be square but was: {}"
                                      "".format(shape))

        parameters = self.rnd.uniform(-0.5, 0.5, size=shape)
        # normalizing and setting spectral radius (correct, slow):
        rho_parameters = max(abs(np.linalg.eig(parameters)[0]))
        return parameters * (self.spectral_radius / rho_parameters)
Ejemplo n.º 5
0
 def __call__(self, shape):
     self._assert_atleast2d(shape)
     if shape[1] < self.connections:
         raise InitializationError("Output dimension to small: {} < {}"
                                   "".format(shape[1], self.connections))
     sub_result = evaluate_initializer(self.sub_initializer, shape)
     connection_mask = np.zeros(shape)
     connection_mask[:, :self.connections] = 1.
     for i in range(shape[0]):
         self.rnd.shuffle(connection_mask[i, :])
     return sub_result * connection_mask
Ejemplo n.º 6
0
 def __call__(self, shape):
     if shape[0] % 4 != 0:
         raise InitializationError("First dim of LstmOpt shape needs to be "
                                   "divisible by 4. But shape was {}"
                                   .format(shape))
     weights = np.zeros(shape)
     n = shape[0] // 4
     sub_shape = (n,) + shape[1:]
     weights[:n] = evaluate_initializer(
         self.block_input, sub_shape, seed=self.rnd.generate_seed())
     weights[n:2 * n] = evaluate_initializer(
         self.input_gate, sub_shape, seed=self.rnd.generate_seed())
     weights[2 * n:3 * n] = evaluate_initializer(
         self.forget_gate, sub_shape, seed=self.rnd.generate_seed())
     weights[3 * n:] = evaluate_initializer(
         self.output_gate, sub_shape, seed=self.rnd.generate_seed())
     return weights
Ejemplo n.º 7
0
    def __call__(self, shape):
        if not self.array.shape == shape:
            raise InitializationError('Shape mismatch {} != {}'
                                      .format(self.array.shape, shape))

        return self.array
Ejemplo n.º 8
0
 def _assert_atleast2d(self, shape):
     if len(shape) < 2:
         raise InitializationError(
             "{} only works on >2D matrices, but shape was {}".format(
                 self.__class__.__name__, shape))