Python tensorflow.complex64() Examples
The following are 30
code examples of tensorflow.complex64().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: audio.py From vqvae-speech with MIT License | 7 votes |
def spectrogram(x, frame_length, nfft=1024): ''' Spectrogram of non-overlapping window ''' with tf.name_scope('Spectrogram'): shape = tf.shape(x) b = shape[0] D = frame_length t = shape[1] // D x = tf.reshape(x, [b, t, D]) window = tf.contrib.signal.hann_window(frame_length) window = tf.expand_dims(window, 0) window = tf.expand_dims(window, 0) # [1, 1, L] x = x * window pad = tf.zeros([b, t, nfft - D]) x = tf.concat([x, pad], -1) x = tf.cast(x, tf.complex64) X = tf.fft(x) # TF's API doesn't do padding automatically yet X = tf.log(tf.abs(X) + 1e-2) X = X[:, :, :nfft//2 + 1] X = tf.transpose(X, [0, 2, 1]) X = tf.reverse(X, [1]) X = tf.expand_dims(X, -1) X = (X - tf.reduce_min(X)) / (tf.reduce_max(X) - tf.reduce_min(X)) X = gray2jet(X) tf.summary.image('spectrogram', X) return X
Example #2
Source File: layers.py From neuron with GNU General Public License v3.0 | 6 votes |
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: ifft = tf.ifft elif self.ndims == 2: ifft = tf.ifft2d else: ifft = tf.ifft3d 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] ifft_inputx = ifft(perm_inputx) return K.permute_dimensions(ifft_inputx, invert_perm_ndims)
Example #3
Source File: train_specgan.py From wavegan with MIT License | 6 votes |
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
Example #4
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testOnesLike(self): for dtype in [tf.float32, tf.float64, tf.int32, tf.uint8, tf.int16, tf.int8, tf.complex64, tf.complex128, tf.int64]: numpy_dtype = dtype.as_numpy_dtype with self.test_session(): # Creates a tensor of non-zero values with shape 2 x 3. d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype) # Constructs a tensor of zeros of the same dimensions and type as "d". z_var = tf.ones_like(d) # Test that the type is correct self.assertEqual(z_var.dtype, dtype) z_value = z_var.eval() # Test that the value is correct self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2))) self.assertEqual([2, 3], z_var.get_shape())
Example #5
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def _compareBCast(self, xs, ys, dtype, np_func, tf_func): if dtype in (np.complex64, np.complex128): x = (1 + np.linspace(0, 2 + 3j, np.prod(xs))).astype(dtype).reshape(xs) y = (1 + np.linspace(0, 2 - 2j, np.prod(ys))).astype(dtype).reshape(ys) else: x = (1 + np.linspace(0, 5, np.prod(xs))).astype(dtype).reshape(xs) y = (1 + np.linspace(0, 5, np.prod(ys))).astype(dtype).reshape(ys) self._compareCpu(x, y, np_func, tf_func) if x.dtype in (np.float16, np.float32, np.float64, np.complex64, np.complex128): if tf_func not in (_FLOORDIV, tf.floordiv): if x.dtype == np.float16: # Compare fp16 theoretical gradients to fp32 numerical gradients, # since fp16 numerical gradients are too imprecise unless great # care is taken with choosing the inputs and the delta. This is # a weaker check (in particular, it does not test the op itself, # only its gradient), but it's much better than nothing. self._compareGradientX(x, y, np_func, tf_func, np.float) self._compareGradientY(x, y, np_func, tf_func, np.float) else: self._compareGradientX(x, y, np_func, tf_func) self._compareGradientY(x, y, np_func, tf_func) self._compareGpu(x, y, np_func, tf_func) # TODO(josh11b,vrv): Refactor this to use parameterized tests.
Example #6
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: math_grad_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def _testGrad(self, shape, dtype=None, max_error=None, bias=None, sigma=None): np.random.seed(7) if dtype in (tf.complex64, tf.complex128): value = tf.complex(self._biasedRandN(shape, bias=bias, sigma=sigma), self._biasedRandN(shape, bias=bias, sigma=sigma)) else: value = tf.convert_to_tensor(self._biasedRandN(shape, bias=bias), dtype=dtype) with self.test_session(use_gpu=True): if dtype in (tf.complex64, tf.complex128): output = tf.complex_abs(value) else: output = tf.abs(value) error = tf.test.compute_gradient_error( value, shape, output, output.get_shape().as_list()) self.assertLess(error, max_error)
Example #8
Source File: tensor_util_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testComplex64N(self): t = tensor_util.make_tensor_proto([(1+2j), (3+4j), (5+6j)], shape=[1, 3], dtype=tf.complex64) self.assertProtoEquals(""" dtype: DT_COMPLEX64 tensor_shape { dim { size: 1 } dim { size: 3 } } scomplex_val: 1 scomplex_val: 2 scomplex_val: 3 scomplex_val: 4 scomplex_val: 5 scomplex_val: 6 """, t) a = tensor_util.MakeNdarray(t) self.assertEquals(np.complex64, a.dtype) self.assertAllEqual(np.array([[(1+2j), (3+4j), (5+6j)]]), a)
Example #9
Source File: tensor_util_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testComplex64NpArray(self): t = tensor_util.make_tensor_proto( np.array([[(1+2j), (3+4j)], [(5+6j), (7+8j)]]), dtype=tf.complex64) # scomplex_val are real_0, imag_0, real_1, imag_1, ... self.assertProtoEquals(""" dtype: DT_COMPLEX64 tensor_shape { dim { size: 2 } dim { size: 2 } } scomplex_val: 1 scomplex_val: 2 scomplex_val: 3 scomplex_val: 4 scomplex_val: 5 scomplex_val: 6 scomplex_val: 7 scomplex_val: 8 """, t) a = tensor_util.MakeNdarray(t) self.assertEquals(np.complex64, a.dtype) self.assertAllEqual(np.array([[(1+2j), (3+4j)], [(5+6j), (7+8j)]]), a)
Example #10
Source File: dtypes_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testNumpyConversion(self): self.assertIs(tf.float32, tf.as_dtype(np.float32)) self.assertIs(tf.float64, tf.as_dtype(np.float64)) self.assertIs(tf.int32, tf.as_dtype(np.int32)) self.assertIs(tf.int64, tf.as_dtype(np.int64)) self.assertIs(tf.uint8, tf.as_dtype(np.uint8)) self.assertIs(tf.uint16, tf.as_dtype(np.uint16)) self.assertIs(tf.int16, tf.as_dtype(np.int16)) self.assertIs(tf.int8, tf.as_dtype(np.int8)) self.assertIs(tf.complex64, tf.as_dtype(np.complex64)) self.assertIs(tf.complex128, tf.as_dtype(np.complex128)) self.assertIs(tf.string, tf.as_dtype(np.object)) self.assertIs(tf.string, tf.as_dtype(np.array(["foo", "bar"]).dtype)) self.assertIs(tf.bool, tf.as_dtype(np.bool)) with self.assertRaises(TypeError): tf.as_dtype(np.dtype([("f1", np.uint), ("f2", np.int32)]))
Example #11
Source File: shape_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testTypes(self): types_to_test = { "bool": (tf.bool, bool), "float32": (tf.float32, float), "float64": (tf.float64, float), "complex64": (tf.complex64, complex), "complex128": (tf.complex128, complex), "uint8": (tf.uint8, int), "int32": (tf.int32, int), "int64": (tf.int64, int), bytes: (tf.string, bytes) } for dtype_np, (dtype_tf, cast) in types_to_test.items(): with self.test_session(use_gpu=True): inp = np.random.rand(4, 1).astype(dtype_np) a = tf.constant([cast(x) for x in inp.ravel(order="C")], shape=[4, 1], dtype=dtype_tf) tiled = tf.tile(a, [1, 4]) result = tiled.eval() self.assertEqual(result.shape, (4, 4)) self.assertEqual([4, 4], tiled.get_shape()) self.assertAllEqual(result, np.tile(inp, (1, 4)))
Example #12
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testTensorCompareTensor(self): x = np.linspace(-15, 15, 6).reshape(1, 3, 2) y = np.linspace(20, -10, 6).reshape(1, 3, 2) for t in [np.float16, np.float32, np.float64, np.int32, np.int64]: xt = x.astype(t) yt = y.astype(t) self._compareBoth(xt, yt, np.less, tf.less) self._compareBoth(xt, yt, np.less_equal, tf.less_equal) self._compareBoth(xt, yt, np.greater, tf.greater) self._compareBoth(xt, yt, np.greater_equal, tf.greater_equal) self._compareBoth(xt, yt, np.equal, tf.equal) self._compareBoth(xt, yt, np.not_equal, tf.not_equal) # TODO(zhifengc): complex64 doesn't work on GPU yet. for t in [np.complex64, np.complex128]: self._compareCpu(x.astype(t), y.astype(t), np.equal, tf.equal) self._compareCpu(x.astype(t), y.astype(t), np.not_equal, tf.not_equal)
Example #13
Source File: layers.py From neuron with GNU General Public License v3.0 | 6 votes |
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)
Example #14
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testDtype(self): with self.test_session(): d = tf.fill([2, 3], 12., name="fill") self.assertEqual(d.get_shape(), [2, 3]) # Test default type for both constant size and dynamic size z = tf.zeros([2, 3]) self.assertEqual(z.dtype, tf.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.eval(), np.zeros([2, 3])) z = tf.zeros(tf.shape(d)) self.assertEqual(z.dtype, tf.float32) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.eval(), np.zeros([2, 3])) # Test explicit type control for dtype in [tf.float32, tf.float64, tf.int32, tf.uint8, tf.int16, tf.int8, tf.complex64, tf.complex128, tf.int64, tf.bool]: z = tf.zeros([2, 3], dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.eval(), np.zeros([2, 3])) z = tf.zeros(tf.shape(d), dtype=dtype) self.assertEqual(z.dtype, dtype) self.assertEqual([2, 3], z.get_shape()) self.assertAllEqual(z.eval(), np.zeros([2, 3]))
Example #15
Source File: reversible_layers.py From BERT with Apache License 2.0 | 6 votes |
def one_hot_add(inputs, shift): """Performs (inputs + shift) % vocab_size in the one-hot space. Args: inputs: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot Tensor. shift: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot Tensor specifying how much to shift the corresponding one-hot vector in inputs. Soft values perform a "weighted shift": for example, shift=[0.2, 0.3, 0.5] performs a linear combination of 0.2 * shifting by zero; 0.3 * shifting by one; and 0.5 * shifting by two. Returns: Tensor of same shape and dtype as inputs. """ # Compute circular 1-D convolution with shift as the kernel. inputs = tf.cast(inputs, tf.complex64) shift = tf.cast(shift, tf.complex64) return tf.real(tf.signal.ifft(tf.signal.fft(inputs) * tf.signal.fft(shift)))
Example #16
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testValues(self): dtypes = [tf.float32, tf.float64, tf.int64, tf.int32, tf.complex64, tf.complex128] indices_flat = np.array([0, 4, 0, 8, 3, 8, 4, 7, 7, 3]) num_segments = 12 for indices in indices_flat, indices_flat.reshape(5, 2): shape = indices.shape + (2,) for dtype in dtypes: with self.test_session(use_gpu=self.use_gpu): tf_x, np_x = self._input(shape, dtype=dtype) np_ans = self._segmentReduce(indices, np_x, np.add, op2=None, num_out_rows=num_segments) s = tf.unsorted_segment_sum(data=tf_x, segment_ids=indices, num_segments=num_segments) tf_ans = s.eval() self._assertAllClose(indices, np_ans, tf_ans) self.assertShapeEqual(np_ans, s)
Example #17
Source File: cast_op_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def _testTypes(self, x, use_gpu=False): """Tests cast(x) to different tf.""" if use_gpu: type_list = [np.float32, np.float64, np.int64, np.complex64, np.complex128] else: type_list = [np.float32, np.float64, np.int32, np.int64, np.complex64, np.complex128] for from_type in type_list: for to_type in type_list: self._test(x.astype(from_type), to_type, use_gpu) self._test(x.astype(np.bool), np.float32, use_gpu) self._test(x.astype(np.uint8), np.float32, use_gpu) if not use_gpu: self._test(x.astype(np.bool), np.int32, use_gpu) self._test(x.astype(np.int32), np.int32, use_gpu)
Example #18
Source File: cast_op_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def _toDataType(self, dtype): """Returns TensorFlow data type for numpy type.""" if dtype == np.float32: return tf.float32 elif dtype == np.float64: return tf.float64 elif dtype == np.int32: return tf.int32 elif dtype == np.int64: return tf.int64 elif dtype == np.bool: return tf.bool elif dtype == np.complex64: return tf.complex64 elif dtype == np.complex128: return tf.complex128 else: return None
Example #19
Source File: audio.py From arabic-tacotron-tts with MIT License | 5 votes |
def _griffin_lim_tensorflow(S): '''TensorFlow implementation of Griffin-Lim Based on https://github.com/Kyubyong/tensorflow-exercises/blob/master/Audio_Processing.ipynb ''' with tf.variable_scope('griffinlim'): # TensorFlow's stft and istft operate on a batch of spectrograms; create batch of size 1 S = tf.expand_dims(S, 0) S_complex = tf.identity(tf.cast(S, dtype=tf.complex64)) y = _istft_tensorflow(S_complex) for i in range(hparams.griffin_lim_iters): est = _stft_tensorflow(y) angles = est / tf.cast(tf.maximum(1e-8, tf.abs(est)), tf.complex64) y = _istft_tensorflow(S_complex * angles) return tf.squeeze(y, 0)
Example #20
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testFillComplex64(self): np_ans = np.array([[0.15] * 3] * 2).astype(np.complex64) self._compare([2, 3], np_ans[0][0], np_ans, use_gpu=False)
Example #21
Source File: dtypes_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testIsUnsigned(self): self.assertEqual(tf.as_dtype("int8").is_unsigned, False) self.assertEqual(tf.as_dtype("int16").is_unsigned, False) self.assertEqual(tf.as_dtype("int32").is_unsigned, False) self.assertEqual(tf.as_dtype("int64").is_unsigned, False) self.assertEqual(tf.as_dtype("uint8").is_unsigned, True) self.assertEqual(tf.as_dtype("uint16").is_unsigned, True) self.assertEqual(tf.as_dtype("float32").is_unsigned, False) self.assertEqual(tf.as_dtype("float64").is_unsigned, False) self.assertEqual(tf.as_dtype("bool").is_unsigned, False) self.assertEqual(tf.as_dtype("string").is_unsigned, False) self.assertEqual(tf.as_dtype("complex64").is_unsigned, False) self.assertEqual(tf.as_dtype("complex128").is_unsigned, False) self.assertEqual(tf.as_dtype("bfloat16").is_integer, False)
Example #22
Source File: odes_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def test_odeint_all_dtypes(self): func = lambda y, t: y t = np.linspace(0.0, 1.0, 11) for y0_dtype in [tf.float32, tf.float64, tf.complex64, tf.complex128]: for t_dtype in [tf.float32, tf.float64]: y0 = tf.cast(1.0, y0_dtype) y_solved = tf.contrib.integrate.odeint(func, y0, tf.cast(t, t_dtype)) with self.test_session() as sess: y_solved = sess.run(y_solved) expected = np.asarray(np.exp(t)) self.assertAllClose(y_solved, expected, rtol=1e-5) self.assertEqual(tf.as_dtype(y_solved.dtype), y0_dtype)
Example #23
Source File: math_grad_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testComplexAbs(self): # Bias random test values away from zero to avoid numeric instabilities. self._testGrad([3, 3], dtype=tf.float32, max_error=2e-5, bias=0.1, sigma=1.0) self._testGrad([3, 3], dtype=tf.complex64, max_error=2e-5, bias=0.1, sigma=1.0) # Ensure stability near the pole at zero. self._testGrad([3, 3], dtype=tf.float32, max_error=100.0, bias=0.0, sigma=0.1) self._testGrad([3, 3], dtype=tf.complex64, max_error=100.0, bias=0.0, sigma=0.1)
Example #24
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testZerosLikeDtype(self): # Make sure zeros_like works even for dtypes that cannot be cast between with self.test_session(): shape = (3, 5) dtypes = np.float32, np.complex64 for in_type in dtypes: x = np.arange(15).astype(in_type).reshape(*shape) for out_type in dtypes: y = tf.zeros_like(x, dtype=out_type).eval() self.assertEqual(y.dtype, out_type) self.assertEqual(y.shape, shape) self.assertAllEqual(y, np.zeros(shape, dtype=out_type))
Example #25
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testZerosLikeCPU(self): for dtype in [tf.float32, tf.float64, tf.int32, tf.uint8, tf.int16, tf.int8, tf.complex64, tf.complex128, tf.int64]: self._compareZeros(dtype, False)
Example #26
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testComplex64(self): self._testAll( np.complex(1, 2) * np.arange(-15, 15).reshape([2, 3, 5]).astype( np.complex64)) self._testAll(np.complex( 1, 2) * np.random.normal(size=30).reshape([2, 3, 5]).astype( np.complex64)) self._testAll(np.empty((2, 0, 5)).astype(np.complex64))
Example #27
Source File: constant_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def _testGpu(self, x): np_ans = np.array(x) with self.test_session(use_gpu=True): tf_ans = tf.convert_to_tensor(x).eval() if np_ans.dtype in [np.float32, np.float64, np.complex64, np.complex128]: self.assertAllClose(np_ans, tf_ans) else: self.assertAllEqual(np_ans, tf_ans)
Example #28
Source File: cast_op_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testGradients(self): t = [tf.float32, tf.float64, tf.complex64, tf.complex128] for src_t in t: for dst_t in t: with self.test_session(): x = tf.constant(1.0, src_t) z = tf.identity(x) y = tf.cast(z, dst_t) err = tf.test.compute_gradient_error(x, [], y, []) self.assertLess(err, 1e-3)
Example #29
Source File: tensor_array_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testTensorArrayGradientWriteRead(self): for dtype in (np.float32, np.float64, np.int32, np.int64, np.complex64, np.complex128): self._testTensorArrayGradientWriteReadType(dtype)
Example #30
Source File: tensor_array_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testTensorArrayWriteGradientAddMultipleAdds(self): for dtype in (tf.int32, tf.int64, tf.float32, tf.float64, tf.complex64, tf.complex128): self._testTensorArrayWriteGradientAddMultipleAdds(dtype)