def __call__(self, shape, dtype, **kwargs): value = linalg_ops_impl.eye(*shape, dtype=dtype) if "partition_shape" in kwargs and "partition_offset" in kwargs: return array_ops.slice(value, kwargs["partition_offset"], kwargs["partition_shape"]) raise AssertionError("PartitionAwareIdentity do not support " "non-partitioned initialization")
def _block_orth(self, p1, p2, p3): """Construct a 3 x 3 kernel. Used to construct orthgonal kernel. Args: p1: A symmetric projection matrix. p2: A symmetric projection matrix. p3: A symmetric projection matrix. Returns: A 2 x 2 x 2 kernel. Raises: ValueError: If the dimensions of p1, p2 and p3 are different. """ p1_shape = p1.shape.as_list() if p1_shape != p2.shape.as_list() or p1_shape != p3.shape.as_list(): raise ValueError("The dimension of the matrices must be the same.") n = p1_shape[0] eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel2x2x2 = {} def matmul(p1, p2, p3): return math_ops.matmul(math_ops.matmul(p1, p2), p3) def cast(i, p): """Return p or (1-p).""" return i * p + (1-i) * (eye - p) for i in [0, 1]: for j in [0, 1]: for k in [0, 1]: kernel2x2x2[i, j, k] = matmul(cast(i, p1), cast(j, p2), cast(k, p3)) return kernel2x2x2
def _block_orth(self, p1, p2, p3): """Construct a 3 x 3 kernel. Used to construct orthgonal kernel. Args: p1: A symmetric projection matrix. p2: A symmetric projection matrix. p3: A symmetric projection matrix. Returns: A 2 x 2 x 2 kernel. Raises: ValueError: If the dimensions of p1, p2 and p3 are different. """ p1_shape = p1.shape.as_list() if p1_shape != p2.shape.as_list() or p1_shape != p3.shape.as_list(): raise ValueError("The dimension of the matrices must be the same.") n = p1_shape[0] eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel2x2x2 = {} def matmul(p1, p2, p3): return math_ops.matmul(math_ops.matmul(p1, p2), p3) def cast(i, p): """Return p or (1-p).""" return i * p + (1 - i) * (eye - p) for i in [0, 1]: for j in [0, 1]: for k in [0, 1]: kernel2x2x2[i, j, k] = matmul(cast(i, p1), cast(j, p2), cast(k, p3)) return kernel2x2x2
def __call__(self, shape, dtype=None, partition_info=None): full_shape = shape if partition_info is None else partition_info.full_shape if len(full_shape) != 2: raise ValueError( "Identity matrix initializer can only be used for 2D matrices.") if dtype is None: dtype = self.dtype initializer = linalg_ops_impl.eye(*full_shape, dtype=dtype) if partition_info is not None: initializer = array_ops.slice(initializer, partition_info.var_offset, shape) return self.gain * initializer
def _block_orth(self, projection_matrix): """Construct a kernel. Used to construct orthgonal kernel. Args: projection_matrix: A symmetric projection matrix of size n x n. Returns: [projection_matrix, (1 - projection_matrix)]. """ n = projection_matrix.shape.as_list()[0] kernel = {} eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel[0] = projection_matrix kernel[1] = eye - projection_matrix return kernel
def __call__(self, shape, dtype=None, partition_info=None): full_shape = shape if partition_info is None else partition_info.full_shape if len(full_shape) != 2: raise ValueError( "Identity matrix initializer can only be used for 2D matrices." ) if dtype is None: dtype = self.dtype gain_identity = self.diagonal_gain * linalg_ops_impl.eye(*full_shape, dtype=dtype) gain_ones = self.off_diagonal_gain * array_ops.ones(full_shape, dtype=dtype) gdn_init = math_ops.sqrt(gain_identity + math_ops.square(gain_ones)) return gdn_init
def _block_orth(self, projection_matrix): """Construct a kernel. Used to construct orthgonal kernel. Args: projection_matrix: A symmetric projection matrix of size n x n. Returns: [projection_matrix, (1 - projection_matrix)]. """ n = projection_matrix.shape.as_list()[0] kernel = {} eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel[0] = projection_matrix kernel[1] = eye - projection_matrix return kernel
def eye(num_rows, num_columns=None, batch_shape=None, dtype=dtypes.float32, name=None): """Construct an identity matrix, or a batch of matrices. See also `tf.ones`, `tf.zeros`, `tf.fill`, `tf.one_hot`. ```python # Construct one identity matrix. tf.eye(2) ==> [[1., 0.], [0., 1.]] # Construct a batch of 3 identity matrices, each 2 x 2. # batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2. batch_identity = tf.eye(2, batch_shape=[3]) # Construct one 2 x 3 "identity" matrix tf.eye(2, num_columns=3) ==> [[ 1., 0., 0.], [ 0., 1., 0.]] ``` Args: num_rows: Non-negative `int32` scalar `Tensor` giving the number of rows in each batch matrix. num_columns: Optional non-negative `int32` scalar `Tensor` giving the number of columns in each batch matrix. Defaults to `num_rows`. batch_shape: A list or tuple of Python integers or a 1-D `int32` `Tensor`. If provided, the returned `Tensor` will have leading batch dimensions of this shape. dtype: The type of an element in the resulting `Tensor` name: A name for this `Op`. Defaults to "eye". Returns: A `Tensor` of shape `batch_shape + [num_rows, num_columns]` """ return linalg_ops_impl.eye(num_rows, num_columns=num_columns, batch_shape=batch_shape, dtype=dtype, name=name)
def eye(num_rows, num_columns=None, batch_shape=None, dtype=dtypes.float32, name=None): """Construct an identity matrix, or a batch of matrices. ```python # Construct one identity matrix. tf.eye(2) ==> [[1., 0.], [0., 1.]] # Construct a batch of 3 identity matricies, each 2 x 2. # batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2. batch_identity = tf.eye(2, batch_shape=[3]) # Construct one 2 x 3 "identity" matrix tf.eye(2, num_columns=3) ==> [[ 1., 0., 0.], [ 0., 1., 0.]] ``` Args: num_rows: Non-negative `int32` scalar `Tensor` giving the number of rows in each batch matrix. num_columns: Optional non-negative `int32` scalar `Tensor` giving the number of columns in each batch matrix. Defaults to `num_rows`. batch_shape: A list or tuple of Python integers or a 1-D `int32` `Tensor`. If provided, the returned `Tensor` will have leading batch dimensions of this shape. dtype: The type of an element in the resulting `Tensor` name: A name for this `Op`. Defaults to "eye". Returns: A `Tensor` of shape `batch_shape + [num_rows, num_columns]` """ return linalg_ops_impl.eye(num_rows, num_columns=num_columns, batch_shape=batch_shape, dtype=dtype, name=name)
def __call__(self, shape, dtype=dtypes.float32, **kwargs): """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. **kwargs: Additional keyword arguments. Raises: ValueError: If the dtype is not floating point ValueError: If the requested shape does not have exactly two axes. """ self._validate_kwargs(kwargs, support_partition=False) dtype = _assert_float_dtype(dtype) if len(shape) != 2: raise ValueError( "Identity matrix initializer can only be used for 2D matrices.") initializer = linalg_ops_impl.eye(*shape, dtype=dtype) return self.gain * initializer
def __call__(self, shape, dtype=dtypes.float32, **kwargs): """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. **kwargs: Additional keyword arguments. Raises: ValueError: If the dtype is not floating point ValueError: If the requested shape does not have exactly two axes. """ self._validate_kwargs(kwargs, support_partition=False) dtype = _assert_float_dtype(dtype) if len(shape) != 2: raise ValueError("The tensor to initialize, specified by argument `shape`" " must be at least two-dimensional. Received shape=" f"{shape}") initializer = linalg_ops_impl.eye(*shape, dtype=dtype) return self.gain * initializer
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 """ partition_info = None # Keeps logic so can be readded later if necessary dtype = _assert_float_dtype(dtype) full_shape = shape if partition_info is None else partition_info.full_shape if len(full_shape) != 2: raise ValueError( "Identity matrix initializer can only be used for 2D matrices.") initializer = linalg_ops_impl.eye(*full_shape, dtype=dtype) if partition_info is not None: initializer = array_ops.slice(initializer, partition_info.var_offset, shape) return self.gain * initializer
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 """ partition_info = None # Keeps logic so can be readded later if necessary dtype = _assert_float_dtype(dtype) full_shape = shape if partition_info is None else partition_info.full_shape if len(full_shape) != 2: raise ValueError( "Identity matrix initializer can only be used for 2D matrices.") initializer = linalg_ops_impl.eye(*full_shape, dtype=dtype) if partition_info is not None: initializer = array_ops.slice(initializer, partition_info.var_offset, shape) return self.gain * initializer
def _block_orth(self, p1, p2): """Construct a 2 x 2 kernel. Used to construct orthgonal kernel. Args: p1: A symmetric projection matrix. p2: A symmetric projection matrix. Returns: A 2 x 2 kernel [[p1p2, p1(1-p2)], [(1-p1)p2, (1-p1)(1-p2)]]. Raises: ValueError: If the dimensions of p1 and p2 are different. """ if p1.shape.as_list() != p2.shape.as_list(): raise ValueError("The dimension of the matrices must be the same.") n = p1.shape.as_list()[0] kernel2x2 = {} eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel2x2[0, 0] = math_ops.matmul(p1, p2) kernel2x2[0, 1] = math_ops.matmul(p1, (eye - p2)) kernel2x2[1, 0] = math_ops.matmul((eye - p1), p2) kernel2x2[1, 1] = math_ops.matmul((eye - p1), (eye - p2)) return kernel2x2
def _block_orth(self, p1, p2): """Construct a 2 x 2 kernel. Used to construct orthgonal kernel. Args: p1: A symmetric projection matrix. p2: A symmetric projection matrix. Returns: A 2 x 2 kernel [[p1p2, p1(1-p2)], [(1-p1)p2, (1-p1)(1-p2)]]. Raises: ValueError: If the dimensions of p1 and p2 are different. """ if p1.shape.as_list() != p2.shape.as_list(): raise ValueError("The dimension of the matrices must be the same.") n = p1.shape.as_list()[0] kernel2x2 = {} eye = linalg_ops_impl.eye(n, dtype=self.dtype) kernel2x2[0, 0] = math_ops.matmul(p1, p2) kernel2x2[0, 1] = math_ops.matmul(p1, (eye - p2)) kernel2x2[1, 0] = math_ops.matmul((eye - p1), p2) kernel2x2[1, 1] = math_ops.matmul((eye - p1), (eye - p2)) return kernel2x2
def __call__(self, shape, dtype, partition): value = linalg_ops_impl.eye(*shape, dtype=dtype) if partition is not None: value = array_ops.slice(value, partition.offsets, partition.shape) return value
def __call__(self, shape, dtype, shard_info): value = linalg_ops_impl.eye(*shape, dtype=dtype) if shard_info is not None: value = array_ops.slice(value, shard_info.offset, shard_info.shape) return value