def _sample_n(self, n, seed=None):
   n_draws = math_ops.cast(self.n, dtype=dtypes.int32)
   if self.n.get_shape().ndims is not None:
     if self.n.get_shape().ndims != 0:
       raise NotImplementedError(
           "Sample only supported for scalar number of draws.")
   elif self.validate_args:
     is_scalar = check_ops.assert_rank(
         n_draws, 0,
         message="Sample only supported for scalar number of draws.")
     n_draws = control_flow_ops.with_dependencies([is_scalar], n_draws)
   k = self.event_shape()[0]
   unnormalized_logits = array_ops.reshape(
       math_ops.log(random_ops.random_gamma(
           shape=[n],
           alpha=self.alpha,
           dtype=self.dtype,
           seed=seed)),
       shape=[-1, k])
   draws = random_ops.multinomial(
       logits=unnormalized_logits,
       num_samples=n_draws,
       seed=distribution_util.gen_new_seed(seed, salt="dirichlet_multinomial"))
   x = math_ops.reduce_sum(array_ops.one_hot(draws, depth=k),
                           reduction_indices=-2)
   final_shape = array_ops.concat([[n], self.batch_shape(), [k]], 0)
   return array_ops.reshape(x, final_shape)
  def initialize(self, name=None):
    """Initialize the decoder.

    Args:
      name: Name scope for any created operations.

    Returns:
      `(finished, start_inputs, initial_state)`.
    """
    finished, start_inputs = self._finished, self._start_inputs

    dtype = nest.flatten(self._initial_cell_state)[0].dtype
    log_probs = array_ops.one_hot(  # shape(batch_sz, beam_sz)
        array_ops.zeros([self._batch_size], dtype=dtypes.int32),
        depth=self._beam_width,
        on_value=ops.convert_to_tensor(0.0, dtype=dtype),
        off_value=ops.convert_to_tensor(-np.Inf, dtype=dtype),
        dtype=dtype)

    initial_state = BeamSearchDecoderState(
        cell_state=self._initial_cell_state,
        log_probs=log_probs,
        finished=finished,
        lengths=array_ops.zeros(
            [self._batch_size, self._beam_width], dtype=dtypes.int64))

    return (finished, start_inputs, initial_state)
Beispiel #3
0
def _sum_states(idx, states):
  """Take logsumexp for each unique state out of all label states.

  Args:
    idx: tensor of shape [batch, label_length] For each sequence, indices into a
      set of unique labels as computed by calling unique.
    states: tensor of shape [frames, batch, label_length] Log probabilities for
      each label state.

  Returns:
    tensor of shape [frames, batch_size, label_length], log probabilites summed
      for each unique label of the sequence.
  """

  with ops.name_scope("sum_states"):
    idx = ops.convert_to_tensor(idx, name="idx")
    num_states = _get_dim(states, 2)
    states = array_ops.expand_dims(states, axis=2)
    one_hot = array_ops.one_hot(
        idx,
        depth=num_states,
        on_value=0.0,
        off_value=math_ops.log(0.0),
        axis=1)
    return math_ops.reduce_logsumexp(states + one_hot, axis=-1)
Beispiel #4
0
 def _sample_n(self, n, seed=None):
   n_draws = math_ops.cast(self.total_count, dtype=dtypes.int32)
   if self.total_count.get_shape().ndims is not None:
     if self.total_count.get_shape().ndims != 0:
       raise NotImplementedError(
           "Sample only supported for scalar number of draws.")
   elif self.validate_args:
     is_scalar = check_ops.assert_rank(
         n_draws, 0,
         message="Sample only supported for scalar number of draws.")
     n_draws = control_flow_ops.with_dependencies([is_scalar], n_draws)
   k = self.event_shape_tensor()[0]
   # Flatten batch dims so logits has shape [B, k],
   # where B = reduce_prod(self.batch_shape_tensor()).
   x = random_ops.multinomial(
       logits=array_ops.reshape(self.logits, [-1, k]),
       num_samples=n * n_draws,
       seed=seed)
   x = array_ops.reshape(x, shape=[-1, n, n_draws])
   x = math_ops.reduce_sum(array_ops.one_hot(x, depth=k),
                           axis=-2)  # shape: [B, n, k]
   x = array_ops.transpose(x, perm=[1, 0, 2])
   final_shape = array_ops.concat([[n], self.batch_shape_tensor(), [k]], 0)
   x = array_ops.reshape(x, final_shape)
   return math_ops.cast(x, self.dtype)
def _mask_probs(probs, eos_token, finished):
  """Masks log probabilities.

  The result is that finished beams allocate all probability mass to eos and
  unfinished beams remain unchanged.

  Args:
    probs: Log probabiltiies of shape `[batch_size, beam_width, vocab_size]`
    eos_token: An int32 id corresponding to the EOS token to allocate
      probability to.
    finished: A boolean tensor of shape `[batch_size, beam_width]` that
      specifies which elements in the beam are finished already.

  Returns:
    A tensor of shape `[batch_size, beam_width, vocab_size]`, where unfinished
    beams stay unchanged and finished beams are replaced with a tensor with all
    probability on the EOS token.
  """
  vocab_size = array_ops.shape(probs)[2]
  # All finished examples are replaced with a vector that has all
  # probability on EOS
  finished_row = array_ops.one_hot(
      eos_token,
      vocab_size,
      dtype=probs.dtype,
      on_value=0.,
      off_value=probs.dtype.min)
  finished_probs = array_ops.tile(
      array_ops.reshape(finished_row, [1, 1, -1]),
      array_ops.concat([array_ops.shape(finished), [1]], 0))
  finished_mask = array_ops.tile(
      array_ops.expand_dims(finished, 2), [1, 1, vocab_size])

  return array_ops.where(finished_mask, finished_probs, probs)
def _mask_probs(probs, eos_token, finished):
  """Masks log probabilities.

  The result is that finished beams allocate all probability mass to eos and
  unfinished beams remain unchanged.

  Args:
    probs: Log probabiltiies of shape `[batch_size, beam_width, vocab_size]`
    eos_token: An int32 id corresponding to the EOS token to allocate
      probability to.
    finished: A boolean tensor of shape `[batch_size, beam_width]` that
      specifies which
      elements in the beam are finished already.

  Returns:
    A tensor of shape `[batch_size, beam_width, vocab_size]`, where unfinished
    beams stay unchanged and finished beams are replaced with a tensor with all
    probability on the EOS token.
  """
  vocab_size = array_ops.shape(probs)[2]
  finished_mask = array_ops.expand_dims(
      math_ops.to_float(1. - math_ops.to_float(finished)), 2)
  # These examples are not finished and we leave them
  non_finished_examples = finished_mask * probs
  # All finished examples are replaced with a vector that has all
  # probability on EOS
  finished_row = array_ops.one_hot(
      eos_token,
      vocab_size,
      dtype=dtypes.float32,
      on_value=0.,
      off_value=dtypes.float32.min)
  finished_examples = (1. - finished_mask) * finished_row
  return finished_examples + non_finished_examples
Beispiel #7
0
def _get_batch(per_class_queues, probs, batch_size):
  """Generates batches according to per-class-probabilities."""
  num_classes = probs.size
  # Number of examples per class is governed by a multinomial distribution.
  # Note: multinomial takes unnormalized log probabilities for its first
  # argument, of dimension [batch_size, num_classes].
  examples = random_ops.multinomial(
      np.expand_dims(np.log(probs), 0), batch_size)

  # Prepare the data and label batches.
  val_list = []
  label_list = []
  for i in range(num_classes):
    num_examples = math_ops.reduce_sum(
        math_ops.cast(math_ops.equal(examples, i), dtypes.int32))
    val_list.append(per_class_queues[i].dequeue_many(num_examples))
    label_list.append(array_ops.ones([num_examples], dtype=dtypes.int32) * i)

  # Create a tensor of labels.
  batch_labels = array_ops.concat(0, label_list)
  batch_labels.set_shape([batch_size])

  # Debug instrumentation.
  sample_tags = ['stratified_sample/samples_class%i' % i for i in
                 range(num_classes)]
  logging_ops.scalar_summary(sample_tags, math_ops.reduce_sum(
      array_ops.one_hot(batch_labels, num_classes), 0))

  return array_ops.concat(0, val_list), batch_labels
Beispiel #8
0
 def _sample_single(args):
   logits, n_draw = args[0], args[1]  # [K], []
   x = random_ops.multinomial(logits[array_ops.newaxis, ...], n_draw,
                              seed)  # [1, n*n_draw]
   x = array_ops.reshape(x, shape=[n, -1])  # [n, n_draw]
   x = math_ops.reduce_sum(array_ops.one_hot(x, depth=k), axis=-2)  # [n, k]
   return x
Beispiel #9
0
def _estimate_data_distribution(labels, num_classes, smoothing_constant=10):
  """Estimate data distribution as labels are seen."""
  # Variable to track running count of classes. Smooth by a nonzero value to
  # avoid division-by-zero. Higher values provide more stability at the cost of
  # slower convergence.
  if smoothing_constant <= 0:
    raise ValueError('smoothing_constant must be nonzero.')
  num_examples_per_class_seen = variables.Variable(
      initial_value=[smoothing_constant] * num_classes, trainable=False,
      name='class_count', dtype=dtypes.int64)

  # Update the class-count based on what labels are seen in batch.
  num_examples_per_class_seen = num_examples_per_class_seen.assign_add(
      math_ops.reduce_sum(array_ops.one_hot(labels, num_classes,
                                            dtype=dtypes.int64), 0))

  # Normalize count into a probability.
  # NOTE: Without the `+= 0` line below, the test
  # `testMultiThreadedEstimateDataDistribution` fails. The reason is that
  # before this line, `num_examples_per_class_seen` is a Tensor that shares a
  # buffer with an underlying `ref` object. When the `ref` is changed by another
  # thread, `num_examples_per_class_seen` changes as well. Since this can happen
  # in the middle of the normalization computation, we get probabilities that
  # are very far from summing to one. Adding `+= 0` copies the contents of the
  # tensor to a new buffer, which will be consistent from the start to the end
  # of the normalization computation.
  num_examples_per_class_seen += 0
  init_prob_estimate = math_ops.truediv(
      num_examples_per_class_seen,
      math_ops.reduce_sum(num_examples_per_class_seen))

  # Must return float32 (not float64) to agree with downstream `_verify_input`
  # checks.
  return math_ops.cast(init_prob_estimate, dtypes.float32)
Beispiel #10
0
  def _get_eval_ops(self, features, targets, metrics):
    features, _, spec = data_ops.ParseDataTensorOrDict(features)
    labels = data_ops.ParseLabelTensorOrDict(targets)
    _assert_float32(features)
    _assert_float32(labels)

    graph_builder = self.graph_builder_class(
        self.params, device_assigner=self.device_assigner, training=False,
        **self.construction_args)

    probabilities = graph_builder.inference_graph(features, data_spec=spec)

    # One-hot the labels.
    if not self.params.regression:
      labels = math_ops.to_int64(array_ops.one_hot(math_ops.to_int64(
          array_ops.squeeze(labels)), self.params.num_classes, 1, 0))

    if metrics is None:
      metrics = {self.accuracy_metric:
                 eval_metrics.get_metric(self.accuracy_metric)}

    result = {}
    for name, metric in six.iteritems(metrics):
      result[name] = metric(probabilities, labels)

    return result
Beispiel #11
0
def create_callable_acgan_model():
  return train.acgan_model(
      Generator(),
      ACGANDiscriminator(),
      real_data=array_ops.zeros([1, 2]),
      generator_inputs=random_ops.random_normal([1, 2]),
      one_hot_labels=array_ops.one_hot([0, 1, 2], 10))
Beispiel #12
0
def per_example_maxent_loss(labels, weights, logits, num_classes, eps=1e-15):
  """Maximum entropy loss for multiclass problems.

  Maximum entropy is a generalization of logistic loss for the case when more
  than 2 classes are present.

  Args:
    labels: Rank 2 (N, 1) or Rank 1 (N) tensor of per-example labels.
    weights: Rank 2 (N, 1) tensor of per-example weights.
    logits: Rank 2 (N, K) tensor of per-example predictions, K - num of
    classes.
    num_classes: number of classes in classification task. Used to expand label
    indices into one-hot encodings.
    eps: tolerance, used as a minimum possible value.

  Returns:
    loss: A Rank 2 (N, 1) tensor of per-example maxent loss
    update_op: An update operation to update the loss's internal state.
  """
  labels = math_ops.to_int64(labels)
  # If labels are of rank 1, make them rank 2.
  labels_shape = labels.get_shape()
  if len(labels_shape) != 2:
    labels = array_ops.expand_dims(labels, 1)
  # Labels are indices of classes, convert them to one hot encodings.
  target_one_hot = array_ops.one_hot(indices=labels, depth=num_classes)
  labels = math_ops.reduce_sum(
      input_tensor=target_one_hot, reduction_indices=[1])
  labels = math_ops.to_float(labels)

  # Calculate softmax probabilities for each class.
  unnormalized_probs = math_ops.exp(logits)
  normalizers = math_ops.reduce_sum(unnormalized_probs, 1, keepdims=True)
  softmax_predictions = math_ops.divide(unnormalized_probs,
                                        math_ops.add(normalizers, eps))

  # Pull out the probabilities for real label.
  probs_for_real_class = math_ops.reduce_sum(labels * softmax_predictions, 1)

  # Add handling for values near 0 and 1.
  zeros = array_ops.zeros_like(probs_for_real_class, dtype=logits.dtype) + eps
  one_minus_eps = array_ops.ones_like(
      probs_for_real_class, dtype=logits.dtype) - eps

  # Take maximum(eps, pred)
  cond = (probs_for_real_class >= eps)
  probs_for_real_class = array_ops.where(cond, probs_for_real_class, zeros)

  # Take minimum(1-eps, pred)
  cond = (probs_for_real_class <= 1 - eps)
  probs_for_real_class = array_ops.where(cond, probs_for_real_class,
                                         one_minus_eps)

  unweighted_loss = array_ops.expand_dims(-math_ops.log(probs_for_real_class),
                                          1)
  if weights is None:
    return unweighted_loss, control_flow_ops.no_op()
  else:
    return unweighted_loss * weights, control_flow_ops.no_op()
Beispiel #13
0
 def _loss(probs, targets):
     one_hot_labels = array_ops.one_hot(
         math_ops.to_int32(targets),
         num_classes,
         on_value=1.,
         off_value=0.,
         dtype=dtypes.float32)
     return loss_fn(probs, one_hot_labels)
Beispiel #14
0
def acgan_discriminator_model(inputs, _, num_classes=10):
  return (
      discriminator_model(inputs, _),
      array_ops.one_hot(
          # TODO(haeusser): infer batch size from input
          random_ops.random_uniform(
              [3], maxval=num_classes, dtype=dtypes.int32),
          num_classes))
Beispiel #15
0
 def to_dnn_input_layer(self,
                        input_tensor,
                        weight_collections=None,
                        trainable=True):
   return array_ops.reshape(
       array_ops.one_hot(
           math_ops.to_int64(input_tensor), self.length, 1., 0.),
       [-1, self.length * self.source_column.dimension])
Beispiel #16
0
 def loop_body(loop_count, cdf):
   temp = math_ops.reduce_sum(
       math_ops.cast(
           math_ops.less_equal(indices, loop_count), dtypes.float32))
   cdf = math_ops.add(
       cdf,
       array_ops.one_hot(
           loop_count, depth=nbins, on_value=temp, off_value=0.0))
   return [loop_count + 1, cdf]
Beispiel #17
0
 def create_input_and_label_tensor(batch_size, img_size, c_size, num_domains):
   input_tensor_list = []
   label_tensor_list = []
   for _ in range(num_domains):
     input_tensor_list.append(
         random_ops.random_uniform((batch_size, img_size, img_size, c_size)))
     domain_idx = random_ops.random_uniform(
         [batch_size], minval=0, maxval=num_domains, dtype=dtypes.int32)
     label_tensor_list.append(array_ops.one_hot(domain_idx, num_domains))
   return input_tensor_list, label_tensor_list
Beispiel #18
0
 def _loss(probs, targets):
   if targets.get_shape().ndims > 1:
     targets = array_ops.squeeze(targets, squeeze_dims=[1])
   one_hot_labels = array_ops.one_hot(
       math_ops.to_int32(targets),
       num_classes,
       on_value=1.,
       off_value=0.,
       dtype=dtypes.float32)
   return loss_fn(probs, one_hot_labels)
 def _testOneHot(self,
                 truth,
                 use_gpu=False,
                 expected_err_re=None,
                 raises=None,
                 **inputs):
   with self.test_session(use_gpu=use_gpu):
     if raises is not None:
       with self.assertRaises(raises):
         array_ops.one_hot(**inputs)
     else:
       ans = array_ops.one_hot(**inputs)
       if expected_err_re is None:
         tf_ans = ans.eval()
         self.assertAllEqual(tf_ans, truth)
         self.assertEqual(tf_ans.shape, ans.get_shape())
       else:
         with self.assertRaisesOpError(expected_err_re):
           ans.eval()
Beispiel #20
0
def logistic_model_no_mode_fn(features, labels):
  features = extract(features, 'input')
  labels = extract(labels, 'labels')
  labels = array_ops.one_hot(labels, 3, 1, 0)
  prediction, loss = (models.logistic_regression_zero_init(features, labels))
  train_op = optimizers.optimize_loss(
      loss, variables.get_global_step(), optimizer='Adagrad', learning_rate=0.1)
  return {
      'class': math_ops.argmax(prediction, 1),
      'prob': prediction
  }, loss, train_op
  def testOneHot(self):
    with self.test_session() as session, self.test_scope():
      indices = array_ops.constant(np.array([[2, 3], [0, 1]], dtype=np.int32))
      op = array_ops.one_hot(indices,
                             np.int32(4),
                             on_value=np.float32(7), off_value=np.float32(3))
      output = session.run(op)
      expected = np.array([[[3, 3, 7, 3], [3, 3, 3, 7]],
                           [[7, 3, 3, 3], [3, 7, 3, 3]]],
                          dtype=np.float32)
      self.assertAllEqual(output, expected)

      op = array_ops.one_hot(indices,
                             np.int32(4),
                             on_value=np.int32(2), off_value=np.int32(1),
                             axis=1)
      output = session.run(op)
      expected = np.array([[[1, 1], [1, 1], [2, 1], [1, 2]],
                           [[2, 1], [1, 2], [1, 1], [1, 1]]],
                          dtype=np.int32)
      self.assertAllEqual(output, expected)
Beispiel #22
0
 def _run_cell(cell_fn, **kwargs):
   inputs = array_ops.one_hot([1, 2, 3, 4], 4)
   cell = cell_fn(5, **kwargs)
   cell.build(inputs.shape)
   initial_state = cell.get_initial_state(
       inputs=inputs, batch_size=4, dtype=dtypes.float32)
   inputs, _ = cell(inputs, initial_state)
   output = inputs
   if not context.executing_eagerly():
     self.evaluate(variables_lib.global_variables_initializer())
     output = self.evaluate(output)
   return output
 def _sample_n(self, n, seed=None):
   sample_shape = array_ops.concat(([n], array_ops.shape(self.logits)), 0)
   logits = self.logits
   if logits.get_shape().ndims == 2:
     logits_2d = logits
   else:
     logits_2d = array_ops.reshape(logits, [-1, self.num_classes])
   samples = random_ops.multinomial(logits_2d, n, seed=seed)
   samples = array_ops.transpose(samples)
   samples = array_ops.one_hot(samples, self.num_classes, dtype=self.dtype)
   ret = array_ops.reshape(samples, sample_shape)
   return ret
Beispiel #24
0
def _ilabel_to_state(labels, num_labels, ilabel_log_probs):
    """Project ilabel log probs to state log probs."""

    num_label_states = _get_dim(labels, 1)
    blank = ilabel_log_probs[:, :, :1]
    blank = array_ops.tile(blank, [1, 1, num_label_states + 1])
    one_hot = array_ops.one_hot(labels, depth=num_labels)
    one_hot = array_ops.expand_dims(one_hot, axis=0)
    ilabel_log_probs = array_ops.expand_dims(ilabel_log_probs, axis=2)
    state_log_probs = math_ops.reduce_sum(ilabel_log_probs * one_hot, axis=3)
    state_log_probs = array_ops.concat([state_log_probs, blank], axis=2)
    return array_ops.pad(state_log_probs, [[0, 0], [0, 0], [1, 0]],
                         constant_values=math_ops.log(0.0))
 def _sample_n(self, n, seed):
   with ops.control_dependencies(self._runtime_assertions):
     x = self.components_distribution.sample(n)             # [n, B, k, E]
     # TODO(jvdillon): Consider using tf.gather (by way of index unrolling).
     npdt = x.dtype.as_numpy_dtype
     mask = array_ops.one_hot(
         indices=self.mixture_distribution.sample(n),       # [n, B]
         depth=self._num_components,                        # == k
         on_value=np.ones([], dtype=npdt),
         off_value=np.zeros([], dtype=npdt))                # [n, B, k]
     mask = self._pad_mix_dims(mask)                        # [n, B, k, [1]*e]
     return math_ops.reduce_sum(
         x * mask, axis=-1 - self._event_ndims)             # [n, B, E]
Beispiel #26
0
 def _run_cell(cell_fn, **kwargs):
   with self.cached_session() as sess:
     inputs = array_ops.one_hot([1, 2, 3, 4], 4)
     cell = cell_fn(5, **kwargs)
     cell.build(inputs.shape)
     initial_state = cell.get_initial_state(
         inputs=inputs, batch_size=4, dtype=dtypes.float32)
     inputs, _ = cell(inputs, initial_state)
     output = inputs
     if not context.executing_eagerly():
       self.evaluate(variables_lib.global_variables_initializer())
       output = self.evaluate(output)
     return output
Beispiel #27
0
def logistic_model_no_mode_fn(features, labels):
    features = extract(features, 'input')
    labels = extract(labels, 'labels')
    labels = array_ops.one_hot(labels, 3, 1, 0)
    prediction, loss = (models.logistic_regression_zero_init(features, labels))
    train_op = optimizers.optimize_loss(loss,
                                        variables.get_global_step(),
                                        optimizer='Adagrad',
                                        learning_rate=0.1)
    return {
        'class': math_ops.argmax(prediction, 1),
        'prob': prediction
    }, loss, train_op
Beispiel #28
0
 def _sample_n(self, n, seed):
   with ops.control_dependencies(self._runtime_assertions):
     x = self.components_distribution.sample(n)             # [n, B, k, E]
     # TODO(jvdillon): Consider using tf.gather (by way of index unrolling).
     npdt = x.dtype.as_numpy_dtype
     mask = array_ops.one_hot(
         indices=self.mixture_distribution.sample(n),       # [n, B]
         depth=self._num_components,                        # == k
         on_value=np.ones([], dtype=npdt),
         off_value=np.zeros([], dtype=npdt))                # [n, B, k]
     mask = self._pad_mix_dims(mask)                        # [n, B, k, [1]*e]
     return math_ops.reduce_sum(
         x * mask, axis=-1 - self._event_ndims)             # [n, B, E]
 def _testOneHot(self,
                 truth,
                 use_gpu=False,
                 expected_err_re=None,
                 raises=None,
                 dtype=None,
                 **inputs):
   with self.cached_session(use_gpu=use_gpu):
     if raises is not None:
       with self.assertRaises(raises):
         array_ops.one_hot(dtype=dtype, **inputs)
     else:
       ans = array_ops.one_hot(dtype=dtype, **inputs)
       if expected_err_re is None:
         tf_ans = self.evaluate(ans)
         self.assertAllEqual(tf_ans, truth)
         if dtype:
           self.assertEqual(tf_ans.dtype, dtype)
         self.assertEqual(tf_ans.shape, ans.get_shape())
       else:
         with self.assertRaisesOpError(expected_err_re):
           self.evaluate(ans)
Beispiel #30
0
def _get_batch_from_per_class_queues(per_class_queues, probs, batch_size):
    """Generates batches according to per-class-probabilities."""
    num_classes = probs.get_shape().num_elements()
    # Number of examples per class is governed by a multinomial distribution.
    # Note: multinomial takes unnormalized log probabilities for its first
    # argument, of dimension [batch_size, num_classes].
    examples = random_ops.multinomial(
        array_ops.expand_dims(math_ops.log(probs), 0), batch_size)

    # Prepare the data and label batches.
    val_list = []
    label_list = []
    for i in range(num_classes):
        num_examples = math_ops.reduce_sum(
            math_ops.cast(math_ops.equal(examples, i), dtypes.int32))
        tensors = per_class_queues[i].dequeue_many(num_examples)

        # If you enqueue a list with a single tensor, only a single tensor is
        # returned. If you enqueue a list with multiple tensors, then a list is
        # returned. We want to handle both cases, so reduce the case of the single
        # tensor to the case of multiple tensors.
        if not isinstance(tensors, list):
            tensors = [tensors]

        val_list.append(tensors)
        label_list.append(
            array_ops.ones([num_examples], dtype=dtypes.int32) * i)

    # Create a list of tensor of values. val_list is of dimension
    # [num_classes x len(tensors)]. We want list_batch_vals to be of dimension
    # [len(tensors)].
    num_data = len(val_list[0])
    list_batch_vals = [
        array_ops.concat(0, [val_list[i][j] for i in range(num_classes)])
        for j in range(num_data)
    ]

    # Create a tensor of labels.
    batch_labels = array_ops.concat(0, label_list)
    batch_labels.set_shape([batch_size])

    # Debug instrumentation.
    sample_tags = [
        'stratified_sample/%s/samples_class%i' % (batch_labels.name, i)
        for i in range(num_classes)
    ]
    logging_ops.scalar_summary(
        sample_tags,
        math_ops.reduce_sum(array_ops.one_hot(batch_labels, num_classes), 0))

    return list_batch_vals, batch_labels
    def _inverse(self, y):
        # To derive the inverse mapping note that:
        #   y[i] = exp(x[i]) / normalization
        # and
        #   y[end] = 1 / normalization.
        # Thus:
        # x[i] = log(exp(x[i])) - log(y[end]) - log(normalization)
        #      = log(exp(x[i])/normalization) - log(y[end])
        #      = log(y[i]) - log(y[end])
        shape = (np.asarray(y.shape.as_list(), dtype=np.int32) if
                 y.shape.is_fully_defined() else array_ops.shape(y,
                                                                 name="shape"))
        ndims = _get_ndims(y)

        # Do this first to make sure CSE catches that it'll happen again in
        # _inverse_log_det_jacobian.
        x = math_ops.log(y)

        # We now extract the last coordinate of the rightmost dimension.
        # Our trick is to slice from [0,0,...,shape[-1]-1] to shape[:-1]+[1].
        begin = array_ops.one_hot(indices=ndims - 1,
                                  depth=ndims,
                                  on_value=shape[-1] -
                                  np.array(1, dtype=shape.dtype),
                                  dtype=shape.dtype)
        size = array_ops.concat(
            [shape[:-1], np.asarray([1], dtype=shape.dtype)], 0)
        log_normalization = -array_ops.strided_slice(x, begin, begin + size)

        # Here we slice out all but the last coordinate; see above for idea.
        begin = array_ops.zeros_like(shape)
        size = array_ops.concat([shape[:-1], [shape[-1] - 1]], 0)
        x = array_ops.strided_slice(x, begin, begin + size)

        x += log_normalization

        if self._static_event_ndims == 0:
            x = array_ops.squeeze(x, squeeze_dims=[ndims - 1])

        # Set shape hints.
        if y.shape.ndims is not None:
            shape = y.shape.as_list()
            if self._static_event_ndims == 0:
                shape = shape[:-1]
            elif shape[-1] is not None:
                shape[-1] -= 1
            shape = tensor_shape.TensorShape(shape)
            x.shape.assert_is_compatible_with(shape)
            x.set_shape(shape)

        return x
Beispiel #32
0
def ctc_state_log_probs(seq_lengths, max_seq_length):
  """Computes CTC alignment initial and final state log probabilities.

  Create the initial/final state values directly as log values to avoid
  having to take a float64 log on tpu (which does not exist).

  Args:
    seq_lengths: int tensor of shape [batch_size], seq lengths in the batch.
    max_seq_length: int, max sequence length possible.

  Returns:
    initial_state_log_probs, final_state_log_probs
  """

  batch_size = _get_dim(seq_lengths, 0)
  num_label_states = max_seq_length + 1
  num_duration_states = 2
  num_states = num_duration_states * num_label_states
  log_0 = math_ops.cast(
      math_ops.log(math_ops.cast(0, dtypes.float64) + 1e-307), dtypes.float32)

  initial_state_log_probs = array_ops.one_hot(
      indices=array_ops.zeros([batch_size], dtype=dtypes.int32),
      depth=num_states,
      on_value=0.0,
      off_value=log_0,
      axis=1)

  label_final_state_mask = array_ops.one_hot(
      seq_lengths, depth=num_label_states, axis=0)
  duration_final_state_mask = array_ops.ones(
      [num_duration_states, 1, batch_size])
  final_state_mask = duration_final_state_mask * label_final_state_mask
  final_state_log_probs = (1.0 - final_state_mask) * log_0
  final_state_log_probs = array_ops.reshape(final_state_log_probs,
                                            [num_states, batch_size])

  return initial_state_log_probs, array_ops.transpose(final_state_log_probs)
def get_cluster_assignment(pairwise_distances, centroid_ids):
  """Assign data points to the neareset centroids.

  Tensorflow has numerical instability and doesn't always choose
    the data point with theoretically zero distance as it's nearest neighbor.
    Thus, for each centroid in centroid_ids, explicitly assign
    the centroid itself as the nearest centroid.
    This is done through the mask tensor and the constraint_vect tensor.

  Args:
    pairwise_distances: 2-D Tensor of pairwise distances.
    centroid_ids: 1-D Tensor of centroid indices.

  Returns:
    y_fixed: 1-D tensor of cluster assignment.
  """
  predictions = math_ops.argmin(
      array_ops.gather(pairwise_distances, centroid_ids), dimension=0)
  batch_size = array_ops.shape(pairwise_distances)[0]

  # Deal with numerical instability
  mask = math_ops.reduce_any(array_ops.one_hot(
      centroid_ids, batch_size, True, False, axis=-1, dtype=dtypes.bool),
                             axis=0)
  constraint_one_hot = math_ops.multiply(
      array_ops.one_hot(centroid_ids,
                        batch_size,
                        array_ops.constant(1, dtype=dtypes.int64),
                        array_ops.constant(0, dtype=dtypes.int64),
                        axis=0,
                        dtype=dtypes.int64),
      math_ops.cast(math_ops.range(array_ops.shape(centroid_ids)[0]),
                    dtypes.int64))
  constraint_vect = math_ops.reduce_sum(
      array_ops.transpose(constraint_one_hot), axis=0)

  y_fixed = array_ops.where(mask, constraint_vect, predictions)
  return y_fixed
Beispiel #34
0
def ctc_state_log_probs(seq_lengths, max_seq_length):
  """Computes CTC alignment initial and final state log probabilities.

  Create the initial/final state values directly as log values to avoid
  having to take a float64 log on tpu (which does not exist).

  Args:
    seq_lengths: int tensor of shape [batch_size], seq lengths in the batch.
    max_seq_length: int, max sequence length possible.

  Returns:
    initial_state_log_probs, final_state_log_probs
  """

  batch_size = _get_dim(seq_lengths, 0)
  num_label_states = max_seq_length + 1
  num_duration_states = 2
  num_states = num_duration_states * num_label_states
  log_0 = math_ops.cast(
      math_ops.log(math_ops.cast(0, dtypes.float64) + 1e-307),
      dtypes.float32)

  initial_state_log_probs = array_ops.one_hot(
      indices=array_ops.zeros([batch_size], dtype=dtypes.int32),
      depth=num_states,
      on_value=0.0,
      off_value=log_0, axis=1)

  label_final_state_mask = array_ops.one_hot(
      seq_lengths, depth=num_label_states, axis=0)
  duration_final_state_mask = array_ops.ones(
      [num_duration_states, 1, batch_size])
  final_state_mask = duration_final_state_mask * label_final_state_mask
  final_state_log_probs = (1.0 - final_state_mask) * log_0
  final_state_log_probs = array_ops.reshape(
      final_state_log_probs, [num_states, batch_size])

  return initial_state_log_probs, array_ops.transpose(final_state_log_probs)
Beispiel #35
0
def _generate_stargan_random_domain_target(batch_size, num_domains):
  """Generate random domain label.

  Args:
    batch_size: (int) Number of random domain label.
    num_domains: (int) Number of domains representing with the label.

  Returns:
    Tensor of shape (batch_size, num_domains) representing random label.
  """
  domain_idx = random_ops.random_uniform(
      [batch_size], minval=0, maxval=num_domains, dtype=dtypes.int32)

  return array_ops.one_hot(domain_idx, num_domains)
 def _sample_n(self, n, seed=None):
     sample_shape = array_ops.concat(([n], array_ops.shape(self.logits)), 0)
     logits = self.logits
     if logits.get_shape().ndims == 2:
         logits_2d = logits
     else:
         logits_2d = array_ops.reshape(logits, [-1, self.num_classes])
     samples = random_ops.multinomial(logits_2d, n, seed=seed)
     samples = array_ops.transpose(samples)
     samples = array_ops.one_hot(samples,
                                 self.num_classes,
                                 dtype=self.dtype)
     ret = array_ops.reshape(samples, sample_shape)
     return ret
Beispiel #37
0
def _generate_stargan_random_domain_target(batch_size, num_domains):
  """Generate random domain label.

  Args:
    batch_size: (int) Number of random domain label.
    num_domains: (int) Number of domains representing with the label.

  Returns:
    Tensor of shape (batch_size, num_domains) representing random label.
  """
  domain_idx = random_ops.random_uniform(
      [batch_size], minval=0, maxval=num_domains, dtype=dtypes.int32)

  return array_ops.one_hot(domain_idx, num_domains)
    def call(self, inputs, count_weights=None):
        if count_weights is not None and self._output_mode != COUNT:
            raise ValueError(
                "count_weights is not used in `output_mode='tf-idf'`, "
                "or `output_mode='binary'`. Please pass a single input.")
        self._called = True
        if self._max_tokens is None:
            out_depth = K.get_value(self.num_elements)
        else:
            out_depth = self._max_tokens

        if self._output_mode == TFIDF:
            # If the input is a sparse tensor, we densify it with the default value of
            # -1. Because -1 is ignored by one_hot, this effectively drops the non-set
            # positions from the output encoding.
            if self._sparse:
                raise ValueError("`sparse=True` with `output_mode=tfidf` "
                                 "is not supported.")
            if isinstance(inputs, sparse_tensor.SparseTensor):
                inputs = sparse_ops.sparse_tensor_to_dense(inputs,
                                                           default_value=-1)
            one_hot_data = array_ops.one_hot(inputs, depth=out_depth)
            counts = math_ops.reduce_sum(one_hot_data, axis=1)
            tf_idf_data = math_ops.multiply(counts, self.tf_idf_weights)
            tf_idf_data.set_shape(tensor_shape.TensorShape((None, out_depth)))
            return tf_idf_data

        binary_output = (self._output_mode == BINARY)
        if self._sparse:
            result = bincount_ops.sparse_bincount(inputs,
                                                  weights=count_weights,
                                                  minlength=out_depth,
                                                  axis=-1,
                                                  binary_output=binary_output)
            result = math_ops.cast(result, K.floatx())
            batch_size = array_ops.shape(result)[0]
            result = sparse_tensor.SparseTensor(
                indices=result.indices,
                values=result.values,
                dense_shape=[batch_size, out_depth])
            return result
        else:
            result = bincount_ops.bincount(inputs,
                                           weights=count_weights,
                                           minlength=out_depth,
                                           dtype=K.floatx(),
                                           axis=-1,
                                           binary_output=binary_output)
            result.set_shape(tensor_shape.TensorShape((None, out_depth)))
            return result
Beispiel #39
0
def _ilabel_to_state(labels, num_labels, ilabel_log_probs):
  """Project ilabel log probs to state log probs."""

  num_label_states = _get_dim(labels, 1)
  blank = ilabel_log_probs[:, :, :1]
  blank = array_ops.tile(blank, [1, 1, num_label_states + 1])
  one_hot = array_ops.one_hot(labels, depth=num_labels)
  one_hot = array_ops.expand_dims(one_hot, axis=0)
  ilabel_log_probs = array_ops.expand_dims(ilabel_log_probs, axis=2)
  state_log_probs = math_ops.reduce_sum(ilabel_log_probs * one_hot, axis=3)
  state_log_probs = array_ops.concat([state_log_probs, blank], axis=2)
  return array_ops.pad(
      state_log_probs, [[0, 0], [0, 0], [1, 0]],
      constant_values=math_ops.log(0.0))
Beispiel #40
0
def get_cluster_assignment(pairwise_distances, centroid_ids):
  """Assign data points to the neareset centroids.

  Tensorflow has numerical instability and doesn't always choose
    the data point with theoretically zero distance as it's nearest neighbor.
    Thus, for each centroid in centroid_ids, explicitly assign
    the centroid itself as the nearest centroid.
    This is done through the mask tensor and the constraint_vect tensor.

  Args:
    pairwise_distances: 2-D Tensor of pairwise distances.
    centroid_ids: 1-D Tensor of centroid indices.

  Returns:
    y_fixed: 1-D tensor of cluster assignment.
  """
  predictions = math_ops.argmin(
      array_ops.gather(pairwise_distances, centroid_ids), dimension=0)
  batch_size = array_ops.shape(pairwise_distances)[0]

  # Deal with numerical instability
  mask = math_ops.reduce_any(array_ops.one_hot(
      centroid_ids, batch_size, True, False, axis=-1, dtype=dtypes.bool),
                             axis=0)
  constraint_one_hot = math_ops.multiply(
      array_ops.one_hot(centroid_ids,
                        batch_size,
                        array_ops.constant(1, dtype=dtypes.int64),
                        array_ops.constant(0, dtype=dtypes.int64),
                        axis=0,
                        dtype=dtypes.int64),
      math_ops.to_int64(math_ops.range(array_ops.shape(centroid_ids)[0])))
  constraint_vect = math_ops.reduce_sum(
      array_ops.transpose(constraint_one_hot), axis=0)

  y_fixed = array_ops.where(mask, constraint_vect, predictions)
  return y_fixed
    def test_get_gan_model(self, mode):
        with ops.Graph().as_default():
            input_data = array_ops.ones([6, 4, 4, 3])
            input_data_domain_label = array_ops.one_hot([0] * 6, 5)
            gan_model = estimator._get_gan_model(mode,
                                                 dummy_generator_fn,
                                                 dummy_discriminator_fn,
                                                 input_data,
                                                 input_data_domain_label,
                                                 add_summaries=False)

        self.assertEqual(input_data, gan_model.input_data)
        self.assertIsNotNone(gan_model.generated_data)
        self.assertIsNotNone(gan_model.generated_data_domain_target)
        self.assertEqual(1, len(gan_model.generator_variables))
        self.assertIsNotNone(gan_model.generator_scope)
        self.assertIsNotNone(gan_model.generator_fn)
        if mode == model_fn_lib.ModeKeys.PREDICT:
            self.assertIsNone(gan_model.input_data_domain_label)
            self.assertEqual(input_data_domain_label,
                             gan_model.generated_data_domain_target)
            self.assertIsNone(gan_model.reconstructed_data)
            self.assertIsNone(
                gan_model.discriminator_input_data_source_predication)
            self.assertIsNone(
                gan_model.discriminator_generated_data_source_predication)
            self.assertIsNone(
                gan_model.discriminator_input_data_domain_predication)
            self.assertIsNone(
                gan_model.discriminator_generated_data_domain_predication)
            self.assertIsNone(gan_model.discriminator_variables)
            self.assertIsNone(gan_model.discriminator_scope)
            self.assertIsNone(gan_model.discriminator_fn)
        else:
            self.assertEqual(input_data_domain_label,
                             gan_model.input_data_domain_label)
            self.assertIsNotNone(gan_model.reconstructed_data.shape)
            self.assertIsNotNone(
                gan_model.discriminator_input_data_source_predication)
            self.assertIsNotNone(
                gan_model.discriminator_generated_data_source_predication)
            self.assertIsNotNone(
                gan_model.discriminator_input_data_domain_predication)
            self.assertIsNotNone(
                gan_model.discriminator_generated_data_domain_predication)
            self.assertEqual(2, len(
                gan_model.discriminator_variables))  # 1 FC layer
            self.assertIsNotNone(gan_model.discriminator_scope)
            self.assertIsNotNone(gan_model.discriminator_fn)
  def _inverse(self, y):
    # To derive the inverse mapping note that:
    #   y[i] = exp(x[i]) / normalization
    # and
    #   y[end] = 1 / normalization.
    # Thus:
    # x[i] = log(exp(x[i])) - log(y[end]) - log(normalization)
    #      = log(exp(x[i])/normalization) - log(y[end])
    #      = log(y[i]) - log(y[end])
    shape = (np.asarray(y.shape.as_list(), dtype=np.int32)
             if y.shape.is_fully_defined()
             else array_ops.shape(y, name="shape"))
    ndims = distribution_util.prefer_static_rank(y)

    # Do this first to make sure CSE catches that it'll happen again in
    # _inverse_log_det_jacobian.
    x = math_ops.log(y)

    # We now extract the last coordinate of the rightmost dimension.
    # Our trick is to slice from [0,0,...,shape[-1]-1] to shape[:-1]+[1].
    begin = array_ops.one_hot(indices=ndims-1,
                              depth=ndims,
                              on_value=shape[-1]-np.array(1, dtype=shape.dtype),
                              dtype=shape.dtype)
    size = array_ops.concat([shape[:-1], np.asarray([1], dtype=shape.dtype)], 0)
    log_normalization = -array_ops.strided_slice(x, begin, begin + size)

    # Here we slice out all but the last coordinate; see above for idea.
    begin = array_ops.zeros_like(shape)
    size = array_ops.concat([shape[:-1], [shape[-1] - 1]], 0)
    x = array_ops.strided_slice(x, begin, begin + size)

    x += log_normalization

    if self._static_event_ndims == 0:
      x = array_ops.squeeze(x, squeeze_dims=[ndims-1])

    # Set shape hints.
    if y.shape.ndims is not None:
      shape = y.shape.as_list()
      if self._static_event_ndims == 0:
        shape = shape[:-1]
      elif shape[-1] is not None:
        shape[-1] -= 1
      shape = tensor_shape.TensorShape(shape)
      x.shape.assert_is_compatible_with(shape)
      x.set_shape(shape)

    return x
Beispiel #43
0
    def testOneHot(self):
        with self.test_session() as session, self.test_scope():
            indices = array_ops.constant(
                np.array([[2, 3], [0, 1]], dtype=np.int32))
            op = array_ops.one_hot(indices,
                                   np.int32(4),
                                   on_value=np.float32(7),
                                   off_value=np.float32(3))
            output = session.run(op)
            expected = np.array(
                [[[3, 3, 7, 3], [3, 3, 3, 7]], [[7, 3, 3, 3], [3, 7, 3, 3]]],
                dtype=np.float32)
            self.assertAllEqual(output, expected)

            op = array_ops.one_hot(indices,
                                   np.int32(4),
                                   on_value=np.int32(2),
                                   off_value=np.int32(1),
                                   axis=1)
            output = session.run(op)
            expected = np.array([[[1, 1], [1, 1], [2, 1], [1, 2]],
                                 [[2, 1], [1, 2], [1, 1], [1, 1]]],
                                dtype=np.int32)
            self.assertAllEqual(output, expected)
Beispiel #44
0
def _state_to_olabel(labels, num_labels, states):
  """Sum state log probs to ilabel log probs."""

  num_label_states = _get_dim(labels, 1) + 1
  label_states = states[:, :, 1:num_label_states]
  blank_states = states[:, :, num_label_states:]
  one_hot = array_ops.one_hot(
      labels - 1, depth=(num_labels - 1),
      on_value=0.0, off_value=math_ops.log(0.0))
  one_hot = array_ops.expand_dims(one_hot, axis=0)
  label_states = array_ops.expand_dims(label_states, axis=3)
  label_olabels = math_ops.reduce_logsumexp(label_states + one_hot, axis=2)
  blank_olabels = math_ops.reduce_logsumexp(
      blank_states, axis=2, keepdims=True)
  return array_ops.concat([blank_olabels, label_olabels], axis=-1)
Beispiel #45
0
def _state_to_olabel(labels, num_labels, states):
  """Sum state log probs to ilabel log probs."""

  num_label_states = _get_dim(labels, 1) + 1
  label_states = states[:, :, 1:num_label_states]
  blank_states = states[:, :, num_label_states:]
  one_hot = array_ops.one_hot(
      labels - 1, depth=(num_labels - 1),
      on_value=0.0, off_value=math_ops.log(0.0))
  one_hot = array_ops.expand_dims(one_hot, axis=0)
  label_states = array_ops.expand_dims(label_states, axis=3)
  label_olabels = math_ops.reduce_logsumexp(label_states + one_hot, axis=2)
  blank_olabels = math_ops.reduce_logsumexp(
      blank_states, axis=2, keepdims=True)
  return array_ops.concat([blank_olabels, label_olabels], axis=-1)
Beispiel #46
0
 def initial_alignments(self, batch_size, dtype):
     """Creates the initial alignment values for the monotonic attentions.
     Initializes to dirac distributions, i.e. [1, 0, 0, ...memory length..., 0]
     for all entries in the batch.
     Args:
     batch_size: `int32` scalar, the batch_size.
     dtype: The `dtype`.
     Returns:
     A `dtype` tensor shaped `[batch_size, alignments_size]`
     (`alignments_size` is the values' `max_time`).
     """
     max_time = self._alignments_size
     return array_ops.one_hot(
         array_ops.zeros((batch_size,), dtype=dtypes.int32), max_time,
         dtype=dtype)
Beispiel #47
0
 def create_input_and_label_tensor(batch_size, img_size, c_size,
                                   num_domains):
     input_tensor_list = []
     label_tensor_list = []
     for _ in range(num_domains):
         input_tensor_list.append(
             random_ops.random_uniform(
                 (batch_size, img_size, img_size, c_size)))
         domain_idx = random_ops.random_uniform([batch_size],
                                                minval=0,
                                                maxval=num_domains,
                                                dtype=dtypes.int32)
         label_tensor_list.append(array_ops.one_hot(domain_idx,
                                                    num_domains))
     return input_tensor_list, label_tensor_list
Beispiel #48
0
    def __init__(self,
                 loc,
                 scale,
                 validate_args=False,
                 allow_nan_stats=True,
                 name="von-Mises-Fisher"):
        """Construct von-Mises-Fisher distributions with mean and concentration `loc` and `scale`.

        Args:
          loc: Floating point tensor; the mean of the distribution(s).
          scale: Floating point tensor; the concentration of the distribution(s).
            Must contain only non-negative values.
          validate_args: Python `bool`, default `False`. When `True` distribution
            parameters are checked for validity despite possibly degrading runtime
            performance. When `False` invalid inputs may silently render incorrect
            outputs.
          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.

        Raises:
          TypeError: if `loc` and `scale` have different `dtype`.
        """
        parameters = locals()
        with ops.name_scope(name, values=[loc, scale]):
            with ops.control_dependencies([
                    check_ops.assert_positive(scale),
                    check_ops.
                    assert_near(linalg_ops.norm(loc, axis=-1), 1, atol=1e-7)
            ] if validate_args else []):
                self._loc = array_ops.identity(loc, name="loc")
                self._scale = array_ops.identity(scale, name="scale")
                check_ops.assert_same_float_dtype([self._loc, self._scale])

        super(VonMisesFisher, self).__init__(
            dtype=self._scale.dtype,
            reparameterization_type=distribution.FULLY_REPARAMETERIZED,
            validate_args=validate_args,
            allow_nan_stats=allow_nan_stats,
            parameters=parameters,
            graph_parents=[self._loc, self._scale],
            name=name)

        self.__m = math_ops.cast(self._loc.shape[-1], dtypes.int32)
        self.__mf = math_ops.cast(self.__m, dtype=self.dtype)
        self.__e1 = array_ops.one_hot([0], self.__m, dtype=self.dtype)
Beispiel #49
0
def segment_iou(predicts, labels):
    """
    :param predicts: shape [batch_size, h, w, c]
    :param labels: shape [batch_size, h, w]
    """
    num_classes = predicts.shape[-1]
    labels = array_ops.one_hot(labels, depth=num_classes)
    predicts = engine.bool(predicts)
    labels = engine.bool(labels)
    inter = engine.float32(math_ops.logical_and(predicts, labels))
    union = engine.float32(math_ops.logical_or(predicts, labels))
    inter = math_ops.reduce_sum(inter, axis=[0, 1, 2])
    union = math_ops.reduce_sum(union, axis=[0, 1, 2])
    classes_iou = inter / union
    mean_iou = math_ops.reduce_mean(classes_iou)
    return mean_iou, classes_iou
Beispiel #50
0
    def initialize(self, name=None):
        """Initialize the decoder.
    Args:
      name: Name scope for any created operations.
    Returns:
      `(finished, start_inputs, initial_state)`.
    """
        finished, start_inputs = self._finished, self._start_inputs

        # Calculating candidate region inputs
        # set prefix mass to N terminus
        start_prefix = array_ops.ones([self._batch_size, self._beam_width],
                                      dtype=dtypes.float32) * tf.constant(
                                          aa_conf.mass_N_terminus)

        with tf.name_scope('fetch_start_input'), tf.device("/gpu:0"):
            candidate_spec = tf.map_fn(
                lambda x: iterator_utils.get_candidate_intensity(
                    x[0], x[1], x[2]),
                (self._source_spectrum, self._peptide_mass, start_prefix),
                dtype=tf.float32,
                parallel_iterations=200,
                back_prop=False)

        # apply input model
        next_candidate_input = self._decoder_input_model_fn(candidate_spec)
        # concat with embeded start token
        start_inputs_combined = tf.concat([start_inputs, next_candidate_input],
                                          2)

        log_probs = array_ops.one_hot(  # shape(batch_sz, beam_sz)
            array_ops.zeros([self._batch_size], dtype=dtypes.int32),
            depth=self._beam_width,
            on_value=0.0,
            off_value=-np.Inf,
            dtype=nest.flatten(self._initial_cell_state)[0].dtype)

        initial_state = IonBeamSearchDecoderState(
            cell_state=self._initial_cell_state,
            log_probs=log_probs,
            finished=finished,
            prefix_mass=start_prefix,  #######################
            lengths=array_ops.zeros([self._batch_size, self._beam_width],
                                    dtype=dtypes.int64))

        return (finished, start_inputs_combined, initial_state
                )  # start_inputs, initial_state)
    def initialize(self, name=None):
        """Initialize the decoder.
        Args:
                name: Name scope for any created operations.
        Returns:
        `(finished, start_inputs, initial_state)`.
        """
        finished, start_inputs = self._finished, self._start_inputs

        dtype = nest.flatten(self._initial_cell_state)[0].dtype

        if self._start_token_logits is None:
            log_probs = array_ops.one_hot(  # shape(batch_sz, beam_sz)
                array_ops.zeros([self._batch_size], dtype=dtypes.int32),
                depth=self._beam_width,
                on_value=ops.convert_to_tensor(0.0, dtype=dtype),
                off_value=ops.convert_to_tensor(-np.Inf, dtype=dtype),
                dtype=dtype)
        else:
            log_probs = self._start_token_logits

        sequence_lengths = array_ops.zeros(
            [self._batch_size, self._beam_width], dtype=dtypes.int64)

        # Start tokens are part of output if no _GO token used. Make changes accordingly
        if not self._use_go_tokens:
            finished = math_ops.equal(self._start_tokens, self._raw_end_token)

            sequence_lengths = array_ops.where(
                math_ops.logical_not(finished),
                array_ops.fill(array_ops.shape(sequence_lengths),
                               tf.constant(1, dtype=dtypes.int64)),
                sequence_lengths)

        init_attention_probs = beam_search_decoder.get_attention_probs(
            self._initial_cell_state, self._coverage_penalty_weight)
        if init_attention_probs is None:
            init_attention_probs = ()

        initial_state = beam_search_decoder.BeamSearchDecoderState(
            cell_state=self._initial_cell_state,
            log_probs=log_probs,
            finished=finished,
            lengths=sequence_lengths,
            accumulated_attention_probs=init_attention_probs)

        return (finished, start_inputs, initial_state)
Beispiel #52
0
    def get_probs():
      """this simulates the initialize method in BeamSearchDecoder."""
      log_prob_mask = array_ops.one_hot(
          array_ops.zeros([self.batch_size], dtype=dtypes.int32),
          depth=self.beam_width,
          on_value=True,
          off_value=False,
          dtype=dtypes.bool)

      log_prob_zeros = array_ops.zeros(
          [self.batch_size, self.beam_width], dtype=dtypes.float32)
      log_prob_neg_inf = array_ops.ones(
          [self.batch_size, self.beam_width], dtype=dtypes.float32) * -np.Inf

      log_probs = array_ops.where(log_prob_mask, log_prob_zeros,
                                  log_prob_neg_inf)
      return log_probs
def hardmax(logits, name=None):
  """Returns batched one-hot vectors.
  The depth index containing the `1` is that of the maximum logit value.
  Args:
    logits: A batch tensor of logit values.
    name: Name to use when creating ops.
  Returns:
    A batched one-hot tensor.
  """
  with ops.name_scope(name, "Hardmax", [logits]):
    logits = ops.convert_to_tensor(logits, name="logits")
    if logits.get_shape()[-1].value is not None:
      depth = logits.get_shape()[-1].value
    else:
      depth = array_ops.shape(logits)[-1]
    return array_ops.one_hot(
        math_ops.argmax(logits, -1), depth, dtype=logits.dtype)
Beispiel #54
0
def labels_to_dataset(labels, label_mode, num_classes):
    """Create a tf.data.Dataset from the list/tuple of labels.
  Args:
    labels: list/tuple of labels to be converted into a tf.data.Dataset.
    label_mode: - 'binary' indicates that the labels (there can be only 2) are
      encoded as `float32` scalars with values 0 or 1 (e.g. for
      `binary_crossentropy`). - 'categorical' means that the labels are mapped
      into a categorical vector. (e.g. for `categorical_crossentropy` loss).
    num_classes: number of classes of labels.
  """
    label_ds = dataset_ops.Dataset.from_tensor_slices(labels)
    if label_mode == 'binary':
        label_ds = label_ds.map(lambda x: array_ops.expand_dims(
            math_ops.cast(x, 'float32'), axis=-1))
    elif label_mode == 'categorical':
        label_ds = label_ds.map(lambda x: array_ops.one_hot(x, num_classes))
    return label_ds
 def _sample_n(self, n, seed=None):
     n_draws = math_ops.cast(self.total_count, dtype=dtypes.int32)
     k = self.event_shape_tensor()[0]
     unnormalized_logits = array_ops.reshape(math_ops.log(
         random_ops.random_gamma(shape=[n],
                                 alpha=self.concentration,
                                 dtype=self.dtype,
                                 seed=seed)),
                                             shape=[-1, k])
     draws = random_ops.multinomial(logits=unnormalized_logits,
                                    num_samples=n_draws,
                                    seed=distribution_util.gen_new_seed(
                                        seed, salt="dirichlet_multinomial"))
     x = math_ops.reduce_sum(array_ops.one_hot(draws, depth=k), -2)
     final_shape = array_ops.concat(
         [[n], self.batch_shape_tensor(), [k]], 0)
     return array_ops.reshape(x, final_shape)
Beispiel #56
0
def _filter_top_k(x, k):
  """Filters top-k values in the last dim of x and set the rest to NEG_INF.

  Used for computing top-k prediction values in dense labels (which has the same
  shape as predictions) for recall and precision top-k metrics.

  Args:
    x: tensor with any dimensions.
    k: the number of values to keep.

  Returns:
    tensor with same shape and dtype as x.
  """
  _, top_k_idx = nn_ops.top_k(x, k, sorted=False)
  top_k_mask = math_ops.reduce_sum(
      array_ops.one_hot(top_k_idx, array_ops.shape(x)[-1], axis=-1), axis=-2)
  return x * top_k_mask + NEG_INF * (1 - top_k_mask)
def paths_and_labels_to_dataset(image_paths, image_size, num_channels, labels,
                                label_mode, num_classes, interpolation):
    """Constructs a dataset of images and labels."""
    # TODO(fchollet): consider making num_parallel_calls settable
    path_ds = dataset_ops.Dataset.from_tensor_slices(image_paths)
    img_ds = path_ds.map(
        lambda x: path_to_image(x, image_size, num_channels, interpolation))
    if label_mode:
        label_ds = dataset_ops.Dataset.from_tensor_slices(labels)
        if label_mode == 'binary':
            label_ds = label_ds.map(lambda x: array_ops.expand_dims(
                math_ops.cast(x, 'float32'), axis=-1))
        elif label_mode == 'categorical':
            label_ds = label_ds.map(
                lambda x: array_ops.one_hot(x, num_classes))
        img_ds = dataset_ops.Dataset.zip((img_ds, label_ds))
    return img_ds
Beispiel #58
0
def one_hot_matrix(tensor_in, num_classes, on_value=1.0, off_value=0.0):
    """Encodes indices from given tensor as one-hot tensor.

  TODO(ilblackdragon): Ideally implementation should be
  part of TensorFlow with Eigen-native operation.

  Args:
    tensor_in: Input tensor of shape [N1, N2].
    num_classes: Number of classes to expand index into.
    on_value: Tensor or float, value to fill-in given index.
    off_value: Tensor or float, value to fill-in everything else.
  Returns:
    Tensor of shape [N1, N2, num_classes] with 1.0 for each id in original
    tensor.
  """
    return array_ops_.one_hot(math_ops.cast(tensor_in, dtypes.int64),
                              num_classes, on_value, off_value)
Beispiel #59
0
    def _mask_outputs_by_lable(self, outputs, last_choice):
        """outputs shape=[batch_size, num_classes]"""

        vocab_size = array_ops.shape(outputs)[1]
        next_choies = gen_array_ops.gather_v2(params=self.lookup_table,
                                              indices=last_choice,
                                              axis=0)
        '''get the [batch_size, vocab_size] mask'''
        mask = math_ops.reduce_sum(array_ops.one_hot(indices=next_choies,
                                                     depth=vocab_size,
                                                     dtype=dtypes.int32),
                                   axis=1)
        mask = math_ops.cast(mask, dtype=dtypes.bool)
        # shape = [batch_size, beam_width, vacab_size]
        finished_probs = array_ops.fill(dims=array_ops.shape(outputs),
                                        value=outputs.dtype.min)
        return array_ops.where(mask, outputs, finished_probs)
Beispiel #60
0
 def testLuongMonotonicHard(self):
   # Run attention mechanism with mode='hard', make sure probabilities are hard
   b, t, u, d = 10, 20, 30, 40
   with self.session(use_gpu=True) as sess:
     a = wrapper.LuongMonotonicAttention(
         d,
         random_ops.random_normal((b, t, u)),
         mode='hard')
     # Just feed previous attention as [1, 0, 0, ...]
     attn, unused_state = a(
         random_ops.random_normal((b, d)), array_ops.one_hot([0]*b, t))
     sess.run(variables.global_variables_initializer())
     attn_out = attn.eval()
     # All values should be 0 or 1
     self.assertTrue(np.all(np.logical_or(attn_out == 0, attn_out == 1)))
     # Sum of distributions should be 0 or 1 (0 when all p_choose_i are 0)
     self.assertTrue(np.all(np.logical_or(attn_out.sum(axis=1) == 1,
                                          attn_out.sum(axis=1) == 0)))