本文整理汇总了Python中tensorflow.real函数的典型用法代码示例。如果您正苦于以下问题:Python real函数的具体用法?Python real怎么用?Python real使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了real函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: antenna_jones
def antenna_jones(lm, stokes, alpha, ref_freq):
"""
Compute the jones terms for each antenna.
lm, stokes and alpha are the source variables.
"""
# Compute the complex phase
cplx_phase = rime.phase(lm, D.uvw, D.frequency, CT=CT)
# Check for nans/infs in the complex phase
phase_msg = ("Check that '1 - l**2 - m**2 >= 0' holds "
"for all your lm coordinates. This is required "
"for 'n = sqrt(1 - l**2 - m**2) - 1' "
"to be finite.")
phase_real = tf.check_numerics(tf.real(cplx_phase), phase_msg)
phase_imag = tf.check_numerics(tf.imag(cplx_phase), phase_msg)
# Compute the square root of the brightness matrix
# (as well as the sign)
bsqrt, sgn_brightness = rime.b_sqrt(stokes, alpha,
D.frequency, ref_freq, CT=CT,
polarisation_type=polarisation_type)
# Check for nans/infs in the bsqrt
bsqrt_msg = ("Check that your stokes parameters "
"satisfy I**2 >= Q**2 + U**2 + V**2. "
"Montblanc performs a cholesky decomposition "
"of the brightness matrix and the above must "
"hold for this to produce valid values.")
bsqrt_real = tf.check_numerics(tf.real(bsqrt), bsqrt_msg)
bsqrt_imag = tf.check_numerics(tf.imag(bsqrt), bsqrt_msg)
# Compute the direction dependent effects from the beam
ejones = rime.e_beam(lm, D.frequency,
D.pointing_errors, D.antenna_scaling,
beam_sin, beam_cos,
D.beam_extents, D.beam_freq_map, D.ebeam)
deps = [phase_real, phase_imag, bsqrt_real, bsqrt_imag]
deps = [] # Do nothing for now
# Combine the brightness square root, complex phase,
# feed rotation and beam dde's
with tf.control_dependencies(deps):
antenna_jones = rime.create_antenna_jones(bsqrt, cplx_phase,
feed_rotation, ejones, FT=FT)
return antenna_jones, sgn_brightness
示例2: get_mu_tensor
def get_mu_tensor(self):
const_fact = self._dist_to_opt_avg**2 * self._h_min**2 / 2 / self._grad_var
coef = tf.Variable([-1.0, 3.0, 0.0, 1.0], dtype=tf.float32, name="cubic_solver_coef")
coef = tf.scatter_update(coef, tf.constant(2), -(3 + const_fact) )
roots = tf.py_func(np.roots, [coef], Tout=tf.complex64, stateful=False)
# filter out the correct root
root_idx = tf.logical_and(tf.logical_and(tf.greater(tf.real(roots), tf.constant(0.0) ),
tf.less(tf.real(roots), tf.constant(1.0) ) ), tf.less(tf.abs(tf.imag(roots) ), 1e-5) )
# in case there are two duplicated roots satisfying the above condition
root = tf.reshape(tf.gather(tf.gather(roots, tf.where(root_idx) ), tf.constant(0) ), shape=[] )
tf.assert_equal(tf.size(root), tf.constant(1) )
dr = self._h_max / self._h_min
mu = tf.maximum(tf.real(root)**2, ( (tf.sqrt(dr) - 1)/(tf.sqrt(dr) + 1) )**2)
return mu
示例3: _inference
def _inference(self, x, dropout):
with tf.name_scope('conv1'):
# Transform to Fourier domain
x_2d = tf.reshape(x, [-1, 28, 28])
x_2d = tf.complex(x_2d, 0)
xf_2d = tf.fft2d(x_2d)
xf = tf.reshape(xf_2d, [-1, NFEATURES])
xf = tf.expand_dims(xf, 1) # NSAMPLES x 1 x NFEATURES
xf = tf.transpose(xf) # NFEATURES x 1 x NSAMPLES
# Filter
Wreal = self._weight_variable([int(NFEATURES/2), self.F, 1])
Wimg = self._weight_variable([int(NFEATURES/2), self.F, 1])
W = tf.complex(Wreal, Wimg)
xf = xf[:int(NFEATURES/2), :, :]
yf = tf.matmul(W, xf) # for each feature
yf = tf.concat([yf, tf.conj(yf)], axis=0)
yf = tf.transpose(yf) # NSAMPLES x NFILTERS x NFEATURES
yf_2d = tf.reshape(yf, [-1, 28, 28])
# Transform back to spatial domain
y_2d = tf.ifft2d(yf_2d)
y_2d = tf.real(y_2d)
y = tf.reshape(y_2d, [-1, self.F, NFEATURES])
# Bias and non-linearity
b = self._bias_variable([1, self.F, 1])
# b = self._bias_variable([1, self.F, NFEATURES])
y += b # NSAMPLES x NFILTERS x NFEATURES
y = tf.nn.relu(y)
with tf.name_scope('fc1'):
W = self._weight_variable([self.F*NFEATURES, NCLASSES])
b = self._bias_variable([NCLASSES])
y = tf.reshape(y, [-1, self.F*NFEATURES])
y = tf.matmul(y, W) + b
return y
示例4: fft_conv
def fft_conv(self, source, filters, width, height, stride, activation='relu', name='fft_conv'):
# This function implements a convolution using a spectrally parameterized filter with the normal
# tf.nn.conv2d() convolution operation. This is done by transforming the spectral filter to spatial
# via tf.batch_ifft2d()
channels = source.get_shape().as_list()[3]
with tf.variable_scope(name):
init = self.random_spatial_to_spectral(channels, filters, height, width)
if name in self.initialization:
init = self.initialization[name]
# Option 1: Over-Parameterize fully in the spectral domain
# w_real = tf.Variable(init.real, dtype=tf.float32, name='real')
# w_imag = tf.Variable(init.imag, dtype=tf.float32, name='imag')
# w = tf.cast(tf.complex(w_real, w_imag), tf.complex64)
# Option 2: Parameterize only 'free' parameters in the spectral domain to enforce conjugate symmetry
# This is very slow.
w = self.spectral_to_variable(init)
b = tf.Variable(tf.constant(0.1, shape=[filters]))
# Transform the spectral parameters into a spatial filter
# and reshape for tf.nn.conv2d
complex_spatial_filter = tf.batch_ifft2d(w)
spatial_filter = tf.real(complex_spatial_filter)
spatial_filter = tf.transpose(spatial_filter, [2, 3, 0, 1])
conv = tf.nn.conv2d(source, spatial_filter, strides=[1, stride, stride, 1], padding='SAME')
output = tf.nn.bias_add(conv, b)
output = tf.nn.relu(output) if activation is 'relu' else output
return output, spatial_filter, w
示例5: __call__
def __call__(self, inputs, state, scope=None ):
with tf.variable_scope(scope or type(self).__name__):
unitary_hidden_state, secondary_cell_hidden_state = tf.split(1,2,state)
mat_in = tf.get_variable('mat_in', [self.input_size, self.state_size*2])
mat_out = tf.get_variable('mat_out', [self.state_size*2, self.output_size])
in_proj = tf.matmul(inputs, mat_in)
in_proj_c = tf.complex(tf.split(1,2,in_proj))
out_state = modReLU( in_proj_c +
ulinear(unitary_hidden_state, self.state_size),
tf.get_variable(name='bias', dtype=tf.float32, shape=tf.shape(unitary_hidden_state), initializer = tf.constant_initalizer(0.)),
scope=scope)
with tf.variable_scope('unitary_output'):
'''computes data linear, unitary linear and summation -- TODO: should be complex output'''
unitary_linear_output_real = linear.linear([tf.real(out_state), tf.imag(out_state), inputs], True, 0.0)
with tf.variable_scope('scale_nonlinearity'):
modulus = tf.complex_abs(unitary_linear_output_real)
rescale = tf.maximum(modulus + hidden_bias, 0.) / (modulus + 1e-7)
#transition to data shortcut connection
#out_ = tf.matmul(tf.concat(1,[tf.real(out_state), tf.imag(out_state), ] ), mat_out) + out_bias
#hidden state is complex but output is completely real
return out_, out_state #complex
示例6: sparse_dot_product0
def sparse_dot_product0(emb, tuples, use_matmul=True, output_type='real'):
"""
Compute the dot product of complex vectors.
It uses complex vectors but tensorflow does not optimize in the complex space (or there is a bug in the gradient
propagation with complex numbers...)
:param emb: embeddings
:param tuples: indices at which we compute dot products
:return: scores (dot products)
"""
n_t = tuples.get_shape()[0].value
rk = emb.get_shape()[1].value
emb_sel_a = tf.gather(emb, tuples[:, 0])
emb_sel_b = tf.gather(emb, tuples[:, 1])
if use_matmul:
pred_cplx = tf.squeeze(tf.batch_matmul(
tf.reshape(emb_sel_a, [n_t, rk, 1]),
tf.reshape(emb_sel_b, [n_t, rk, 1]), adj_x=True))
else:
pred_cplx = tf.reduce_sum(tf.mul(tf.conj(emb_sel_a), emb_sel_b), 1)
if output_type == 'complex':
return pred_cplx
elif output_type == 'real':
return tf.real(pred_cplx) + tf.imag(pred_cplx)
elif output_type == 'real':
return tf.abs(pred_cplx)
elif output_type == 'angle':
raise NotImplementedError('No argument or inverse-tanh function for complex number in Tensorflow')
else:
raise NotImplementedError()
示例7: _compareMulGradient
def _compareMulGradient(self, data):
# data is a float matrix of shape [n, 4]. data[:, 0], data[:, 1],
# data[:, 2], data[:, 3] are real parts of x, imaginary parts of
# x, real parts of y and imaginary parts of y.
with self.test_session():
inp = tf.convert_to_tensor(data)
xr, xi, yr, yi = tf.split(1, 4, inp)
def vec(x): # Reshape to a vector
return tf.reshape(x, [-1])
xr, xi, yr, yi = vec(xr), vec(xi), vec(yr), vec(yi)
def cplx(r, i): # Combine to a complex vector
return tf.complex(r, i)
x, y = cplx(xr, xi), cplx(yr, yi)
# z is x times y in complex plane.
z = x * y
# Defines the loss function as the sum of all coefficients of z.
loss = tf.reduce_sum(tf.real(z) + tf.imag(z))
epsilon = 0.005
jacob_t, jacob_n = tf.test.compute_gradient(inp,
list(data.shape),
loss,
[1],
x_init_value=data,
delta=epsilon)
self.assertAllClose(jacob_t, jacob_n, rtol=epsilon, atol=epsilon)
示例8: get_reconstructed_image
def get_reconstructed_image(self, real, imag, name=None):
"""
:param real:
:param imag:
:param name:
:return:
"""
complex_k_space_label = tf.complex(real=tf.squeeze(real), imag=tf.squeeze(imag), name=name+"_complex_k_space")
rec_image_complex = tf.expand_dims(tf.ifft2d(complex_k_space_label), axis=1)
rec_image_real = tf.reshape(tf.real(rec_image_complex), shape=[-1, 1, self.dims_out[1], self.dims_out[2]])
rec_image_imag = tf.reshape(tf.imag(rec_image_complex), shape=[-1, 1, self.dims_out[1], self.dims_out[2]])
# Shifting
top, bottom = tf.split(rec_image_real, num_or_size_splits=2, axis=2)
top_left, top_right = tf.split(top, num_or_size_splits=2, axis=3)
bottom_left, bottom_right = tf.split(bottom, num_or_size_splits=2, axis=3)
top_shift = tf.concat(axis=3, values=[bottom_right, bottom_left])
bottom_shift = tf.concat(axis=3, values=[top_right, top_left])
shifted_image = tf.concat(axis=2, values=[top_shift, bottom_shift])
# Shifting
top_imag, bottom_imag = tf.split(rec_image_imag, num_or_size_splits=2, axis=2)
top_left_imag, top_right_imag = tf.split(top_imag, num_or_size_splits=2, axis=3)
bottom_left_imag, bottom_right_imag = tf.split(bottom_imag, num_or_size_splits=2, axis=3)
top_shift_imag = tf.concat(axis=3, values=[bottom_right_imag, bottom_left_imag])
bottom_shift_imag = tf.concat(axis=3, values=[top_right_imag, top_left_imag])
shifted_image_imag = tf.concat(axis=2, values=[top_shift_imag, bottom_shift_imag])
shifted_image_two_channels = tf.stack([shifted_image[:,0,:,:], shifted_image_imag[:,0,:,:]], axis=1)
return shifted_image_two_channels
示例9: visualize_data_transformations
def visualize_data_transformations():
records = glob.glob(os.path.join(utils.working_dir, 'train_fragment_*.tfrecords'))
dataset = tf.data.TFRecordDataset(records)
dataset = dataset.map(parse_tfrecord_raw)
dataset = dataset.repeat()
dataset = dataset.shuffle(buffer_size=10)
dataset = dataset.prefetch(2)
it = dataset.make_one_shot_iterator()
data_x = tf.placeholder(tf.float32, shape=(utils.sample_rate * utils.audio_clip_len,))
data_y = tf.placeholder(tf.float32, shape=(utils.timesteps,))
stfts = tf.contrib.signal.stft(data_x, frame_length=utils.frame_length, frame_step=utils.frame_step,
fft_length=4096)
power_stfts = tf.real(stfts * tf.conj(stfts))
magnitude_spectrograms = tf.abs(stfts)
power_magnitude_spectrograms = tf.abs(power_stfts)
num_spectrogram_bins = magnitude_spectrograms.shape[-1].value
# scale frequency to mel scale and put into bins to reduce dimensionality
lower_edge_hertz, upper_edge_hertz = 30.0, 17000.0
num_mel_bins = utils.mel_bins_base * 4
linear_to_mel_weight_matrix = tf.contrib.signal.linear_to_mel_weight_matrix(
num_mel_bins, num_spectrogram_bins, utils.sample_rate, lower_edge_hertz,
upper_edge_hertz)
mel_spectrograms = tf.tensordot(magnitude_spectrograms, linear_to_mel_weight_matrix, 1)
mel_spectrograms.set_shape(
magnitude_spectrograms.shape[:-1].concatenate(linear_to_mel_weight_matrix.shape[-1:]))
# log scale the mel bins to better represent human loudness perception
log_offset = 1e-6
log_mel_spectrograms = tf.log(mel_spectrograms + log_offset)
# compute first order differential and concat. "It indicates a raise or reduction of the energy for each
# frequency bin at a frame relative to its predecessor"
first_order_diff = tf.abs(
tf.subtract(log_mel_spectrograms, tf.manip.roll(log_mel_spectrograms, shift=1, axis=1)))
mel_fod = tf.concat([log_mel_spectrograms, first_order_diff], 1)
with tf.Session() as sess:
while True:
try:
raw_x, raw_y = sess.run(it.get_next())
np_stfts = sess.run(power_stfts, feed_dict={data_x: raw_x})
np_magnitude_spectrograms = sess.run(power_magnitude_spectrograms, feed_dict={data_x: raw_x})
np_mel_spectrograms = sess.run(mel_spectrograms, feed_dict={data_x: raw_x})
np_log_mel_spectrograms = sess.run(log_mel_spectrograms, feed_dict={data_x: raw_x})
np_mel_fod = sess.run(mel_fod, feed_dict={data_x: raw_x})
utils.plot_signal_transforms(raw_x,
np_stfts,
np_magnitude_spectrograms,
np_mel_spectrograms,
np_log_mel_spectrograms,
np_mel_fod)
print('wank')
except tf.errors.OutOfRangeError:
break
示例10: bilinear_pool
def bilinear_pool(self, x1, x2):
p1 = tf.matmul(x1, self.C[0])
p2 = tf.matmul(x2, self.C[1])
pc1 = tf.complex(p1, tf.zeros_like(p1))
pc2 = tf.complex(p2, tf.zeros_like(p2))
conved = tf.batch_ifft(tf.batch_fft(pc1) * tf.batch_fft(pc2))
return tf.real(conved)
示例11: compute_spectrograms
def compute_spectrograms(self, waveforms, labels=None):
"""Computes spectrograms for a batch of waveforms."""
s = self.settings
# Set final dimension of waveforms, which comes to us as `None`.
self._set_waveforms_shape(waveforms)
# Compute STFTs.
waveforms = tf.cast(waveforms, tf.float32)
stfts = tf.contrib.signal.stft(
waveforms, self.window_size, self.hop_size,
fft_length=self.dft_size, window_fn=self.window_fn)
# Slice STFTs along frequency axis.
stfts = stfts[..., self.freq_start_index:self.freq_end_index]
# Get STFT magnitudes squared, i.e. squared spectrograms.
grams = tf.real(stfts * tf.conj(stfts))
# gram = tf.abs(stft) ** 2
# Take natural log of squared spectrograms. Adding an epsilon
# avoids log-of-zero errors.
grams = tf.log(grams + s.spectrogram_log_epsilon)
# Clip spectrograms if indicated.
if s.spectrogram_clipping_enabled:
grams = tf.clip_by_value(
grams, s.spectrogram_clipping_min, s.spectrogram_clipping_max)
# Normalize spectrograms if indicated.
if s.spectrogram_normalization_enabled:
grams = \
s.spectrogram_normalization_scale_factor * grams + \
s.spectrogram_normalization_offset
# Reshape spectrograms for input into Keras neural network.
grams = self._reshape_grams(grams)
# Create features dictionary.
features = {self.output_feature_name: grams}
if labels is None:
return features
else:
# have labels
# Reshape labels into a single 2D column.
labels = tf.reshape(labels, (-1, 1))
return features, labels
示例12: _compareRealImag
def _compareRealImag(self, cplx, use_gpu):
np_real, np_imag = np.real(cplx), np.imag(cplx)
with self.test_session(use_gpu=use_gpu) as sess:
inx = tf.convert_to_tensor(cplx)
tf_real = tf.real(inx)
tf_imag = tf.imag(inx)
tf_real_val, tf_imag_val = sess.run([tf_real, tf_imag])
self.assertAllEqual(np_real, tf_real_val)
self.assertAllEqual(np_imag, tf_imag_val)
self.assertShapeEqual(np_real, tf_real)
self.assertShapeEqual(np_imag, tf_imag)
示例13: one_bp_iteration
def one_bp_iteration(self, xe_v2c_pre_iter, H_sumC_to_V, H_sumV_to_C, xe_0):
xe_tanh = tf.tanh(tf.to_double(tf.truediv(xe_v2c_pre_iter, [2.0])))
xe_tanh = tf.to_float(xe_tanh)
xe_tanh_temp = tf.sign(xe_tanh)
xe_sum_log_img = tf.matmul(H_sumC_to_V, tf.multiply(tf.truediv((1 - xe_tanh_temp), [2.0]), [3.1415926]))
xe_sum_log_real = tf.matmul(H_sumC_to_V, tf.log(1e-8 + tf.abs(xe_tanh)))
xe_sum_log_complex = tf.complex(xe_sum_log_real, xe_sum_log_img)
xe_product = tf.real(tf.exp(xe_sum_log_complex))
xe_product_temp = tf.multiply(tf.sign(xe_product), -2e-7)
xe_pd_modified = tf.add(xe_product, xe_product_temp)
xe_v_sumc = tf.multiply(self.atanh(xe_pd_modified), [2.0])
xe_c_sumv = tf.add(xe_0, tf.matmul(H_sumV_to_C, xe_v_sumc))
return xe_v_sumc, xe_c_sumv
示例14: _checkGrad
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)
示例15: c2q1d
def c2q1d(x):
""" An internal function to convert a 1D Complex vector back to a real
array, which is twice the height of x.
"""
# Input has shape [batch, r, c, 2]
r, c = x.get_shape().as_list()[1:3]
x1 = tf.real(x)
x2 = tf.imag(x)
# Stack 2 inputs of shape [batch, r, c] to [batch, r, 2, c]
y = tf.stack([x1, x2], axis=-2)
# Reshaping interleaves the results
y = tf.reshape(y, [-1, 2 * r, c])
return y