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


Python categorical.Categorical方法代碼示例

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


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

示例1: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
        """Gets a sample for one step."""
        del time, state  # unused by sample_fn
        # Outputs are logits, we sample instead of argmax (greedy).
        if not isinstance(outputs, ops.Tensor):
            raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                            type(outputs))
        if self._softmax_temperature is None:
            logits = outputs
        else:
            logits = outputs / self._softmax_temperature

        sample_id_sampler = categorical.Categorical(logits=logits)
        sample_ids = sample_id_sampler.sample(seed=self._seed)

        return sample_ids 
開發者ID:qkaren,項目名稱:Counterfactual-StoryRW,代碼行數:18,代碼來源:tf_helpers.py

示例2: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
        """Gets a sample for one step."""
        del time, state  # unused by sample_fn
        # Outputs are logits, we sample from the top_k candidates
        if not isinstance(outputs, tf.Tensor):
            raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                            type(outputs))
        if self._softmax_temperature is None:
            logits = outputs
        else:
            logits = outputs / self._softmax_temperature

        logits = _top_k_logits(logits, k=self._top_k)

        sample_id_sampler = categorical.Categorical(logits=logits)
        sample_ids = sample_id_sampler.sample(seed=self._seed)

        return sample_ids 
開發者ID:qkaren,項目名稱:Counterfactual-StoryRW,代碼行數:20,代碼來源:rnn_decoder_helpers.py

示例3: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
    """sample for SampleEmbeddingHelper."""
    del time, state  # unused by sample_fn
    # Outputs are logits, we sample instead of argmax (greedy).
    if not isinstance(outputs, ops.Tensor):
      raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                      type(outputs))
    if self._softmax_temperature is None:
      logits = outputs
    else:
      logits = outputs / self._softmax_temperature

    sample_id_sampler = categorical.Categorical(logits=logits)
    sample_ids = sample_id_sampler.sample(seed=self._seed)

    return sample_ids 
開發者ID:NVIDIA,項目名稱:OpenSeq2Seq,代碼行數:18,代碼來源:helper.py

示例4: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
    """sample for SampleEmbeddingHelper."""
    del time, state  # unused by sample_fn
    # Outputs are logits, we sample instead of argmax (greedy).
    if not isinstance(outputs, ops.Tensor):
      raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                      type(outputs))
    if self._softmax_temperature is None:
      logits = outputs
    else:
      #logits = outputs / self._softmax_temperature
      logits = math_ops.divide(outputs, self._softmax_temperature)

    sample_id_sampler = categorical.Categorical(logits=logits)
    sample_ids = sample_id_sampler.sample(seed=self._seed)

    return sample_ids 
開發者ID:vsuthichai,項目名稱:paraphraser,代碼行數:19,代碼來源:sample_embedding_helper.py

示例5: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
    with ops.name_scope(name, "ScheduledEmbeddingTrainingHelperSample",
                        [time, outputs, state]):
      # Return -1s where we did not sample, and sample_ids elsewhere
      select_sample_noise = random_ops.random_uniform(
          [self.batch_size], seed=self._scheduling_seed)
      select_sample = (self._sampling_probability > select_sample_noise)
      sample_id_sampler = categorical.Categorical(logits=outputs)
      return array_ops.where(
          select_sample,
          sample_id_sampler.sample(seed=self._seed),
          array_ops.tile([-1], [self.batch_size])) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:14,代碼來源:helper.py

示例6: dist

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def dist(self):
        return categorical.Categorical(logits=self._logits) 
開發者ID:gd-zhang,項目名稱:noisy-K-FAC,代碼行數:4,代碼來源:loss_functions.py

示例7: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
    if self._softmax_temperature is not None:
      outputs = outputs / self._softmax_temperature

    sampler = categorical.Categorical(logits=outputs)
    sample_ids = sampler.sample()
    return sample_ids 
開發者ID:ChrisCummins,項目名稱:clgen,代碼行數:9,代碼來源:helper.py

示例8: sample

# 需要導入模塊: from tensorflow.python.ops.distributions import categorical [as 別名]
# 或者: from tensorflow.python.ops.distributions.categorical import Categorical [as 別名]
def sample(self, time, outputs, state, name=None):
        """sample for SyntacticGreedyEmbeddingHelper."""
        del time, state  # unused by sample_fn
        # Outputs are logits, use argmax to get the most probable id
        if not isinstance(outputs, ops.Tensor):
            raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                            type(outputs))

        # Mask outputs to reduce candidates to syntatically correct ones.
        def mask_output(outputs, end_token):
            if len(self.previous_tokens) == 0:  # when there is no previous token, skip masking.
                mask = np.zeros(outputs.shape, dtype=outputs.dtype)
                mask[:, self.dsl_syntax.token2int['DEF']] = 1
                return mask
            tokens = np.stack(self.previous_tokens, axis=1)
            masks = []
            for i in range(outputs.shape[0]):
                if tokens[i][-1] == end_token:
                    next_tokens = [end_token]
                else:
                    try:
                        p_str = self.dsl_syntax.intseq2str(tokens[i])
                        next_tokens_with_counts = self.dsl_syntax.get_next_candidates(
                            '{}'.format(p_str))
                        next_tokens = [t[0] for t in next_tokens_with_counts
                                       if t[1] <= self.max_program_len - len(tokens[i])]
                    except:
                        # TODO: this code rarely cause syntax error, which
                        # should not happen. We should fix this in the future.
                        next_tokens = [t for t in range(len(self.dsl_syntax.int2token))]
                    else:
                        next_tokens = [self.dsl_syntax.token2int[t] for t in next_tokens]
                mask = np.zeros([outputs.shape[1]], dtype=outputs.dtype)
                for t in next_tokens:
                    mask[t] = 1
                masks.append(mask)
            return np.stack(masks, axis=0)
        masks = tf.py_func(mask_output, [outputs, self._end_token], tf.float32)
        masks.set_shape(outputs.get_shape())
        masked_outputs = tf.exp(outputs) * masks
        masked_probs = masked_outputs / \
            tf.reduce_sum(masked_outputs, axis=1, keep_dims=True)
        sample_id_sampler = categorical.Categorical(probs=masked_probs)
        sample_ids = sample_id_sampler.sample(seed=self._seed)

        def add_sample_ids(sample_ids, masked_probs, masks):
            self.previous_tokens.append(sample_ids)
            self.previous_probs.append(masked_probs)
            self.previous_masks.append(masks)

            return sample_ids
        new_sample_ids = tf.py_func(add_sample_ids, [sample_ids, masked_probs, masks], tf.int32)
        new_sample_ids.set_shape(sample_ids.get_shape())
        return new_sample_ids 
開發者ID:shaohua0116,項目名稱:demo2program,代碼行數:56,代碼來源:seq2seq_helper.py


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