當前位置: 首頁>>代碼示例>>Python>>正文


Python v1.ones_like方法代碼示例

本文整理匯總了Python中tensorflow.compat.v1.ones_like方法的典型用法代碼示例。如果您正苦於以下問題:Python v1.ones_like方法的具體用法?Python v1.ones_like怎麽用?Python v1.ones_like使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.compat.v1的用法示例。


在下文中一共展示了v1.ones_like方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testTwoClassLogLikelihoodVersusOldImplementation

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def testTwoClassLogLikelihoodVersusOldImplementation(self):
    def alt_two_class_log_likelihood_impl(predictions, labels):
      float_labels = tf.cast(labels, dtype=tf.float64)
      float_predictions = tf.cast(tf.squeeze(predictions), dtype=tf.float64)
      # likelihood should be just p for class 1, and 1 - p for class 0.
      # signs is 1 for class 1, and -1 for class 0
      signs = 2 * float_labels - tf.ones_like(float_labels)
      # constant_term is 1 for class 0, and 0 for class 1.
      constant_term = tf.ones_like(float_labels) - float_labels
      likelihoods = constant_term + signs * float_predictions
      log_likelihoods = tf.log(likelihoods)
      avg_log_likelihood = tf.reduce_mean(log_likelihoods)
      return avg_log_likelihood
    predictions = np.random.rand(1, 10, 1)
    targets = np.random.randint(2, size=10)
    with self.test_session() as session:
      new_log_likelihood, _ = metrics.two_class_log_likelihood(
          predictions, targets)
      alt_log_likelihood = alt_two_class_log_likelihood_impl(
          predictions, targets)
      new_impl, alt_impl = session.run([new_log_likelihood, alt_log_likelihood])
    self.assertAlmostEqual(new_impl, alt_impl) 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:24,代碼來源:metrics_test.py

示例2: rank_loss

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def rank_loss(sentence_emb, image_emb, margin=0.2):
  """Experimental rank loss, thanks to kkurach@ for the code."""
  with tf.name_scope("rank_loss"):
    # Normalize first as this is assumed in cosine similarity later.
    sentence_emb = tf.nn.l2_normalize(sentence_emb, 1)
    image_emb = tf.nn.l2_normalize(image_emb, 1)
    # Both sentence_emb and image_emb have size [batch, depth].
    scores = tf.matmul(image_emb, tf.transpose(sentence_emb))  # [batch, batch]
    diagonal = tf.diag_part(scores)  # [batch]
    cost_s = tf.maximum(0.0, margin - diagonal + scores)  # [batch, batch]
    cost_im = tf.maximum(
        0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores)  # [batch, batch]
    # Clear diagonals.
    batch_size = tf.shape(sentence_emb)[0]
    empty_diagonal_mat = tf.ones_like(cost_s) - tf.eye(batch_size)
    cost_s *= empty_diagonal_mat
    cost_im *= empty_diagonal_mat
    return tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im) 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:20,代碼來源:slicenet.py

示例3: _select_top_k

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def _select_top_k(logits, top_k):
  """Replaces logits, expect the top k highest values, with small number (-1e6).

  If k is -1 don't replace anything.

  Args:
    logits: A `Tensor` of shape [batch_size, ..., vocab_size]
    top_k: vector of batch size.

  Returns:
    A `Tensor` with same shape  as logits.
  """
  vocab_size = logits.shape[-1]

  top_k = tf.where(
      tf.not_equal(top_k, -1), top_k,
      tf.ones_like(top_k) * vocab_size)

  return tf.where(
      tf.argsort(logits) < tf.reshape(top_k, [-1] + [1] *
                                      (len(logits.shape) - 1)), logits,
      tf.ones_like(logits) * -1e6) 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:24,代碼來源:common_layers.py

示例4: _compute_auxiliary_structure

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def _compute_auxiliary_structure(self, contents_and_mask):
    """Compute segment and position metadata."""
    contents = contents_and_mask[:, :self._num_sequences]
    start_mask = tf.cast(contents_and_mask[:, self._num_sequences:],
                         dtype=INDEX_DTYPE)

    segment = tf.cumsum(start_mask, axis=0)
    uniform_count = tf.ones_like(segment[:, 0])
    position = []
    for i in range(self._num_sequences):
      segment_slice = segment[:, i]
      counts = tf.math.segment_sum(uniform_count, segment[:, i])
      position.append(tf.range(self._packed_length) -  tf.cumsum(
          tf.gather(counts, segment_slice - 1) * start_mask[:, i]))
    position = tf.concat([i[:, tf.newaxis] for i in position], axis=1)

    # Correct for padding tokens.
    pad_mask = tf.cast(tf.not_equal(contents, 0), dtype=INDEX_DTYPE)
    segment *= pad_mask
    position *= pad_mask

    return segment, position 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:24,代碼來源:generator_utils.py

示例5: _finalize

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def _finalize(self, _, contents):
    """Structure output and compute segment and position metadata."""

    # The output shape information is lost during the filter; however we can
    # guarantee the shape. (That's the point of this exercise, after all!)
    contents.set_shape((self._packed_length, self._num_sequences * 2))

    # Both the dummy branch of the scan step function and the eviction dataset
    # use vectors of minus one. The cost of this check is negligible and the
    # leakage of such dummy sequences would be difficult to debug downstream.
    check_leaks = tf.assert_none_equal(contents, -tf.ones_like(contents))
    with tf.control_dependencies([check_leaks]):
      contents = tf.identity(contents)

    segment, position = self._compute_auxiliary_structure(contents)
    return {"contents": contents[:, :self._num_sequences],
            "segment": segment, "position": position} 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:19,代碼來源:generator_utils.py

示例6: unwrap

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def unwrap(p, discont=np.pi, axis=-1):
  """Unwrap a cyclical phase tensor.

  Args:
    p: Phase tensor.
    discont: Float, size of the cyclic discontinuity.
    axis: Axis of which to unwrap.

  Returns:
    unwrapped: Unwrapped tensor of same size as input.
  """
  dd = diff(p, axis=axis)
  ddmod = tf.mod(dd + np.pi, 2.0 * np.pi) - np.pi
  idx = tf.logical_and(tf.equal(ddmod, -np.pi), tf.greater(dd, 0))
  ddmod = tf.where(idx, tf.ones_like(ddmod) * np.pi, ddmod)
  ph_correct = ddmod - dd
  idx = tf.less(tf.abs(dd), discont)
  ddmod = tf.where(idx, tf.zeros_like(ddmod), dd)
  ph_cumsum = tf.cumsum(ph_correct, axis=axis)

  shape = p.get_shape().as_list()
  shape[axis] = 1
  ph_cumsum = tf.concat([tf.zeros(shape, dtype=p.dtype), ph_cumsum], axis=axis)
  unwrapped = p + ph_cumsum
  return unwrapped 
開發者ID:magenta,項目名稱:magenta,代碼行數:27,代碼來源:spectral_ops.py

示例7: apply_piecewise_monotonic_fn

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def apply_piecewise_monotonic_fn(self, wrapper, fn, boundaries, *args):
    valid_values = []
    for a in [self] + list(args):
      vs = []
      vs.append(a.lower)
      vs.append(a.upper)
      for b in boundaries:
        vs.append(
            tf.maximum(a.lower, tf.minimum(a.upper, b * tf.ones_like(a.lower))))
      valid_values.append(vs)
    outputs = []
    for inputs in itertools.product(*valid_values):
      outputs.append(fn(*inputs))
    outputs = tf.stack(outputs, axis=-1)
    return IntervalBounds(tf.reduce_min(outputs, axis=-1),
                          tf.reduce_max(outputs, axis=-1)) 
開發者ID:deepmind,項目名稱:interval-bound-propagation,代碼行數:18,代碼來源:bounds.py

示例8: filter_correct_class

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def filter_correct_class(verifiable_obj, num_classes, labels, margin):
  """Filters out the objective when the target class contains the true label.

  Args:
    verifiable_obj: 2D tensor of shape (num_classes, batch_size) containing
      verifiable objectives.
    num_classes: number of target classes.
    labels: 1D tensor of shape (batch_size) containing the labels for each
      example in the batch.
    margin: Verifiable objective values for correct class will be forced to
      `-margin`, thus disregarding large negative bounds when maximising.

  Returns:
   2D tensor of shape (num_classes, batch_size) containing the corrected
   verifiable objective values for each (class, example).
  """
  targets_to_filter = tf.expand_dims(
      tf.range(num_classes, dtype=labels.dtype), axis=1)
  neq = tf.not_equal(targets_to_filter, labels)
  verifiable_obj = tf.where(neq, verifiable_obj, -margin *
                            tf.ones_like(verifiable_obj))
  return verifiable_obj 
開發者ID:deepmind,項目名稱:interval-bound-propagation,代碼行數:24,代碼來源:robust_model.py

示例9: test_gaussian_mixture_approximate_mode

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def test_gaussian_mixture_approximate_mode(self):
    sample_size = 10
    num_alphas = 5
    # Manually set alphas to 1 in zero-th column and 0 elsewhere, making the
    # first component the most likely.
    alphas = tf.one_hot(2 * [0], num_alphas)
    mus = tf.random.normal((2, num_alphas, sample_size))
    sigmas = tf.ones_like(mus)
    mix_dist = tfp.distributions.Categorical(logits=alphas)
    comp_dist = tfp.distributions.MultivariateNormalDiag(
        loc=mus, scale_diag=sigmas)
    gm = tfp.distributions.MixtureSameFamily(
        mixture_distribution=mix_dist, components_distribution=comp_dist)
    approximate_mode = mdn.gaussian_mixture_approximate_mode(gm)
    with self.test_session() as sess:
      approximate_mode_np, mus_np = sess.run([approximate_mode, mus])
      # The approximate mode should be the mean of the zero-th (most likely)
      # component.
      self.assertAllClose(approximate_mode_np, mus_np[:, 0, :]) 
開發者ID:google-research,項目名稱:tensor2robot,代碼行數:21,代碼來源:mdn_test.py

示例10: CausallyMaskedSoftmax

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def CausallyMaskedSoftmax(x):
  """Causally masked Softmax. Zero out probabilities before and after norm.

  pre-softmax logits are masked by setting upper diagonal to -inf:

  |a  0, 0|    |0, -inf, -inf|
  |b, d, 0|  + |0,   0,  -inf|
  |c, e, f|    |0,   0,    0 |

  Args:
    x: Batched tensor of shape [batch_size, T, T].
  Returns:
    Softmax where each row corresponds to softmax vector for each query.
  """
  lower_diag = tf.linalg.band_part(x, -1, 0)
  upper_diag = -np.inf * tf.ones_like(x)
  upper_diag = tf.linalg.band_part(upper_diag, 0, -1)
  upper_diag = tf.linalg.set_diag(
      upper_diag, tf.zeros_like(tf.linalg.diag_part(x)))
  x = lower_diag + upper_diag
  softmax = tf.nn.softmax(x)
  return tf.linalg.band_part(softmax, -1, 0) 
開發者ID:google-research,項目名稱:tensor2robot,代碼行數:24,代碼來源:snail.py

示例11: top_k_logits

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def top_k_logits(logits, k):
    if k == 0:
        # no truncation
        return logits

    def _top_k():
        values, _ = tf.nn.top_k(logits, k=k)
        min_values = values[:, -1, tf.newaxis]
        return tf.where(
            logits < min_values,
            tf.ones_like(logits, dtype=logits.dtype) * -1e10,
            logits,
        )
    return tf.cond(
        tf.equal(k, 0),
        lambda: logits,
        lambda: _top_k(),
    ) 
開發者ID:re-search,項目名稱:gpt2-estimator,代碼行數:20,代碼來源:sample.py

示例12: _test_upper_bound

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def _test_upper_bound(self, gradient):
    inputs = tf.placeholder(dtype=tf.float32)
    outputs = math_ops.upper_bound(inputs, 0, gradient=gradient)
    pgrads, = tf.gradients([outputs], [inputs], [tf.ones_like(inputs)])
    ngrads, = tf.gradients([outputs], [inputs], [-tf.ones_like(inputs)])

    inputs_feed = [-1, 1]
    outputs_expected = [-1, 0]
    if gradient == "disconnected":
      pgrads_expected = [1, 0]
      ngrads_expected = [-1, 0]
    elif gradient == "identity":
      pgrads_expected = [1, 1]
      ngrads_expected = [-1, -1]
    else:
      pgrads_expected = [1, 1]
      ngrads_expected = [-1, 0]

    with self.cached_session() as sess:
      outputs, pgrads, ngrads = sess.run(
          [outputs, pgrads, ngrads], {inputs: inputs_feed})
      self.assertAllEqual(outputs, outputs_expected)
      self.assertAllEqual(pgrads, pgrads_expected)
      self.assertAllEqual(ngrads, ngrads_expected) 
開發者ID:tensorflow,項目名稱:compression,代碼行數:26,代碼來源:math_ops_test.py

示例13: _test_lower_bound

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def _test_lower_bound(self, gradient):
    inputs = tf.placeholder(dtype=tf.float32)
    outputs = math_ops.lower_bound(inputs, 0, gradient=gradient)
    pgrads, = tf.gradients([outputs], [inputs], [tf.ones_like(inputs)])
    ngrads, = tf.gradients([outputs], [inputs], [-tf.ones_like(inputs)])

    inputs_feed = [-1, 1]
    outputs_expected = [0, 1]
    if gradient == "disconnected":
      pgrads_expected = [0, 1]
      ngrads_expected = [0, -1]
    elif gradient == "identity":
      pgrads_expected = [1, 1]
      ngrads_expected = [-1, -1]
    else:
      pgrads_expected = [0, 1]
      ngrads_expected = [-1, -1]

    with self.cached_session() as sess:
      outputs, pgrads, ngrads = sess.run(
          [outputs, pgrads, ngrads], {inputs: inputs_feed})
      self.assertAllEqual(outputs, outputs_expected)
      self.assertAllEqual(pgrads, pgrads_expected)
      self.assertAllEqual(ngrads, ngrads_expected) 
開發者ID:tensorflow,項目名稱:compression,代碼行數:26,代碼來源:math_ops_test.py

示例14: infer

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def infer(self,
            features,
            *args,
            **kwargs):
    """Produce predictions from the model."""
    del args, kwargs
    inputs_old = None
    if "inputs" in features and len(features["inputs"].shape) < 4:
      inputs_old = features["inputs"]
      features["inputs"] = tf.expand_dims(features["inputs"], 2)
    features["targets"] = tf.identity(features["inputs"])

    # logits, _ = self(features)
    t2t_model.set_custom_getter_compose(self._custom_getter)
    tf.get_variable_scope().set_initializer(
        optimize.get_variable_initializer(self.hparams))
    with self._eager_var_store.as_default():
      self._fill_problem_hparams_features(features)
      # intentionally disable sharding during inference (in multi GPU)
      with tf.variable_scope(self.name):
        logits, _, _, targets_mask = self.model_fn(features)

    samples = tf.argmax(logits, axis=-1)
    samples = tf.where(
        tf.cast(targets_mask[..., tf.newaxis, tf.newaxis], tf.bool),
        samples, tf.ones_like(samples))
    if inputs_old is not None:  # Restore to not confuse Estimator.
      features["inputs"] = inputs_old
    return samples 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:31,代碼來源:transformer_vae_flow_prior.py

示例15: postprocess

# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import ones_like [as 別名]
def postprocess(x, n_bits_x=8):
  """Converts x from [-0.5, 0.5], to [0, 255].

  Args:
    x: 3-D or 4-D Tensor normalized between [-0.5, 0.5]
    n_bits_x: Number of bits representing each pixel of the output.
              Defaults to 8, to default to 256 possible values.
  Returns:
    x: 3-D or 4-D Tensor representing images or videos.
  """
  x = tf.where(tf.is_finite(x), x, tf.ones_like(x))
  x = tf.clip_by_value(x, -0.5, 0.5)
  x += 0.5
  x = x * 2**n_bits_x
  return tf.cast(tf.clip_by_value(x, 0, 255), dtype=tf.uint8) 
開發者ID:tensorflow,項目名稱:tensor2tensor,代碼行數:17,代碼來源:glow_ops.py


注:本文中的tensorflow.compat.v1.ones_like方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。