本文整理汇总了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
示例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
示例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
示例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
示例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]))
示例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)
示例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
示例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