def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = build_info.shape # For this test class, we are creating Hermitian spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # pre_spectrum is bounded away from zero. pre_spectrum = linear_operator_test_util.random_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) pre_spectrum_c = _to_complex(pre_spectrum) # Real{IFFT[pre_spectrum]} # = IFFT[EvenPartOf[pre_spectrum]] # is the IFFT of something that is also bounded away from zero. # Therefore, FFT[pre_h] would be a well-conditioned spectrum. pre_h = math_ops.ifft2d(pre_spectrum_c) # A spectrum is Hermitian iff it is the DFT of a real convolution kernel. # So we will make spectrum = FFT[h], for real valued h. h = math_ops.real(pre_h) h_c = _to_complex(h) spectrum = math_ops.fft2d(h_c) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D( lin_op_spectrum, input_output_dtype=dtype) mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = build_info.shape # For this test class, we are creating Hermitian spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # pre_spectrum is bounded away from zero. pre_spectrum = linear_operator_test_util.random_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) pre_spectrum_c = _to_complex(pre_spectrum) # Real{IFFT[pre_spectrum]} # = IFFT[EvenPartOf[pre_spectrum]] # is the IFFT of something that is also bounded away from zero. # Therefore, FFT[pre_h] would be a well-conditioned spectrum. pre_h = math_ops.ifft2d(pre_spectrum_c) # A spectrum is Hermitian iff it is the DFT of a real convolution kernel. # So we will make spectrum = FFT[h], for real valued h. h = math_ops.real(pre_h) h_c = _to_complex(h) spectrum = math_ops.fft2d(h_c) if use_placeholder: spectrum_ph = array_ops.placeholder(dtypes.complex64) # Evaluate here because (i) you cannot feed a tensor, and (ii) # it is random and we want the same value used for both mat and feed_dict. spectrum = spectrum.eval() operator = linalg.LinearOperatorCirculant2D( spectrum_ph, input_output_dtype=dtype) feed_dict = {spectrum_ph: spectrum} else: operator = linalg.LinearOperatorCirculant2D( spectrum, input_output_dtype=dtype) feed_dict = None mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat, feed_dict
def _spectrum_to_circulant_2d(self, spectrum, shape, dtype): """Creates a block circulant matrix from a spectrum. Intentionally done in an explicit yet inefficient way. This provides a cross check to the main code that uses fancy reshapes. Args: spectrum: Float or complex `Tensor`. shape: Python list. Desired shape of returned matrix. dtype: Type to cast the returned matrix to. Returns: Block circulant (batch) matrix of desired `dtype`. """ spectrum = _to_complex(spectrum) spectrum_shape = self._shape_to_spectrum_shape(shape) domain_dimension = spectrum_shape[-1] if not domain_dimension: return array_ops.zeros(shape, dtype) block_shape = spectrum_shape[-2:] # Explicitly compute the action of spectrum on basis vectors. matrix_rows = [] for n0 in range(block_shape[0]): for n1 in range(block_shape[1]): x = np.zeros(block_shape) # x is a basis vector. x[n0, n1] = 1.0 fft_x = math_ops.fft2d(x.astype(np.complex64)) h_convolve_x = math_ops.ifft2d(spectrum * fft_x) # We want the flat version of the action of the operator on a basis # vector, not the block version. h_convolve_x = array_ops.reshape(h_convolve_x, shape[:-1]) matrix_rows.append(h_convolve_x) matrix = array_ops.stack(matrix_rows, axis=-1) return math_ops.cast(matrix, dtype)
def _IFFT2DGrad(_, grad): rsize = 1. / math_ops.cast(_FFTSizeForGrad(grad, 2), dtypes.float32) return math_ops.fft2d(grad) * math_ops.complex(rsize, 0.)
def _IFFT2DGrad(_, grad): rsize = 1. / math_ops.cast(array_ops.size(grad), dtypes.float32) return math_ops.fft2d(grad) * math_ops.complex(rsize, 0.)