本文整理汇总了Python中tensorflow.complex方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.complex方法的具体用法?Python tensorflow.complex怎么用?Python tensorflow.complex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.complex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def call(self, inputx):
if not inputx.dtype in [tf.complex64, tf.complex128]:
print('Warning: inputx is not complex. Converting.', file=sys.stderr)
# if inputx is float, this will assume 0 imag channel
inputx = tf.cast(inputx, tf.complex64)
# get the right fft
if self.ndims == 1:
fft = tf.fft
elif self.ndims == 2:
fft = tf.fft2d
else:
fft = tf.fft3d
perm_dims = [0, self.ndims + 1] + list(range(1, self.ndims + 1))
invert_perm_ndims = [0] + list(range(2, self.ndims + 2)) + [1]
perm_inputx = K.permute_dimensions(inputx, perm_dims) # [batch_size, nb_features, *vol_size]
fft_inputx = fft(perm_inputx)
return K.permute_dimensions(fft_inputx, invert_perm_ndims)
示例2: invert_spectra_griffin_lim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def invert_spectra_griffin_lim(X_mag, nfft, nhop, ngl):
X = tf.complex(X_mag, tf.zeros_like(X_mag))
def b(i, X_best):
x = tf.contrib.signal.inverse_stft(X_best, nfft, nhop)
X_est = tf.contrib.signal.stft(x, nfft, nhop)
phase = X_est / tf.cast(tf.maximum(1e-8, tf.abs(X_est)), tf.complex64)
X_best = X * phase
return i + 1, X_best
i = tf.constant(0)
c = lambda i, _: tf.less(i, ngl)
_, X = tf.while_loop(c, b, [i, X], back_prop=False)
x = tf.contrib.signal.inverse_stft(X, nfft, nhop)
x = x[:, :_SLICE_LEN]
return x
示例3: _testBCastByFunc
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def _testBCastByFunc(self, funcs, xs, ys):
dtypes = [
np.float16,
np.float32,
np.float64,
np.int32,
np.int64,
np.complex64,
np.complex128,
]
for dtype in dtypes:
for (np_func, tf_func) in funcs:
if (dtype in (np.complex64, np.complex128) and
tf_func in (_FLOORDIV, tf.floordiv)):
continue # floordiv makes no sense for complex numbers
self._compareBCast(xs, ys, dtype, np_func, tf_func)
self._compareBCast(ys, xs, dtype, np_func, tf_func)
示例4: _compareGradient
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def _compareGradient(self, x):
# x[:, 0] is real, x[:, 1] is imag. We combine real and imag into
# complex numbers. Then, we extract real and imag parts and
# computes the squared sum. This is obviously the same as sum(real
# * real) + sum(imag * imag). We just want to make sure the
# gradient function is checked.
with self.test_session():
inx = tf.convert_to_tensor(x)
real, imag = tf.split(1, 2, inx)
real, imag = tf.reshape(real, [-1]), tf.reshape(imag, [-1])
cplx = tf.complex(real, imag)
cplx = tf.conj(cplx)
loss = tf.reduce_sum(
tf.square(tf.real(cplx))) + tf.reduce_sum(
tf.square(tf.imag(cplx)))
epsilon = 1e-3
jacob_t, jacob_n = tf.test.compute_gradient(inx,
list(x.shape),
loss,
[1],
x_init_value=x,
delta=epsilon)
self.assertAllClose(jacob_t, jacob_n, rtol=epsilon, atol=epsilon)
示例5: _checkGrad
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def _checkGrad(self, func, x, y, use_gpu=False):
with self.test_session(use_gpu=use_gpu):
inx = tf.convert_to_tensor(x)
iny = tf.convert_to_tensor(y)
# func is a forward or inverse FFT function (batched or unbatched)
z = func(tf.complex(inx, iny))
# loss = sum(|z|^2)
loss = tf.reduce_sum(tf.real(z * tf.conj(z)))
((x_jacob_t, x_jacob_n),
(y_jacob_t, y_jacob_n)) = tf.test.compute_gradient(
[inx, iny],
[list(x.shape), list(y.shape)],
loss,
[1],
x_init_value=[x, y],
delta=1e-2)
self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=1e-2)
self.assertAllClose(y_jacob_t, y_jacob_n, rtol=1e-2, atol=1e-2)
示例6: test_fftc
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def test_fftc(self):
shape = [10, 10, 2]
data = tf.complex(
tf.random_uniform(shape), tf.random_uniform(shape))
fdata = tfmri.fftc(data)
fdata_np = self._fftnc(data, axes=(-2,))
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fftc(data, data_format='channels_first')
fdata_np = self._fftnc(data, axes=(-1,))
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fftc(data, orthonorm=False)
fdata_np = self._fftnc(data, axes=(-2,), norm=None)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
示例7: test_ifftc
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def test_ifftc(self):
shape = [10, 10, 2]
data = tf.complex(
tf.random_uniform(shape), tf.random_uniform(shape))
fdata_np = self._fftnc(data, axes=(-2,), transpose=True)
fdata = tfmri.ifftc(data)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fftc(data, transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.ifftc(data, data_format='channels_first')
fdata_np = self._fftnc(data, axes=(-1,), transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.ifftc(data, orthonorm=False)
fdata_np = self._fftnc(data, axes=(-2,), norm=None, transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
示例8: test_fft2c
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def test_fft2c(self):
shape = [10, 10, 2]
data = tf.complex(
tf.random_uniform(shape), tf.random_uniform(shape))
fdata = tfmri.fft2c(data)
fdata_np = self._fftnc(data, axes=(-3, -2))
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fft2c(data, data_format='channels_first')
fdata_np = self._fftnc(data, axes=(-2, -1))
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fft2c(data, orthonorm=False)
fdata_np = self._fftnc(data, axes=(-3, -2), norm=None)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
示例9: test_ifft2c
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def test_ifft2c(self):
shape = [10, 10, 2]
data = tf.complex(
tf.random_uniform(shape), tf.random_uniform(shape))
fdata_np = self._fftnc(data, axes=(-3, -2), transpose=True)
fdata = tfmri.ifft2c(data)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.fft2c(data, transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.ifft2c(data, data_format='channels_first')
fdata_np = self._fftnc(data, axes=(-2, -1), transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
fdata = tfmri.ifft2c(data, orthonorm=False)
fdata_np = self._fftnc(data, axes=(-3, -2), norm=None, transpose=True)
diff = np.mean(np.abs(fdata_np - fdata) ** 2)
self.assertTrue(diff < eps)
示例10: channels_to_complex
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def channels_to_complex(image,
data_format='channels_last',
name='channels2complex'):
"""Convert data from channels to complex."""
if len(image.shape) != 3 and len(image.shape) != 4:
raise TypeError('Input data must be have 3 or 4 dimensions')
axis_c = -1 if data_format == 'channels_last' else -3
shape_c = image.shape[axis_c].value
if shape_c and (shape_c % 2 != 0):
raise TypeError(
'Number of channels (%d) must be divisible by 2' % shape_c)
if image.dtype is tf.complex64 or image.dtype is tf.complex128:
raise TypeError('Input data cannot be complex')
with tf.name_scope(name):
image_real, image_imag = tf.split(image, 2, axis=axis_c)
image_out = tf.complex(image_real, image_imag)
return image_out
示例11: hdrplus_merge
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def hdrplus_merge(imgs, N, c, sig):
ccast_tf = lambda x : tf.complex(x, tf.zeros_like(x))
# imgs is [batch, h, w, ch]
rcw = tf.expand_dims(rcwindow(N), axis=-1)
imgs = imgs * rcw
imgs = tf.transpose(imgs, [0, 3, 1, 2])
imgs_f = tf.fft2d(ccast_tf(imgs))
imgs_f = tf.transpose(imgs_f, [0, 2, 3, 1])
Dz2 = tf.square(tf.abs(imgs_f[...,0:1] - imgs_f))
Az = Dz2 / (Dz2 + c*sig**2)
filt0 = 1 + tf.expand_dims(tf.reduce_sum(Az[...,1:], axis=-1), axis=-1)
filts = tf.concat([filt0, 1 - Az[...,1:]], axis=-1)
output_f = tf.reduce_mean(imgs_f * ccast_tf(filts), axis=-1)
output_f = tf.real(tf.ifft2d(output_f))
return output_f
示例12: complex_modulus
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def complex_modulus(x):
"""Computes complex modulus.
Parameters
----------
x : tensor
Input tensor whose complex modulus is to be calculated.
Returns
-------
modulus : tensor
Tensor the same size as input_array. modulus holds the
result of the complex modulus.
"""
modulus = tf.abs(x)
return modulus
示例13: compute_fft
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def compute_fft(x, direction="C2C", inverse=False):
if direction == 'C2R':
inverse = True
x_shape = x.get_shape().as_list()
h, w = x_shape[-2], x_shape[-3]
x_complex = tf.complex(x[..., 0], x[..., 1])
if direction == 'C2R':
out = tf.real(tf.ifft2d(x_complex)) * h * w
return out
else:
if inverse:
out = stack_real_imag(tf.ifft2d(x_complex)) * h * w
else:
out = stack_real_imag(tf.fft2d(x_complex))
return out
示例14: reduce_mean_angle
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def reduce_mean_angle(weights, angles, use_complex=False, name=None):
""" Computes the weighted mean of angles. Accepts option to compute use complex exponentials or real numbers.
Complex number-based version is giving wrong gradients for some reason, but forward calculation is fine.
See https://en.wikipedia.org/wiki/Mean_of_circular_quantities
Args:
weights: [BATCH_SIZE, NUM_ANGLES]
angles: [NUM_ANGLES, NUM_DIHEDRALS]
Returns:
[BATCH_SIZE, NUM_DIHEDRALS]
"""
with tf.name_scope(name, 'reduce_mean_angle', [weights, angles]) as scope:
weights = tf.convert_to_tensor(weights, name='weights')
angles = tf.convert_to_tensor(angles, name='angles')
if use_complex:
# use complexed-valued exponentials for calculation
cwts = tf.complex(weights, 0.) # cast to complex numbers
exps = tf.exp(tf.complex(0., angles)) # convert to point on complex plane
unit_coords = tf.matmul(cwts, exps) # take the weighted mixture of the unit circle coordinates
return tf.angle(unit_coords, name=scope) # return angle of averaged coordinate
else:
# use real-numbered pairs of values
sins = tf.sin(angles)
coss = tf.cos(angles)
y_coords = tf.matmul(weights, sins)
x_coords = tf.matmul(weights, coss)
return tf.atan2(y_coords, x_coords, name=scope)
示例15: random
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import complex [as 别名]
def random(self, *shapes, **kwargs):
if all(isinstance(i, int) for i in shapes):
if kwargs.get("complex", False):
return (self.random(*shapes) + 1j * self.random(*shapes)).astype(np.complex64)
else:
return np.random.rand(*shapes)
else:
return tuple(self.random(*shape) for shape in shapes)