Ejemplo n.º 1
0
    def __call__(self, shape, dtype=None, partition_info=None):
        if dtype is None:
            dtype = self.dtype
        # Check the shape
        if len(shape) < 2:
            raise ValueError("The tensor to initialize must be "
                             "at least two-dimensional")
        # Flatten the input shape with the last dimension remaining
        # its original shape so it works for conv2d
        num_rows = 1
        for dim in shape[:-1]:
            num_rows *= dim
        num_cols = shape[-1]
        flat_shape = (num_cols,
                      num_rows) if num_rows < num_cols else (num_rows,
                                                             num_cols)

        # Generate a random matrix
        a = random_ops.random_normal(flat_shape, dtype=dtype, seed=self.seed)
        # Compute the qr factorization
        if context.executing_eagerly():
            with ops.device("cpu:0"):  # TODO(b/73102536)
                q, r = gen_linalg_ops.qr(a, full_matrices=False)
        else:
            q, r = gen_linalg_ops.qr(a, full_matrices=False)
        # Make Q uniform
        d = array_ops.diag_part(r)
        q *= math_ops.sign(d)
        if num_rows < num_cols:
            q = array_ops.matrix_transpose(q)
        return self.gain * array_ops.reshape(q, shape)
Ejemplo n.º 2
0
    def __call__(self, shape, dtype=None, partition_info=None):
        if dtype is None:
            dtype = self.dtype
        # Check the shape
        if len(shape) < 3 or len(shape) > 5:
            raise ValueError("The tensor to initialize must be at least "
                             "three-dimensional and at most five-dimensional")

        if shape[-2] > shape[-1]:
            raise ValueError("In_filters cannot be greater than out_filters.")

        # Generate a random matrix
        a = random_ops.random_normal([shape[-1], shape[-1]],
                                     dtype=dtype,
                                     seed=self.seed)
        # Compute the qr factorization
        q, r = gen_linalg_ops.qr(a, full_matrices=False)
        # Make Q uniform
        d = array_ops.diag_part(r)
        q *= math_ops.sign(d)
        q = q[:shape[-2], :]
        q *= math_ops.sqrt(math_ops.cast(self.gain, dtype=dtype))
        if len(shape) == 3:
            weight = array_ops.scatter_nd([[(shape[0] - 1) // 2]],
                                          array_ops.expand_dims(q, 0), shape)
        elif len(shape) == 4:
            weight = array_ops.scatter_nd([[(shape[0] - 1) // 2,
                                            (shape[1] - 1) // 2]],
                                          array_ops.expand_dims(q, 0), shape)
        else:
            weight = array_ops.scatter_nd([[(shape[0] - 1) // 2,
                                            (shape[1] - 1) // 2,
                                            (shape[2] - 1) // 2]],
                                          array_ops.expand_dims(q, 0), shape)
        return weight
Ejemplo n.º 3
0
    def __call__(self, shape, dtype=dtypes.float32):
        """Returns a tensor object initialized as specified by the initializer.
    Args:
      shape: Shape of the tensor.
      dtype: Optional dtype of the tensor. Only floating point types are
        supported.
    Raises:
      ValueError: If the dtype is not floating point or the input shape is not
       valid.
    """
        dtype = _assert_float_dtype(dtype)
        # Check the shape
        if len(shape) < 2:
            raise ValueError("The tensor to initialize must be "
                             "at least two-dimensional")
        # Flatten the input shape with the last dimension remaining
        # its original shape so it works for conv2d
        num_rows = 1
        for dim in shape[:-1]:
            num_rows *= dim
        num_cols = shape[-1]
        flat_shape = (max(num_cols, num_rows), min(num_cols, num_rows))

        # Generate a random matrix
        a = self._random_generator.random_normal(flat_shape, dtype=dtype)
        # Compute the qr factorization
        q, r = gen_linalg_ops.qr(a, full_matrices=False)
        # Make Q uniform
        d = array_ops.diag_part(r)
        q *= math_ops.sign(d)
        if tf.linalg.det(q) < 0:
            q[:, 0] *= -1
        if num_rows < num_cols:
            q = array_ops.matrix_transpose(q)
        return self.gain * array_ops.reshape(q, shape)
Ejemplo n.º 4
0
  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    # Check the shape
    if len(shape) < 3 or len(shape) > 5:
      raise ValueError("The tensor to initialize must be at least "
                       "three-dimensional and at most five-dimensional")

    if shape[-2] > shape[-1]:
      raise ValueError("In_filters cannot be greater than out_filters.")

    # Generate a random matrix
    a = random_ops.random_normal([shape[-1], shape[-1]],
                                 dtype=dtype, seed=self.seed)
    # Compute the qr factorization
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    # Make Q uniform
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    q = q[:shape[-2], :]
    q *= math_ops.sqrt(math_ops.cast(self.gain, dtype=dtype))
    if len(shape) == 3:
      weight = array_ops.scatter_nd([[(shape[0]-1)//2]],
                                    array_ops.expand_dims(q, 0), shape)
    elif len(shape) == 4:
      weight = array_ops.scatter_nd([[(shape[0]-1)//2, (shape[1]-1)//2]],
                                    array_ops.expand_dims(q, 0), shape)
    else:
      weight = array_ops.scatter_nd([[(shape[0]-1)//2, (shape[1]-1)//2,
                                      (shape[2]-1)//2]],
                                    array_ops.expand_dims(q, 0), shape)
    return weight
Ejemplo n.º 5
0
  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    # Check the shape
    if len(shape) < 2:
      raise ValueError("The tensor to initialize must be "
                       "at least two-dimensional")
    # Flatten the input shape with the last dimension remaining
    # its original shape so it works for conv2d
    num_rows = 1
    for dim in shape[:-1]:
      num_rows *= dim
    num_cols = shape[-1]
    flat_shape = (num_cols, num_rows) if num_rows < num_cols else (num_rows,
                                                                   num_cols)

    # Generate a random matrix
    a = random_ops.random_normal(flat_shape, dtype=dtype, seed=self.seed)
    # Compute the qr factorization
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    # Make Q uniform
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    if num_rows < num_cols:
      q = array_ops.matrix_transpose(q)
    return self.gain * array_ops.reshape(q, shape)
Ejemplo n.º 6
0
  def __call__(self, shape, dtype=dtypes.float32):
    """Returns a tensor object initialized as specified by the initializer.

    Args:
      shape: Shape of the tensor.
      dtype: Optional dtype of the tensor. Only floating point types are
       supported.

    Raises:
      ValueError: If the dtype is not floating point or the input shape is not
       valid.
    """
    dtype = _assert_float_dtype(dtype)
    # Check the shape
    if len(shape) < 2:
      raise ValueError("The tensor to initialize must be "
                       "at least two-dimensional")
    # Flatten the input shape with the last dimension remaining
    # its original shape so it works for conv2d
    num_rows = 1
    for dim in shape[:-1]:
      num_rows *= dim
    num_cols = shape[-1]
    flat_shape = (max(num_cols, num_rows), min(num_cols, num_rows))

    # Generate a random matrix
    a = random_ops.random_normal(flat_shape, dtype=dtype, seed=self.seed)
    # Compute the qr factorization
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    # Make Q uniform
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    if num_rows < num_cols:
      q = array_ops.matrix_transpose(q)
    return self.gain * array_ops.reshape(q, shape)
Ejemplo n.º 7
0
    def orth(self, dim=None):
        '''
        Orthogonalize the tensor.

        Parameters
        ----------
        dim : int, optional
            The dimension of the orthogonal dim-matricization of self. The
            default is None, returning a copy of the original tensor.

        Returns
        -------
        tensor : FullTensor
            A tensor whose dim-matricization is an orthogonal matrix
            corresponding to the Q factor of a QR factorization of the
            dim-matricization of self.
        r_matrix : numpy.ndarray
            The R factor.

        '''
        tensor = FullTensor(self)  # Copy the tensor

        if dim is None:
            return tensor, np.array([])

        if dim == -1:
            dim = tensor.order - 1

        dims = np.concatenate(
            (np.arange(dim), np.arange(dim + 1, tensor.order), [dim]))
        tensor = tensor.transpose(dims)

        shape0 = np.array(tensor.shape)
        tensor = tensor.reshape([np.prod(shape0[:-1]), shape0[-1]])

        try:
            from tensorflow.python.ops.gen_linalg_ops import qr
            q_tf, r_tf = qr(tensor.data, full_matrices=False)
            tensor.data, r_matrix = q_tf.numpy(), r_tf.numpy()
        except ImportError:
            tensor.data, r_matrix = np.linalg.qr(tensor.data)

        shape0[-1] = r_matrix.shape[0]
        tensor = tensor.reshape(shape0)
        tensor = tensor.itranspose(dims)
        tensor.is_orth = True
        tensor.orth_dim = dim
        return tensor, r_matrix
Ejemplo n.º 8
0
    def _orthogonal_matrix(self, n):
        """Construct an n x n orthogonal matrix.

    Args:
      n: Dimension.
    Returns:
      A n x n orthogonal matrix.
    """
        a = random_ops.random_normal([n, n], dtype=self.dtype, seed=self.seed)
        if self.seed:
            self.seed += 1
        q, r = gen_linalg_ops.qr(a)
        d = array_ops.diag_part(r)
        # make q uniform
        q *= math_ops.sign(d)
        return q
Ejemplo n.º 9
0
  def _orthogonal_matrix(self, n):
    """Construct an n x n orthogonal matrix.

    Args:
      n: Dimension.
    Returns:
      A n x n orthogonal matrix.
    """
    a = random_ops.random_normal([n, n], dtype=self.dtype, seed=self.seed)
    if self.seed:
      self.seed += 1
    q, r = gen_linalg_ops.qr(a)
    d = array_ops.diag_part(r)
    # make q uniform
    q *= math_ops.sign(d)
    return q
Ejemplo n.º 10
0
    def __call__(self, shape, dtype=None, **kwargs):
        """Returns a tensor object initialized to an orthogonal matrix.

    Args:
      shape: Shape of the tensor.
      dtype: Optional dtype of the tensor. Only floating point types are
        supported. If not specified, `tf.keras.backend.floatx()` is used,
       which default to `float32` unless you configured it otherwise
       (via `tf.keras.backend.set_floatx(float_dtype)`)
      **kwargs: Additional keyword arguments.
    """
        _validate_kwargs(self.__class__.__name__,
                         kwargs,
                         support_partition=False)
        dtype = _assert_float_dtype(_get_dtype(dtype))
        # Check the shape
        if len(shape) < 2:
            raise ValueError('The tensor to initialize must be '
                             'at least two-dimensional')
        # Flatten the input shape with the last dimension remaining
        # its original shape so it works for conv2d
        num_rows = 1
        for dim in shape[:-1]:
            num_rows *= dim
        num_cols = shape[-1]
        flat_shape = (max(num_cols, num_rows), min(num_cols, num_rows))

        # Generate a random matrix
        a = self._random_generator.random_normal(flat_shape, dtype=dtype)
        # Compute the qr factorization
        q, r = gen_linalg_ops.qr(a, full_matrices=False)
        # Make Q uniform
        d = array_ops.tensor_diag_part(r)
        q *= math_ops.sign(d)
        if num_rows < num_cols:
            q = array_ops.matrix_transpose(q)
        return self.gain * array_ops.reshape(q, shape)