Python tensorflow.log1p() Examples
The following are 30
code examples of tensorflow.log1p().
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: univariate.py From zhusuan with MIT License | 6 votes |
def _sample(self, n_samples): # samples must be sampled from (-1, 1) rather than [-1, 1) loc, scale = self.loc, self.scale if not self.is_reparameterized: loc = tf.stop_gradient(loc) scale = tf.stop_gradient(scale) shape = tf.concat([[n_samples], self.batch_shape], 0) uniform_samples = tf.random_uniform( shape=shape, minval=np.nextafter(self.dtype.as_numpy_dtype(-1.), self.dtype.as_numpy_dtype(0.)), maxval=1., dtype=self.dtype) samples = loc - scale * tf.sign(uniform_samples) * \ tf.log1p(-tf.abs(uniform_samples)) static_n_samples = n_samples if isinstance(n_samples, int) else None samples.set_shape( tf.TensorShape([static_n_samples]).concatenate( self.get_batch_shape())) return samples
Example #2
Source File: sample.py From glas with Apache License 2.0 | 6 votes |
def calculate_latent_loss(self, latent_weights): """ Calculate the latent loss in the form of KL divergence """ for posterior in self.posteriors: # Minimize the chi squared divergence of the posterior 'f' from the prior 'g' (a # standard normal distribution), this amounts to minimizing the square of the difference # of the first moment of f from the first moment of g divided by the squared variance of # g (NOTE: mt_f is the t-th moment of the distribution f): # min(chisq) = (m1_f - m1_g)^2 / sigma_g^2 # # The idea behind using the chi squared divergence is that it is an upper bound for the # Kullback-Leibler divergence. The following inequality holds: # KL(f||g) <= log(1 + Chi^2(f||g)) # # So minimize this bound rather than the chi squared divergence directly mean, _ = self.compute_moments(posterior) axes = tf.range(1, tf.rank(mean)) chisq = tf.log1p(tf.square(mean - self.prior.mean()) / self.prior.variance()) chisq = tf.reduce_sum(latent_weights * chisq, axes) tf.losses.add_loss(tf.reduce_mean(chisq, name='chisq'))
Example #3
Source File: 1_simple_boston.py From deep-learning-note with MIT License | 6 votes |
def input_fn(partition, training, batch_size): """Generate an input function for the Estimator.""" def _input_fn(): if partition == "train": dataset = tf.data.Dataset.from_tensor_slices(({ FEATURES_KEY: tf.log1p(x_train) }, tf.log1p(y_train))) else: dataset = tf.data.Dataset.from_tensor_slices(({ FEATURES_KEY: tf.log1p(x_test) }, tf.log1p(y_test))) if training: dataset = dataset.shuffle(10 * batch_size, seed=RANDOM_SEED).repeat() dataset = dataset.batch(batch_size) iterator = dataset.make_one_shot_iterator() features, labels = iterator.get_next() return features, labels return _input_fn
Example #4
Source File: regularizers.py From BERT with Apache License 2.0 | 6 votes |
def __call__(self, x): """Computes regularization given an ed.Normal random variable as input.""" if not isinstance(x, ed.RandomVariable): raise ValueError('Input must be an ed.RandomVariable (for correct math, ' 'an ed.Normal random variable).') # Clip magnitude of dropout rate, where we get the dropout rate alpha from # the additive parameterization (Molchanov et al., 2017): for weight ~ # Normal(mu, sigma**2), the variance `sigma**2 = alpha * mu**2`. mean = x.distribution.mean() log_variance = tf.log(x.distribution.variance()) log_alpha = log_variance - tf.log(tf.square(mean) + tf.keras.backend.epsilon()) log_alpha = tf.clip_by_value(log_alpha, -8., 8.) # Set magic numbers for cubic polynomial approx. (Molchanov et al., 2017). k1 = 0.63576 k2 = 1.8732 k3 = 1.48695 c = -k1 output = tf.reduce_sum(k1 * tf.nn.sigmoid(k2 + k3 * log_alpha) + -0.5 * tf.log1p(tf.exp(-log_alpha)) + c) return output
Example #5
Source File: autoencoders.py From BERT with Apache License 2.0 | 6 votes |
def bottleneck(self, x): # pylint: disable=arguments-differ hparams = self.hparams if hparams.unordered: return super(AutoencoderOrderedDiscrete, self).bottleneck(x) noise = hparams.bottleneck_noise hparams.bottleneck_noise = 0.0 # We'll add noise below. x, loss = discretization.parametrized_bottleneck(x, hparams) hparams.bottleneck_noise = noise if hparams.mode == tf.estimator.ModeKeys.TRAIN: # We want a number p such that p^bottleneck_bits = 1 - noise. # So log(p) * bottleneck_bits = log(noise) log_p = tf.log1p(-float(noise) / 2) / float(hparams.bottleneck_bits) # Probabilities of flipping are p, p^2, p^3, ..., p^bottleneck_bits. noise_mask = 1.0 - tf.exp(tf.cumsum(tf.zeros_like(x) + log_p, axis=-1)) # Having the no-noise mask, we can make noise just uniformly at random. ordered_noise = tf.random_uniform(tf.shape(x)) # We want our noise to be 1s at the start and random {-1, 1} bits later. ordered_noise = tf.to_float(tf.less(noise_mask, ordered_noise)) # Now we flip the bits of x on the noisy positions (ordered and normal). x *= 2.0 * ordered_noise - 1 return x, loss
Example #6
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 6 votes |
def test_forward_unary(): def _test_forward_unary(op, a_min=1, a_max=5, dtype=np.float32): """test unary operators""" np_data = np.random.uniform(a_min, a_max, size=(2, 3, 5)).astype(dtype) tf.reset_default_graph() with tf.Graph().as_default(): in_data = tf.placeholder(dtype, (2, 3, 5), name="in_data") out = op(in_data) compare_tf_with_tvm([np_data], ['in_data:0'], out.name) _test_forward_unary(tf.acos, -1, 1) _test_forward_unary(tf.asin, -1, 1) _test_forward_unary(tf.atanh, -1, 1) _test_forward_unary(tf.sinh) _test_forward_unary(tf.cosh) _test_forward_unary(tf.acosh) _test_forward_unary(tf.asinh) _test_forward_unary(tf.atan) _test_forward_unary(tf.sin) _test_forward_unary(tf.cos) _test_forward_unary(tf.tan) _test_forward_unary(tf.tanh) _test_forward_unary(tf.erf) _test_forward_unary(tf.log) _test_forward_unary(tf.log1p)
Example #7
Source File: util.py From style-token_tacotron2 with MIT License | 5 votes |
def _log1p(x): #wrapper to support tensorflow tensors/numpy arrays isnumpy = isinstance(x, np.ndarray) isscalar = np.isscalar(x) return np.log1p(x) if (isnumpy or isscalar) else tf.log1p(x)
Example #8
Source File: Input.py From AdversarialAudioSeparation with MIT License | 5 votes |
def norm(magnitude): ''' Log(1 + magnitude) :param magnitude: Input magnitude spectrogram :return: Log-normalized magnitude spectrogram ''' return tf.log1p(magnitude)
Example #9
Source File: wavenet_encoder.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def _mu_law_encode(signal, channels, dtype): mu = tf.saturate_cast(channels - 1, dtype) safe_audio_abs = tf.minimum(tf.abs(signal), 1.0) magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu) signal = tf.sign(signal) * magnitude return tf.cast((signal + 1) / 2 * mu + 0.5, tf.int32)
Example #10
Source File: Utilities.py From DeepDenoiser with Apache License 2.0 | 5 votes |
def signed_log1p(inputs): return tf.multiply(tf.sign(inputs), tf.log1p(tf.abs(inputs)))
Example #11
Source File: activation.py From SchNet with MIT License | 5 votes |
def _softplus(x): return tf.log1p(tf.exp(x))
Example #12
Source File: activation_fn.py From PhysNet with MIT License | 5 votes |
def _softplus(x): return tf.log1p(tf.exp(x))
Example #13
Source File: activation_fn.py From PhysNet with MIT License | 5 votes |
def smooth_ELU(x): return tf.log1p(1.718281828459045*tf.exp(x))-1.0 #(e-1) = 1.718281828459045
Example #14
Source File: nn_extra_nvp.py From bruno with MIT License | 5 votes |
def backward(self, y, z, sum_log_det_jacobian): x = tf.sigmoid(y) x = (x - 0.5 * self.alpha) / (1 - self.alpha) jac = tf.reduce_sum(y - 2. * tf.log1p(tf.exp(y)) - tf.log(1. - self.alpha), [1, 2, 3]) sum_log_det_jacobian += jac return x, sum_log_det_jacobian
Example #15
Source File: nn_extra_student.py From bruno with MIT License | 5 votes |
def get_log_likelihood(self, observation, mask_dim=None): x = observation mu, var, nu = self.current_distribution ln_gamma_quotient = tf.lgamma((1. + nu) / 2.) - tf.lgamma(nu / 2.) ln_nom = (-(1. + nu) / 2.) * tf.log1p(tf.square(x - mu) / ((nu - 2.) * var)) ln_denom = 0.5 * tf.log((nu - 2.) * np.pi * var) log_pdf = ln_gamma_quotient + ln_nom - ln_denom if mask_dim is not None: return tf.reduce_sum(log_pdf * mask_dim, 1) else: return tf.reduce_sum(log_pdf, 1)
Example #16
Source File: nn_extra_student.py From bruno with MIT License | 5 votes |
def get_log_likelihood_under_prior(self, observation, mask_dim=None): x = observation mu, var, nu = self.prior ln_gamma_quotient = tf.lgamma((1. + nu) / 2.) - tf.lgamma(nu / 2.) ln_nom = (-(1. + nu) / 2.) * tf.log1p((tf.square(x - mu) / ((nu - 2.) * var))) ln_denom = 0.5 * tf.log((nu - 2.) * np.pi * var) log_pdf = ln_gamma_quotient + ln_nom - ln_denom if mask_dim is not None: return tf.reduce_sum(log_pdf * mask_dim, 1) else: return tf.reduce_sum(log_pdf, 1)
Example #17
Source File: ops.py From Tacotron2-Wavenet-Korean-TTS with MIT License | 5 votes |
def mu_law_encode(audio, quantization_channels): '''Quantizes waveform amplitudes.''' with tf.name_scope('encode'): mu = tf.to_float(quantization_channels - 1) # Perform mu-law companding transformation (ITU-T, 1988). # Minimum operation is here to deal with rare large amplitudes caused # by resampling. safe_audio_abs = tf.minimum(tf.abs(audio), 1.0) magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu) # tf.log1p(x) = log(1+x) signal = tf.sign(audio) * magnitude # Quantize signal to the specified number of levels. return tf.to_int32((signal + 1) / 2 * mu + 0.5)
Example #18
Source File: audio.py From Tacotron2-Wavenet-Korean-TTS with MIT License | 5 votes |
def _log1p(x): #wrapper to support tensorflow tensors/numpy arrays isnumpy = isinstance(x, np.ndarray) isscalar = np.isscalar(x) return np.log1p(x) if (isnumpy or isscalar) else tf.log1p(x)
Example #19
Source File: tensorflow.py From deepx with MIT License | 5 votes |
def log1p(self, x): return tf.log1p(x)
Example #20
Source File: resnet_main.py From class-balanced-loss with MIT License | 5 votes |
def focal_loss(labels, logits, alpha, gamma): """Compute the focal loss between `logits` and the ground truth `labels`. Focal loss = -alpha_t * (1-pt)^gamma * log(pt) where pt is the probability of being classified to the true class. pt = p (if true class), otherwise pt = 1 - p. p = sigmoid(logit). Args: labels: A float32 tensor of size [batch, num_classes]. logits: A float32 tensor of size [batch, num_classes]. alpha: A float32 tensor of size [batch_size] specifying per-example weight for balanced cross entropy. gamma: A float32 scalar modulating loss from hard and easy examples. Returns: focal_loss: A float32 scalar representing normalized total loss. """ with tf.name_scope('focal_loss'): logits = tf.cast(logits, dtype=tf.float32) cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits( labels=labels, logits=logits) # positive_label_mask = tf.equal(labels, 1.0) # probs = tf.sigmoid(logits) # probs_gt = tf.where(positive_label_mask, probs, 1.0 - probs) # # With gamma < 1, the implementation could produce NaN during back prop. # modulator = tf.pow(1.0 - probs_gt, gamma) # A numerically stable implementation of modulator. if gamma == 0.0: modulator = 1.0 else: modulator = tf.exp(-gamma * labels * logits - gamma * tf.log1p( tf.exp(-1.0 * logits))) loss = modulator * cross_entropy weighted_loss = alpha * loss focal_loss = tf.reduce_sum(weighted_loss) # Normalize by the total number of positive samples. focal_loss /= tf.reduce_sum(labels) return focal_loss
Example #21
Source File: cifar_main.py From class-balanced-loss with MIT License | 5 votes |
def focal_loss(labels, logits, alpha, gamma): """Compute the focal loss between `logits` and the ground truth `labels`. Focal loss = -alpha_t * (1-pt)^gamma * log(pt) where pt is the probability of being classified to the true class. pt = p (if true class), otherwise pt = 1 - p. p = sigmoid(logit). Args: labels: A float32 tensor of size [batch, num_classes]. logits: A float32 tensor of size [batch, num_classes]. alpha: A float32 tensor of size [batch_size] specifying per-example weight for balanced cross entropy. gamma: A float32 scalar modulating loss from hard and easy examples. Returns: focal_loss: A float32 scalar representing normalized total loss. """ with tf.name_scope('focal_loss'): logits = tf.cast(logits, dtype=tf.float32) cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits( labels=labels, logits=logits) # positive_label_mask = tf.equal(labels, 1.0) # probs = tf.sigmoid(logits) # probs_gt = tf.where(positive_label_mask, probs, 1.0 - probs) # # With gamma < 1, the implementation could produce NaN during back prop. # modulator = tf.pow(1.0 - probs_gt, gamma) # A numerically stable implementation of modulator. if gamma == 0.0: modulator = 1.0 else: modulator = tf.exp(-gamma * labels * logits - gamma * tf.log1p( tf.exp(-1.0 * logits))) loss = modulator * cross_entropy weighted_loss = alpha * loss focal_loss = tf.reduce_sum(weighted_loss) # Normalize by the total number of positive samples. focal_loss /= tf.reduce_sum(labels) return focal_loss
Example #22
Source File: sample_rnn_unaligned_speech_ljspeech.py From representation_mixing with BSD 3-Clause "New" or "Revised" License | 5 votes |
def logmel(waveform): z = tf.contrib.signal.stft(waveform, window_size, step) magnitudes = tf.abs(z) filterbank = tf.contrib.signal.linear_to_mel_weight_matrix( num_mel_bins=n_mel, num_spectrogram_bins=magnitudes.shape[-1].value, sample_rate=sample_rate, lower_edge_hertz=125., upper_edge_hertz=7800.) melspectrogram = tf.tensordot(magnitudes, filterbank, 1) return tf.log1p(melspectrogram)
Example #23
Source File: sample_rnn_unaligned_speech_ljspeech.py From representation_mixing with BSD 3-Clause "New" or "Revised" License | 5 votes |
def logmel(waveform): z = tf.contrib.signal.stft(waveform, window_size, step) magnitudes = tf.abs(z) filterbank = tf.contrib.signal.linear_to_mel_weight_matrix( num_mel_bins=n_mel, num_spectrogram_bins=magnitudes.shape[-1].value, sample_rate=sample_rate, lower_edge_hertz=125., upper_edge_hertz=7800.) melspectrogram = tf.tensordot(magnitudes, filterbank, 1) return tf.log1p(melspectrogram)
Example #24
Source File: sample_rnn_unaligned_speech_ljspeech.py From representation_mixing with BSD 3-Clause "New" or "Revised" License | 5 votes |
def logmel(waveform): z = tf.contrib.signal.stft(waveform, window_size, step) magnitudes = tf.abs(z) filterbank = tf.contrib.signal.linear_to_mel_weight_matrix( num_mel_bins=n_mel, num_spectrogram_bins=magnitudes.shape[-1].value, sample_rate=sample_rate, lower_edge_hertz=125., upper_edge_hertz=7800.) melspectrogram = tf.tensordot(magnitudes, filterbank, 1) return tf.log1p(melspectrogram)
Example #25
Source File: magrecnp.py From representation_mixing with BSD 3-Clause "New" or "Revised" License | 5 votes |
def logmel(waveform): z = tf.contrib.signal.stft(waveform, window_size, step) magnitudes = tf.abs(z) filterbank = tf.contrib.signal.linear_to_mel_weight_matrix( num_mel_bins=n_mel, num_spectrogram_bins=magnitudes.shape[-1].value, sample_rate=sample_rate, lower_edge_hertz=125., upper_edge_hertz=7800.) melspectrogram = tf.tensordot(magnitudes, filterbank, 1) return tf.log1p(melspectrogram)
Example #26
Source File: magrecnp.py From representation_mixing with BSD 3-Clause "New" or "Revised" License | 5 votes |
def logmel2(waveform): res = np.abs(stft(waveform, windowsize=window_size, step=step, real=False, compute_onesided=True)) mels = linear_to_mel_weight_matrix( res.shape[1], sample_rate, lower_edge_hertz=125., upper_edge_hertz=7800., n_filts=n_mel, dtype=np.float64) mel_res = np.dot(res, mels) return np.log1p(mel_res)
Example #27
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testFloatBasic(self): x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float32) y = (x + .5).astype(np.float32) # no zero z = (x + 15.5).astype(np.float32) # all positive k = np.arange(-0.90, 0.90, 0.25).astype(np.float32) # between -1 and 1 self._compareBoth(x, np.abs, tf.abs) self._compareBoth(x, np.abs, _ABS) self._compareBoth(x, np.negative, tf.neg) self._compareBoth(x, np.negative, _NEG) self._compareBoth(y, self._inv, tf.inv) self._compareBoth(x, np.square, tf.square) self._compareBoth(z, np.sqrt, tf.sqrt) self._compareBoth(z, self._rsqrt, tf.rsqrt) self._compareBoth(x, np.exp, tf.exp) self._compareBoth(z, np.log, tf.log) self._compareBoth(z, np.log1p, tf.log1p) self._compareBoth(x, np.tanh, tf.tanh) self._compareBoth(x, self._sigmoid, tf.sigmoid) self._compareBoth(y, np.sign, tf.sign) self._compareBoth(x, np.sin, tf.sin) self._compareBoth(x, np.cos, tf.cos) self._compareBoth(k, np.arcsin, tf.asin) self._compareBoth(k, np.arccos, tf.acos) self._compareBoth(x, np.arctan, tf.atan) self._compareBoth(x, np.tan, tf.tan) self._compareBoth( y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)), tf.lgamma) self._compareBoth(x, np.vectorize(math.erf), tf.erf) self._compareBoth(x, np.vectorize(math.erfc), tf.erfc) self._compareBothSparse(x, np.abs, tf.abs) self._compareBothSparse(x, np.negative, tf.neg) self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) self._compareBothSparse(y, np.sign, tf.sign) self._compareBothSparse(x, np.vectorize(math.erf), tf.erf)
Example #28
Source File: ops.py From tensorflow-wavenet with MIT License | 5 votes |
def mu_law_encode(audio, quantization_channels): '''Quantizes waveform amplitudes.''' with tf.name_scope('encode'): mu = tf.to_float(quantization_channels - 1) # Perform mu-law companding transformation (ITU-T, 1988). # Minimum operation is here to deal with rare large amplitudes caused # by resampling. safe_audio_abs = tf.minimum(tf.abs(audio), 1.0) magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu) signal = tf.sign(audio) * magnitude # Quantize signal to the specified number of levels. return tf.to_int32((signal + 1) / 2 * mu + 0.5)
Example #29
Source File: losses.py From EnglishSpeechUpsampler with MIT License | 5 votes |
def geo_mean(sname, true, model): with tf.name_scope(sname): waveform_loss = tf.exp(tf.reduce_mean(tf.log1p( tf.abs(tf.subtract(true, model))))) tf.summary.scalar(sname, waveform_loss) return waveform_loss
Example #30
Source File: layers.py From variance-networks with Apache License 2.0 | 5 votes |
def pt_dense(input_tensor, num_inputs, num_outputs, name, stochastic=True, with_bias=True, reuse=False): with tf.variable_scope(name) as scope: W = tf.get_variable('W', [num_inputs, num_outputs], initializer=tf.truncated_normal_initializer(1e-2), dtype=tf.float32, trainable=True) log_alpha = tf.get_variable('log_alpha', [], initializer=tf.constant_initializer(-10.0), dtype=tf.float32, trainable=True) log_alpha = tf.clip_by_value(log_alpha, -20.0, 20.0) if not reuse: # computing reg k1, k2, k3 = 0.63576, 1.8732, 1.48695 C = -k1 mdkl = k1 * tf.nn.sigmoid(k2 + k3 * log_alpha) - 0.5 * tf.log1p(tf.exp(-log_alpha)) + C kl = -tf.reduce_sum(mdkl) * tf.reduce_prod(tf.cast(W.get_shape(), tf.float32)) tf.add_to_collection('kl_loss', kl) # computing output mu = tf.matmul(input_tensor, W) si = tf.sqrt(tf.matmul(input_tensor * input_tensor, tf.exp(log_alpha) * W * W) + 1e-16) output = mu if stochastic: output += tf.random_normal(mu.shape, mean=0, stddev=1) * si if with_bias: biases = tf.get_variable('biases', num_outputs, tf.float32, tf.constant_initializer(0.0)) output = tf.nn.bias_add(output, biases) # summaries if not reuse: if with_bias: error = 0.5*(1.0+tf.erf((-mu-biases)/tf.sqrt(2.0)/si)) else: error = 0.5*(1.0+tf.erf((-mu)/tf.sqrt(2.0)/si)) tf.summary.scalar('error', tf.reduce_sum(error)) tf.summary.scalar('log_alpha', log_alpha) tf.add_to_collection('log_alpha', log_alpha) return output