本文整理汇总了Python中tensorflow.is_nan函数的典型用法代码示例。如果您正苦于以下问题:Python is_nan函数的具体用法?Python is_nan怎么用?Python is_nan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_nan函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:JXieHao,项目名称:kaggle-web-traffic,代码行数:34,代码来源:input_pipe.py
示例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
开发者ID:johny-c,项目名称:DirectFuturePrediction,代码行数:29,代码来源:future_predictor_agent_advantage.py
示例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])
开发者ID:asudomoeva,项目名称:probability,代码行数:15,代码来源:uniform_test.py
示例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)
开发者ID:zhangjiulong,项目名称:models,代码行数:25,代码来源:ops.py
示例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
开发者ID:animebing,项目名称:handwriting-synthesis,代码行数:28,代码来源:rnn.py
示例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])
开发者ID:2020zyc,项目名称:tensorflow,代码行数:16,代码来源:uniform_test.py
示例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)
开发者ID:ALISCIFP,项目名称:models,代码行数:30,代码来源:losses.py
示例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")
开发者ID:asudomoeva,项目名称:probability,代码行数:59,代码来源:kullback_leibler.py
示例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)
开发者ID:qixiuai,项目名称:tensor2tensor,代码行数:8,代码来源:glow.py
示例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)
开发者ID:katbailey,项目名称:tffactorization,代码行数:45,代码来源:tfnmf.py
示例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)
开发者ID:NoPointExc,项目名称:models,代码行数:11,代码来源:ops.py
示例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)
开发者ID:peace195,项目名称:tensorflow,代码行数:12,代码来源:cwise_ops_test.py
示例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)
开发者ID:rwth-i6,项目名称:returnn,代码行数:12,代码来源:TFUpdater.py
示例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()))
开发者ID:asudomoeva,项目名称:probability,代码行数:12,代码来源:uniform.py
示例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
开发者ID:qixiuai,项目名称:tensor2tensor,代码行数:37,代码来源:yellowfin.py
示例16: __call__
def __call__(self,
prediction_tensor,
target_tensor,
ignore_nan_targets=False,
losses_mask=None,
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.
losses_mask: A [batch] boolean tensor that indicates whether losses should
be applied to individual images in the batch. For elements that
are True, corresponding prediction, target, and weight tensors will be
removed prior to loss computation. If None, no filtering will take place
prior to loss computation.
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)
if losses_mask is not None:
tensor_multiplier = self._get_loss_multiplier_for_tensor(
prediction_tensor,
losses_mask)
prediction_tensor *= tensor_multiplier
target_tensor *= tensor_multiplier
if 'weights' in params:
params['weights'] = tf.convert_to_tensor(params['weights'])
weights_multiplier = self._get_loss_multiplier_for_tensor(
params['weights'],
losses_mask)
params['weights'] *= weights_multiplier
return self._compute_loss(prediction_tensor, target_tensor, **params)
开发者ID:ahmedtalbi,项目名称:models,代码行数:49,代码来源:losses.py
示例17: __init__
def __init__(self, batch_size, vocab_size, encoding_size, embedding_size,
num_glimpses = 8,
grad_norm_clip = 5.,
l2_reg_coef=1e-4,
session=tf.Session(),
name='AlternatingAttention'):
"""
Creates an iterative alternating attention network as described in https://arxiv.org/abs/1606.02245
"""
self._batch_size = batch_size
self._vocab_size = vocab_size
self._encode_size = encoding_size
self._infer_size = 4 * encoding_size
self._embedding_size = embedding_size
self._num_glimpses = num_glimpses
self._sess = session
self._name = name
self._build_placeholders()
self._build_variables()
# Regularization
tf.contrib.layers.apply_regularization(tf.contrib.layers.l2_regularizer(l2_reg_coef), [self._embeddings])
# Answer probability
doc_attentions = self._inference(self._docs, self._queries)
nans = tf.reduce_sum(tf.to_float(tf.is_nan(doc_attentions)))
self._doc_attentions = doc_attentions
ans_mask = tf.to_float(tf.equal(tf.expand_dims(self._answers, -1), self._docs))
P_a = tf.reduce_sum(ans_mask * doc_attentions, 1)
loss_op = -tf.reduce_mean(tf.log(P_a + tf.constant(0.00001)))
self._loss_op = loss_op
# Optimizer and gradients
with tf.name_scope("optimizer"):
self._opt = tf.train.AdamOptimizer(learning_rate=self._learning_rate)
grads_and_vars = self._opt.compute_gradients(loss_op)
capped_grads_and_vars = [(tf.clip_by_norm(g, grad_norm_clip), v) for g,v in grads_and_vars]
self._train_op = self._opt.apply_gradients(capped_grads_and_vars, global_step=self._global_step)
tf.summary.scalar('loss', self._loss_op)
tf.summary.scalar('learning_rate', self._learning_rate)
tf.summary.histogram('answer_probability', P_a)
self._summary_op = tf.summary.merge_all()
self._sess.run(tf.global_variables_initializer())
开发者ID:MrCrumpets,项目名称:alternating-reader-tf,代码行数:48,代码来源:AlternatingAttention.py
示例18: check_grads
def check_grads(grads_and_vars):
has_nan_ops = []
amax_ops = []
for grad, _ in grads_and_vars:
if grad is not None:
if isinstance(grad, tf.IndexedSlices):
x = grad.values
else:
x = grad
has_nan_ops.append(tf.reduce_any(tf.is_nan(x)))
amax_ops.append(tf.reduce_max(tf.abs(x)))
has_nan = tf.reduce_any(has_nan_ops)
amax = tf.reduce_max(amax_ops)
return has_nan, amax
开发者ID:fotwo,项目名称:OpenSeq2Seq,代码行数:17,代码来源:automatic_loss_scaler.py
示例19: def_preprocessing_fn
def def_preprocessing_fn(inputs):
"""tf.transform's callback function for preprocessing inputs.
Args:
inputs: map from feature keys to raw not-yet-transformed features.
Returns:
Map from string feature key to transformed feature operations.
"""
outputs = {}
for key in taxi.DENSE_FLOAT_FEATURE_KEYS:
# Preserve this feature as a dense float, setting nan's to the mean.
outputs[taxi.transformed_name(key)] = transform.scale_to_z_score(
_fill_in_missing(inputs[key]))
for key in taxi.VOCAB_FEATURE_KEYS:
# Build a vocabulary for this feature.
outputs[
taxi.transformed_name(key)] = transform.compute_and_apply_vocabulary(
_fill_in_missing(inputs[key]),
top_k=taxi.VOCAB_SIZE,
num_oov_buckets=taxi.OOV_SIZE)
for key in taxi.BUCKET_FEATURE_KEYS:
outputs[taxi.transformed_name(key)] = transform.bucketize(
_fill_in_missing(inputs[key]), taxi.FEATURE_BUCKET_COUNT)
for key in taxi.CATEGORICAL_FEATURE_KEYS:
outputs[taxi.transformed_name(key)] = _fill_in_missing(inputs[key])
# Was this passenger a big tipper?
taxi_fare = _fill_in_missing(inputs[taxi.FARE_KEY])
tips = _fill_in_missing(inputs[taxi.LABEL_KEY])
outputs[taxi.transformed_name(taxi.LABEL_KEY)] = tf.where(
tf.is_nan(taxi_fare),
tf.cast(tf.zeros_like(taxi_fare), tf.int64),
# Test if the tip was > 20% of the fare.
tf.cast(
tf.greater(tips, tf.multiply(taxi_fare, tf.constant(0.2))),
tf.int64))
return outputs
开发者ID:devmvrborges,项目名称:code-snippets,代码行数:42,代码来源:taxi_preprocess_bq.py
示例20: _arccosine
def _arccosine(self, slist1, slist2, tf_embs):
"""
Uses an arccosine kernel of degree 0 to calculate
the similarity matrix between two vectors of embeddings.
This is just cosine similarity projected into the [0,1] interval.
"""
dot = self._dot(slist1, slist2, tf_embs)
# This calculation corresponds to an arc-cosine with
# degree 0. It can be interpreted as cosine
# similarity but projected into a [0,1] interval.
# TODO: arc-cosine with degree 1.
tf_pi = tf.constant(np.pi, dtype=tf.float64)
tf_norms = tf.constant(self.norms, dtype=tf.float64, name='norms')
normlist1 = tf.gather(tf_norms, slist1, name='normlist1')
normlist2 = tf.matrix_transpose(tf.gather(tf_norms, slist2, name='normlist2'))
norms = tf.batch_matmul(normlist1, normlist2)
cosine = tf.clip_by_value(tf.truediv(dot, norms), -1, 1)
angle = tf.acos(cosine)
angle = tf.select(tf.is_nan(angle), tf.ones_like(angle) * tf_pi, angle)
return 1 - (angle / tf_pi)
开发者ID:beckdaniel,项目名称:flakes,代码行数:20,代码来源:sk_tf_batch.py
注:本文中的tensorflow.is_nan函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论