本文整理汇总了Python中tensorflow.compat.v1.py_func方法的典型用法代码示例。如果您正苦于以下问题:Python v1.py_func方法的具体用法?Python v1.py_func怎么用?Python v1.py_func使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.py_func方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rouge_2_fscore
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def rouge_2_fscore(predictions, labels, **unused_kwargs):
"""ROUGE-2 F1 score computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge2_fscore: approx rouge-2 f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_2_f_score = tf.py_func(rouge_n, (outputs, labels), tf.float32)
return rouge_2_f_score, tf.constant(1.0)
示例2: bleu_score
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def bleu_score(predictions, labels, **unused_kwargs):
"""BLEU score computation between labels and predictions.
An approximate BLEU scoring method since we do not glue word pieces or
decode the ids and tokenize the output. By default, we use ngram order of 4
and use brevity penalty. Also, this does not have beam search.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
bleu: int, approx bleu score
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
bleu = tf.py_func(compute_bleu, (labels, outputs), tf.float32)
return bleu, tf.constant(1.0)
示例3: make_input_fn_from_generator
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def make_input_fn_from_generator(gen):
"""Use py_func to yield elements from the given generator."""
first_ex = six.next(gen)
flattened = contrib.framework().nest.flatten(first_ex)
types = [t.dtype for t in flattened]
shapes = [[None] * len(t.shape) for t in flattened]
first_ex_list = [first_ex]
def py_func():
if first_ex_list:
example = first_ex_list.pop()
else:
example = six.next(gen)
return contrib.framework().nest.flatten(example)
def input_fn():
flat_example = tf.py_func(py_func, [], types)
_ = [t.set_shape(shape) for t, shape in zip(flat_example, shapes)]
example = contrib.framework().nest.pack_sequence_as(first_ex, flat_example)
return example
return input_fn
示例4: transform_wav_data_op
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def transform_wav_data_op(wav_data_tensor, hparams, jitter_amount_sec):
"""Transforms with audio for data augmentation. Only for training."""
def transform_wav_data(wav_data):
"""Transforms with sox."""
if jitter_amount_sec:
wav_data = audio_io.jitter_wav_data(wav_data, hparams.sample_rate,
jitter_amount_sec)
wav_data = audio_transform.transform_wav_audio(wav_data, hparams)
return [wav_data]
return tf.py_func(
transform_wav_data, [wav_data_tensor],
tf.string,
name='transform_wav_data_op')
示例5: generate_detections
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def generate_detections(self, cls_outputs, box_outputs, indices, classes,
image_id, image_scale, level_index,
min_score_thresh, max_boxes_to_draw,
use_tf=False):
if use_tf:
return _generate_detections_tf(
cls_outputs,
box_outputs,
self._anchors.boxes,
indices,
classes,
image_id,
image_scale,
level_index,
min_score_thresh=min_score_thresh,
max_boxes_to_draw=max_boxes_to_draw)
else:
return tf.py_func(_generate_detections, [
cls_outputs, box_outputs, self._anchors.boxes, indices, classes,
image_id, image_scale, self._num_classes, level_index,
#image_id, image_scale, self._target_classes, level_index,
], [tf.float32, tf.float32, tf.float32, tf.float32])
示例6: parse
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def parse(data_dict):
"""Parse dataset from _data_gen into the same format as sst_binary."""
sentiment = data_dict['label']
sentence = data_dict['sentence']
dense_chars = tf.decode_raw(sentence, tf.uint8)
dense_chars.set_shape((None,))
chars = tfp.math.dense_to_sparse(dense_chars)
if six.PY3:
safe_chr = lambda c: '?' if c >= 128 else chr(c)
else:
safe_chr = chr
to_char = np.vectorize(safe_chr)
chars = tf.SparseTensor(indices=chars.indices,
values=tf.py_func(to_char, [chars.values], tf.string),
dense_shape=chars.dense_shape)
return {'sentiment': sentiment,
'sentence': chars}
示例7: tf_put_text
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def tf_put_text(imgs, texts, text_size=1, text_pos=(0, 30),
text_color=(0, 0, 1)):
"""Adds text to an image tensor."""
def _put_text(imgs, texts):
"""Python function that renders text onto a image."""
result = np.empty_like(imgs)
for i in range(imgs.shape[0]):
text = texts[i]
if isinstance(text, bytes):
text = six.ensure_text(text)
# You may need to adjust text size and position and size.
# If your images are in [0, 255] range replace (0, 0, 1) with (0, 0, 255)
result[i, :, :, :] = cv2.putText(
imgs[i, :, :, :], str(text), text_pos,
cv2.FONT_HERSHEY_COMPLEX, text_size, text_color, 1)
return result
return tf.py_func(_put_text, [imgs, texts], Tout=imgs.dtype)
示例8: print_text
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def print_text(tf_sequences, vocab, use_bpe=False, predict_mode=False):
"""Print text."""
def _print_separator():
if not predict_mode:
tf.logging.info("=" * 80)
print_ops = [tf.py_func(_print_separator, [], [])]
for name, tf_sequence, tf_length, convert2txt in tf_sequences:
def _do_print(n, sequence, lengths, to_txt):
if to_txt:
s = sequence[0][:lengths[0]]
output = id2text(s, vocab, use_bpe=use_bpe)
else:
output = " ".join(sequence[0])
if not predict_mode:
tf.logging.info("%s: %s", n, output)
with tf.control_dependencies(print_ops):
print_ops.append(tf.py_func(
_do_print, [name, tf_sequence, tf_length, convert2txt], []))
with tf.control_dependencies(print_ops):
return tf.py_func(_print_separator, [], [])
示例9: shaped_py_func
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def shaped_py_func(func, inputs, types, shapes, stateful=True, name=None):
"""Wrapper around tf.py_func that adds static shape information to the output.
Args:
func: Python function to call.
inputs: List of input tensors.
types: List of output tensor types.
shapes: List of output tensor shapes.
stateful: Whether or not the python function is stateful.
name: Name of the op.
Returns:
output_tensors: List of output tensors.
"""
output_tensors = tf.py_func(
func=func, inp=inputs, Tout=types, stateful=stateful, name=name)
for t, s in zip(output_tensors, shapes):
t.set_shape(s)
return output_tensors
示例10: bleu_score
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def bleu_score(logits, labels):
"""Approximate BLEU score computation between labels and predictions.
An approximate BLEU scoring method since we do not glue word pieces or
decode the ids and tokenize the output. By default, we use ngram order of 4
and use brevity penalty. Also, this does not have beam search.
Args:
logits: Tensor of size [batch_size, length_logits, vocab_size]
labels: Tensor of size [batch-size, length_labels]
Returns:
bleu: int, approx bleu score
"""
predictions = tf.to_int32(tf.argmax(logits, axis=-1))
# TODO: Look into removing use of py_func
bleu = tf.py_func(compute_bleu, (labels, predictions), tf.float32)
return bleu, tf.constant(1.0)
示例11: rouge_2_fscore
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def rouge_2_fscore(logits, labels):
"""ROUGE-2 F1 score computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
logits: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge2_fscore: approx rouge-2 f1 score.
"""
predictions = tf.to_int32(tf.argmax(logits, axis=-1))
# TODO: Look into removing use of py_func
rouge_2_f_score = tf.py_func(rouge_n, (predictions, labels), tf.float32)
return rouge_2_f_score, tf.constant(1.0)
示例12: rouge_l_fscore
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def rouge_l_fscore(predictions, labels):
"""ROUGE scores computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge_l_fscore: approx rouge-l f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (outputs, labels),
tf.float32)
return rouge_l_f_score, tf.constant(1.0)
示例13: _slow_tensorflow_op
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def _slow_tensorflow_op(self):
"""Returns a TensorFlow op that takes approximately 0.1s to complete."""
def slow_func(v):
time.sleep(0.1)
return v
return tf.py_func(slow_func, [tf.constant(0.)], tf.float32).op
示例14: simulate
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def simulate(self, action):
"""Step the batch of environments.
The results of the step can be accessed from the variables defined below.
Args:
action: Tensor holding the batch of actions to apply.
Returns:
Operation.
"""
with tf.name_scope("environment/simulate"):
if action.dtype in (tf.float16, tf.float32, tf.float64):
action = tf.check_numerics(action, "action")
def step(action):
step_response = self._batch_env.step(action)
# Current env doesn't return `info`, but EnvProblem does.
# TODO(afrozm): The proper way to do this is to make T2TGymEnv return
# an empty info return value.
if len(step_response) == 3:
(observ, reward, done) = step_response
else:
(observ, reward, done, _) = step_response
return (observ, reward.astype(np.float32), done)
observ, reward, done = tf.py_func(
step, [action],
[self.observ_dtype, tf.float32, tf.bool], name="step")
reward = tf.check_numerics(reward, "reward")
reward.set_shape((len(self),))
done.set_shape((len(self),))
with tf.control_dependencies([self._observ.assign(observ)]):
return tf.identity(reward), tf.identity(done)
示例15: _reset_non_empty
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import py_func [as 别名]
def _reset_non_empty(self, indices):
"""Reset the batch of environments.
Args:
indices: The batch indices of the environments to reset; defaults to all.
Returns:
Batch tensor of the new observations.
"""
observ = tf.py_func(
self._batch_env.reset, [indices], self.observ_dtype, name="reset")
observ.set_shape(indices.get_shape().concatenate(self.observ_shape))
with tf.control_dependencies([
tf.scatter_update(self._observ, indices, observ)]):
return tf.identity(observ)