本文整理汇总了Python中tensorflow.is_nan函数的典型用法代码示例。如果您正苦于以下问题:Python is_nan函数的具体用法?Python is_nan怎么用?Python is_nan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_nan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cut
def cut(self, hits, start, end):
"""
Cuts [start:end] diapason from input data
:param hits: hits timeseries
:param start: start index
:param end: end index
:return: tuple (train_hits, test_hits, dow, lagged_hits)
"""
# Pad hits to ensure we have enough array length for prediction
hits = tf.concat([hits, tf.fill([self.predict_window], np.NaN)], axis=0)
cropped_hit = hits[start:end]
# cut day of week
cropped_dow = self.inp.dow[start:end]
# Cut lagged hits
# gather() accepts only int32 indexes
cropped_lags = tf.cast(self.inp.lagged_ix[start:end], tf.int32)
# Mask for -1 (no data) lag indexes
lag_mask = cropped_lags < 0
# Convert -1 to 0 for gather(), it don't accept anything exotic
cropped_lags = tf.maximum(cropped_lags, 0)
# Translate lag indexes to hit values
lagged_hit = tf.gather(hits, cropped_lags)
# Convert masked (see above) or NaN lagged hits to zeros
lag_zeros = tf.zeros_like(lagged_hit)
lagged_hit = tf.where(lag_mask | tf.is_nan(lagged_hit), lag_zeros, lagged_hit)
# Split for train and test
x_hits, y_hits = tf.split(cropped_hit, [self.train_window, self.predict_window], axis=0)
# Convert NaN to zero in for train data
x_hits = tf.where(tf.is_nan(x_hits), tf.zeros_like(x_hits), x_hits)
return x_hits, y_hits, cropped_dow, lagged_hit
示例2: make_losses
def make_losses(self, pred_relevant, targets_preprocessed, objective_indices, objective_coeffs):
# make a loss function and compute some summary numbers
per_target_loss = my_ops.mse_ignore_nans(pred_relevant, targets_preprocessed, reduction_indices=0)
loss = tf.reduce_sum(per_target_loss)
# compute objective value, just for logging purposes
# TODO add multiplication by the objective_coeffs (somehow not trivial)
obj = tf.reduce_sum(self.postprocess_predictions(targets_preprocessed), 1)
#obj = tf.sum(self.postprocess_predictions(targets_preprocessed[:,objective_indices]) * objective_coeffs[None,:], axis=1)
obj_nonan = tf.where(tf.is_nan(obj), tf.zeros_like(obj), obj)
num_valid_targets = tf.reduce_sum(1-tf.cast(tf.is_nan(obj), tf.float32))
mean_obj = tf.reduce_sum(obj_nonan) / num_valid_targets
# summaries
obj_sum = tf.summary.scalar("objective_todo", mean_obj)
#TODO
per_target_loss_sums = []
#per_target_loss_sums = [tf.summary.scalar(name, loss) for name,loss in zip(self.target_names,per_target_loss)]
loss_sum = tf.summary.scalar("full loss", loss)
#self.per_target_loss = tf.get_variable('avg_targets', [self.target_dim], initializer=tf.constant_initializer(value=0.))
full_loss = loss
errs_to_print = [loss]
short_summary = [loss_sum]
detailed_summary = per_target_loss_sums + [obj_sum]
return full_loss, errs_to_print, short_summary, detailed_summary
示例3: testUniformNans
def testUniformNans(self):
a = 10.0
b = [11.0, 100.0]
uniform = uniform_lib.Uniform(low=a, high=b)
no_nans = tf.constant(1.0)
nans = tf.constant(0.0) / tf.constant(0.0)
self.assertTrue(self.evaluate(tf.is_nan(nans)))
with_nans = tf.stack([no_nans, nans])
pdf = uniform.prob(with_nans)
is_nan = self.evaluate(tf.is_nan(pdf))
self.assertFalse(is_nan[0])
self.assertTrue(is_nan[1])
示例4: filter_groundtruth_with_nan_box_coordinates
def filter_groundtruth_with_nan_box_coordinates(tensor_dict):
"""Filters out groundtruth with no bounding boxes.
Args:
tensor_dict: a dictionary of following groundtruth tensors -
fields.InputDataFields.groundtruth_boxes
fields.InputDataFields.groundtruth_classes
fields.InputDataFields.groundtruth_confidences
fields.InputDataFields.groundtruth_keypoints
fields.InputDataFields.groundtruth_instance_masks
fields.InputDataFields.groundtruth_is_crowd
fields.InputDataFields.groundtruth_area
fields.InputDataFields.groundtruth_label_types
Returns:
a dictionary of tensors containing only the groundtruth that have bounding
boxes.
"""
groundtruth_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes]
nan_indicator_vector = tf.greater(tf.reduce_sum(tf.to_int32(
tf.is_nan(groundtruth_boxes)), reduction_indices=[1]), 0)
valid_indicator_vector = tf.logical_not(nan_indicator_vector)
valid_indices = tf.where(valid_indicator_vector)
return retain_groundtruth(tensor_dict, valid_indices)
示例5: NLL
def NLL(self, y, lengths, pis, mus, sigmas, rho, es, eps=1e-8):
sigma_1, sigma_2 = tf.split(sigmas, 2, axis=2)
y_1, y_2, y_3 = tf.split(y, 3, axis=2)
mu_1, mu_2 = tf.split(mus, 2, axis=2)
norm = 1.0 / (2*np.pi*sigma_1*sigma_2 * tf.sqrt(1 - tf.square(rho)))
Z = tf.square((y_1 - mu_1) / (sigma_1)) + \
tf.square((y_2 - mu_2) / (sigma_2)) - \
2*rho*(y_1 - mu_1)*(y_2 - mu_2) / (sigma_1*sigma_2)
exp = -1.0*Z / (2*(1 - tf.square(rho)))
gaussian_likelihoods = tf.exp(exp) * norm
gmm_likelihood = tf.reduce_sum(pis * gaussian_likelihoods, 2)
gmm_likelihood = tf.clip_by_value(gmm_likelihood, eps, np.inf)
bernoulli_likelihood = tf.squeeze(tf.where(tf.equal(tf.ones_like(y_3), y_3), es, 1 - es))
nll = -(tf.log(gmm_likelihood) + tf.log(bernoulli_likelihood))
sequence_mask = tf.logical_and(
tf.sequence_mask(lengths, maxlen=tf.shape(y)[1]),
tf.logical_not(tf.is_nan(nll)),
)
nll = tf.where(sequence_mask, nll, tf.zeros_like(nll))
num_valid = tf.reduce_sum(tf.cast(sequence_mask, tf.float32), axis=1)
sequence_loss = tf.reduce_sum(nll, axis=1) / tf.maximum(num_valid, 1.0)
element_loss = tf.reduce_sum(nll) / tf.maximum(tf.reduce_sum(num_valid), 1.0)
return sequence_loss, element_loss
示例6: testUniformNans
def testUniformNans(self):
with self.test_session():
a = 10.0
b = [11.0, 100.0]
uniform = tf.contrib.distributions.Uniform(a=a, b=b)
no_nans = tf.constant(1.0)
nans = tf.constant(0.0) / tf.constant(0.0)
self.assertTrue(tf.is_nan(nans).eval())
with_nans = tf.pack([no_nans, nans])
pdf = uniform.pdf(with_nans)
is_nan = tf.is_nan(pdf).eval()
self.assertFalse(is_nan[0])
self.assertTrue(is_nan[1])
示例7: __call__
def __call__(self,
prediction_tensor,
target_tensor,
ignore_nan_targets=False,
scope=None,
**params):
"""Call the loss function.
Args:
prediction_tensor: an N-d tensor of shape [batch, anchors, ...]
representing predicted quantities.
target_tensor: an N-d tensor of shape [batch, anchors, ...] representing
regression or classification targets.
ignore_nan_targets: whether to ignore nan targets in the loss computation.
E.g. can be used if the target tensor is missing groundtruth data that
shouldn't be factored into the loss.
scope: Op scope name. Defaults to 'Loss' if None.
**params: Additional keyword arguments for specific implementations of
the Loss.
Returns:
loss: a tensor representing the value of the loss function.
"""
with tf.name_scope(scope, 'Loss',
[prediction_tensor, target_tensor, params]) as scope:
if ignore_nan_targets:
target_tensor = tf.where(tf.is_nan(target_tensor),
prediction_tensor,
target_tensor)
return self._compute_loss(prediction_tensor, target_tensor, **params)
示例8: kl_divergence
def kl_divergence(distribution_a, distribution_b,
allow_nan_stats=True, name=None):
"""Get the KL-divergence KL(distribution_a || distribution_b).
If there is no KL method registered specifically for `type(distribution_a)`
and `type(distribution_b)`, then the class hierarchies of these types are
searched.
If one KL method is registered between any pairs of classes in these two
parent hierarchies, it is used.
If more than one such registered method exists, the method whose registered
classes have the shortest sum MRO paths to the input types is used.
If more than one such shortest path exists, the first method
identified in the search is used (favoring a shorter MRO distance to
`type(distribution_a)`).
Args:
distribution_a: The first distribution.
distribution_b: The second distribution.
allow_nan_stats: Python `bool`, default `True`. When `True`,
statistics (e.g., mean, mode, variance) use the value "`NaN`" to
indicate the result is undefined. When `False`, an exception is raised
if one or more of the statistic's batch members are undefined.
name: Python `str` name prefixed to Ops created by this class.
Returns:
A Tensor with the batchwise KL-divergence between `distribution_a`
and `distribution_b`.
Raises:
NotImplementedError: If no KL method is defined for distribution types
of `distribution_a` and `distribution_b`.
"""
kl_fn = _registered_kl(type(distribution_a), type(distribution_b))
if kl_fn is None:
# TODO(b/117098119): For backwards compatibility, we check TF's registry as
# well. This typically happens when this function is called on a pair of
# TF's distributions.
with deprecation.silence():
return tf.distributions.kl_divergence(distribution_a, distribution_b)
with tf.name_scope("KullbackLeibler"):
kl_t = kl_fn(distribution_a, distribution_b, name=name)
if allow_nan_stats:
return kl_t
# Check KL for NaNs
kl_t = tf.identity(kl_t, name="kl")
with tf.control_dependencies([
tf.Assert(
tf.logical_not(
tf.reduce_any(tf.is_nan(kl_t))),
["KL calculation between %s and %s returned NaN values "
"(and was called with allow_nan_stats=False). Values:"
% (distribution_a.name, distribution_b.name), kl_t])]):
return tf.identity(kl_t, name="checked_kl")
示例9: scale
def scale(self, x):
"""Scale x from -0.5 - 0.5 to 0 - 255."""
x = tf.where(tf.is_nan(x), tf.ones_like(x), x)
x = tf.where(tf.is_inf(x), tf.ones_like(x), x)
x = tf.clip_by_value(x, -0.5, 0.5)
x += 0.5
x = x * 2**self.hparams.n_bits_x
return tf.cast(tf.clip_by_value(x, 0, 255), dtype=tf.uint8)
示例10: _build_mu_algorithm
def _build_mu_algorithm(self):
"""build dataflow graph for Multiplicative algorithm"""
V, H, W = self.V, self.H, self.W
rank = self.rank
shape = V.get_shape()
graph = tf.get_default_graph()
#save W for calculating delta with the updated W
W_old = tf.get_variable(name="W_old", shape=[shape[0], rank])
save_W = W_old.assign(W)
#Multiplicative updates
with graph.control_dependencies([save_W]):
#update operation for H
Wt = tf.transpose(W)
WV = tf.matmul(Wt, V)
WWH = tf.matmul(tf.matmul(Wt, W), H)
WV_WWH = WV / WWH
#select op should be executed in CPU not in GPU
with tf.device('/cpu:0'):
#convert nan to zero
WV_WWH = tf.select(tf.is_nan(WV_WWH),
tf.zeros_like(WV_WWH),
WV_WWH)
H_new = H * WV_WWH
update_H = H.assign(H_new)
with graph.control_dependencies([save_W, update_H]):
#update operation for W (after updating H)
Ht = tf.transpose(H)
VH = tf.matmul(V, Ht)
WHH = tf.matmul(W, tf.matmul(H, Ht))
VH_WHH = VH / WHH
with tf.device('/cpu:0'):
VH_WHH = tf.select(tf.is_nan(VH_WHH),
tf.zeros_like(VH_WHH),
VH_WHH)
W_new = W * VH_WHH
update_W = W.assign(W_new)
self.delta = tf.reduce_sum(tf.abs(W_old - W))
self.step = tf.group(save_W, update_H, update_W)
示例11: replace_nan_groundtruth_label_scores_with_ones
def replace_nan_groundtruth_label_scores_with_ones(label_scores):
"""Replaces nan label scores with 1.0.
Args:
label_scores: a tensor containing object annoation label scores.
Returns:
a tensor where NaN label scores have been replaced by ones.
"""
return tf.where(
tf.is_nan(label_scores), tf.ones(tf.shape(label_scores)), label_scores)
示例12: _compare
def _compare(self, x, use_gpu):
np_finite, np_inf, np_nan = np.isfinite(x), np.isinf(x), np.isnan(x)
with self.test_session(use_gpu=use_gpu) as sess:
inx = tf.convert_to_tensor(x)
ofinite, oinf, onan = tf.is_finite(inx), tf.is_inf(inx), tf.is_nan(inx)
tf_finite, tf_inf, tf_nan = sess.run([ofinite, oinf, onan])
self.assertAllEqual(np_inf, tf_inf)
self.assertAllEqual(np_nan, tf_nan)
self.assertAllEqual(np_finite, tf_finite)
self.assertShapeEqual(np_inf, oinf)
self.assertShapeEqual(np_nan, onan)
self.assertShapeEqual(np_finite, ofinite)
示例13: set_zero_on_high_global_norm
def set_zero_on_high_global_norm(self, grad, grad_norm_threshold, global_norm_tag=None):
"""
:param tf.Tensor grad:
:param float grad_norm_threshold:
:param str|None global_norm_tag:
:rtype: tf.Tensor
"""
norm = self.get_global_grad_norm(tag=global_norm_tag)
# Also check nan/inf. Treat them as if we would have been over grad_norm_threshold.
zero_cond = tf.logical_or(tf.is_nan(norm), tf.is_inf(norm))
zero_cond = tf.logical_or(zero_cond, tf.greater(norm, grad_norm_threshold))
return tf.where(zero_cond, tf.zeros_like(grad), grad)
示例14: _prob
def _prob(self, x):
broadcasted_x = x * tf.ones(
self.batch_shape_tensor(), dtype=x.dtype)
return tf.where(
tf.is_nan(broadcasted_x),
broadcasted_x,
tf.where(
tf.logical_or(broadcasted_x < self.low,
# This > is only sound for continuous uniform
broadcasted_x > self.high),
tf.zeros_like(broadcasted_x),
tf.ones_like(broadcasted_x) / self.range()))
示例15: _get_cubic_root
def _get_cubic_root(self):
"""Get the cubic root."""
# We have the equation x^2 D^2 + (1-x)^4 * C / h_min^2
# where x = sqrt(mu).
# We substitute x, which is sqrt(mu), with x = y + 1.
# It gives y^3 + py = q
# where p = (D^2 h_min^2)/(2*C) and q = -p.
# We use the Vieta's substitution to compute the root.
# There is only one real solution y (which is in [0, 1] ).
# http://mathworld.wolfram.com/VietasSubstitution.html
assert_array = [
tf.Assert(
tf.logical_not(tf.is_nan(self._dist_to_opt_avg)),
[self._dist_to_opt_avg,]),
tf.Assert(
tf.logical_not(tf.is_nan(self._h_min)),
[self._h_min,]),
tf.Assert(
tf.logical_not(tf.is_nan(self._grad_var)),
[self._grad_var,]),
tf.Assert(
tf.logical_not(tf.is_inf(self._dist_to_opt_avg)),
[self._dist_to_opt_avg,]),
tf.Assert(
tf.logical_not(tf.is_inf(self._h_min)),
[self._h_min,]),
tf.Assert(
tf.logical_not(tf.is_inf(self._grad_var)),
[self._grad_var,])
]
with tf.control_dependencies(assert_array):
p = self._dist_to_opt_avg**2 * self._h_min**2 / 2 / self._grad_var
w3 = (-tf.sqrt(p**2 + 4.0 / 27.0 * p**3) - p) / 2.0
w = tf.sign(w3) * tf.pow(tf.abs(w3), 1.0/3.0)
y = w - p / 3.0 / w
x = y + 1
return x