def test_scalar_row_col_raises(self): with self.assertRaisesRegex(ValueError, "must have at least 1 dimension"): linear_operator_toeplitz.LinearOperatorToeplitz(1., 1.) with self.assertRaisesRegex(ValueError, "must have at least 1 dimension"): linear_operator_toeplitz.LinearOperatorToeplitz([1.], 1.) with self.assertRaisesRegex(ValueError, "must have at least 1 dimension"): linear_operator_toeplitz.LinearOperatorToeplitz(1., [1.])
def test_convert_variables_to_tensors(self): col = variables_module.Variable([1.]) row = variables_module.Variable([1.]) operator = linear_operator_toeplitz.LinearOperatorToeplitz( col, row, is_self_adjoint=True, is_positive_definite=True) with self.cached_session() as sess: sess.run([x.initializer for x in operator.variables]) self.check_convert_variables_to_tensors(operator)
def operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) row = np.random.uniform(low=1., high=5., size=shape[:-1]) col = np.random.uniform(low=1., high=5., size=shape[:-1]) # Make sure first entry is the same row[..., 0] = col[..., 0] if ensure_self_adjoint_and_pd: # Note that a Toeplitz matrix generated from a linearly decreasing # non-negative sequence is positive definite. See # https://www.math.cinvestav.mx/~grudsky/Papers/118_29062012_Albrecht.pdf # for details. row = np.linspace(start=10., stop=1., num=shape[-1]) # The entries for the first row and column should be the same to guarantee # symmetric. row = col lin_op_row = math_ops.cast(row, dtype=dtype) lin_op_col = math_ops.cast(col, dtype=dtype) if use_placeholder: lin_op_row = array_ops.placeholder_with_default(lin_op_row, shape=None) lin_op_col = array_ops.placeholder_with_default(lin_op_col, shape=None) operator = linear_operator_toeplitz.LinearOperatorToeplitz( row=lin_op_row, col=lin_op_col, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) flattened_row = np.reshape(row, (-1, shape[-1])) flattened_col = np.reshape(col, (-1, shape[-1])) flattened_toeplitz = np.zeros( [flattened_row.shape[0], shape[-1], shape[-1]]) for i in range(flattened_row.shape[0]): flattened_toeplitz[i] = scipy.linalg.toeplitz( flattened_col[i], flattened_row[i]) matrix = np.reshape(flattened_toeplitz, shape) matrix = math_ops.cast(matrix, dtype=dtype) return operator, matrix
def test_tape_safe(self): col = variables_module.Variable([1.]) row = variables_module.Variable([1.]) operator = linear_operator_toeplitz.LinearOperatorToeplitz( col, row, is_self_adjoint=True, is_positive_definite=True) self.check_tape_safe( operator, skip_options=[ # .diag_part, .trace depend only on `col`, so test explicitly below. linear_operator_test_util.CheckTapeSafeSkipOptions.DIAG_PART, linear_operator_test_util.CheckTapeSafeSkipOptions.TRACE, ]) with backprop.GradientTape() as tape: self.assertIsNotNone(tape.gradient(operator.diag_part(), col)) with backprop.GradientTape() as tape: self.assertIsNotNone(tape.gradient(operator.trace(), col))