本文整理汇总了Python中tensorflow.py_function方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.py_function方法的具体用法?Python tensorflow.py_function怎么用?Python tensorflow.py_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.py_function方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: yolo_nms
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def yolo_nms(outputs, anchors, masks, num_classes, iou_threshold=0.6, score_threshold=0.15):
boxes, confs, classes = [], [], []
for o in outputs:
boxes.append(tf.reshape(o[0], (tf.shape(o[0])[0], -1, tf.shape(o[0])[-1])))
confs.append(tf.reshape(o[1], (tf.shape(o[0])[0], -1, tf.shape(o[1])[-1])))
classes.append(tf.reshape(o[2], (tf.shape(o[0])[0], -1, tf.shape(o[2])[-1])))
boxes = tf.concat(boxes, axis=1)
confs = tf.concat(confs, axis=1)
class_probs = tf.concat(classes, axis=1)
box_scores = confs * class_probs
mask = box_scores >= score_threshold
mask = tf.reduce_any(mask, axis=-1)
class_boxes = tf.boolean_mask(boxes, mask)
class_boxes = tf.reshape(class_boxes, (tf.shape(boxes)[0], -1, 4))
class_box_scores = tf.boolean_mask(box_scores, mask)
class_box_scores = tf.reshape(class_box_scores, (tf.shape(boxes)[0], -1, num_classes))
class_boxes, class_box_scores = tf.py_function(func=batched_nms,
inp=[class_boxes, class_box_scores, num_classes, iou_threshold],
Tout=[tf.float32, tf.float32])
classes = tf.argmax(class_box_scores, axis=-1)
return class_boxes, class_box_scores, classes
示例2: step
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def step(self, actions, name=None):
"""take 1-step in all environments.
:param tf.Tensor action: float32[batch_size, dim_action]
:param name: Operator名
:rtype: (tf.Tensor, tf.Tensor, tf.Tensor)
:return:
(obs, reward, done)
obs = [batch_size, dim_obs]
reward = [batch_size]
done = [batch_size]
reach_limit = [batch_size] : whether each environment reached time limit or not.
"""
assert isinstance(actions, tf.Tensor)
# with tf.variable_scope(name, default_name="MultiStep"):
obs, reward, done = tf.py_function(
func=self.py_step,
inp=[actions],
Tout=[tf.float32, tf.float32, tf.float32],
name="py_step")
obs.set_shape((self.batch_size,) + self.observation_shape)
reward.set_shape((self.batch_size,))
done.set_shape((self.batch_size,))
return obs, reward, done, None
示例3: _tokenize_tensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def _tokenize_tensor(self, text):
"""Tokenizes a tensor.
When not overriden, this default implementation calls the string-based
tokenization.
Args:
text: A 1-D string ``tf.Tensor``.
Returns:
A 1-D string ``tf.Tensor``.
"""
def _python_wrapper(string_t):
string = tf.compat.as_text(string_t.numpy())
tokens = self._tokenize_string(string)
return tf.constant(tokens, dtype=tf.string)
tokens = tf.py_function(_python_wrapper, [text], tf.string)
tokens.set_shape([None])
return tokens
示例4: _detokenize_tensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def _detokenize_tensor(self, tokens):
"""Detokenizes tokens.
When not overriden, this default implementation calls the string-based
detokenization.
Args:
tokens: A 1-D ``tf.Tensor``.
Returns:
A 0-D string ``tf.Tensor``.
"""
def _python_wrapper(tokens_t):
tokens = [tf.compat.as_text(s) for s in tokens_t.numpy()]
string = self._detokenize_string(tokens)
return tf.constant(string)
text = tf.py_function(_python_wrapper, [tokens], tf.string)
text.set_shape([])
return text
示例5: test_shutdown_while_in_call
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def test_shutdown_while_in_call(self, dim, batched):
address = self.get_unix_address()
server = ops.Server([address])
is_waiting = threading.Event()
@tf.function(input_signature=[tf.TensorSpec(dim, tf.int32)])
def foo(x):
tf.py_function(is_waiting.set, [], [])
tf.py_function(time.sleep, [1], [])
return x + 1
server.bind(foo, batched=batched)
server.start()
client = ops.Client(address)
with futures.ThreadPoolExecutor(max_workers=1) as executor:
f = executor.submit(client.foo, 42)
is_waiting.wait()
server.shutdown()
with self.assertRaisesRegexp(tf.errors.UnavailableError, 'server closed'):
f.result()
示例6: test_not_fully_specified_outputs2
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def test_not_fully_specified_outputs2(self):
address = self.get_unix_address()
server = ops.Server([address])
@tf.function(input_signature=[tf.TensorSpec([1], tf.int32)])
def foo(x):
result, = tf.py_function(lambda x: x, [x], [tf.int32])
result.set_shape([None])
return result
server.bind(foo, batched=True)
server.start()
client = ops.Client(address)
self.assertAllEqual(42, client.foo(42))
server.shutdown()
示例7: masked_q_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def masked_q_loss(data, y_pred):
"""Computes the MSE between the Q-values of the actions that were taken and the cumulative discounted
rewards obtained after taking those actions. Updates trace priorities if using PrioritizedExperienceReplay.
"""
action_batch, target_qvals = data[:, 0], data[:, 1]
seq = tf.cast(tf.range(0, tf.shape(action_batch)[0]), tf.int32)
action_idxs = tf.transpose(tf.stack([seq, tf.cast(action_batch, tf.int32)]))
qvals = tf.gather_nd(y_pred, action_idxs)
if isinstance(self.memory, memory.PrioritizedExperienceReplay):
def update_priorities(_qvals, _target_qvals, _traces_idxs):
"""Computes the TD error and updates memory priorities."""
td_error = np.abs((_target_qvals - _qvals).numpy())
_traces_idxs = (tf.cast(_traces_idxs, tf.int32)).numpy()
self.memory.update_priorities(_traces_idxs, td_error)
return _qvals
qvals = tf.py_function(func=update_priorities, inp=[qvals, target_qvals, data[:,2]], Tout=tf.float32)
return tf.keras.losses.mse(qvals, target_qvals)
示例8: set_tags
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def set_tags(self, **tags):
keys, values = zip(*sorted(tags.items(), key=lambda x: x[0]))
def inner(*values):
parsed = []
for index, value in enumerate(values):
if value.dtype == tf.string:
parsed.append(value.numpy().decode('utf-8'))
elif value.dtype in (tf.float32, tf.float64):
parsed.append(float(value.numpy()))
elif value.dtype in (tf.int32, tf.int64):
parsed.append(int(value.numpy()))
else:
raise NotImplementedError(value.dtype)
tags = dict(zip(keys, parsed))
return self._metrics.set_tags(**tags)
# String tensors in tf.py_function are only supported on CPU.
with tf.device('/cpu:0'):
return tf.py_function(inner, values, [], 'set_tags')
示例9: tf_parse_line
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def tf_parse_line(line, data_dir, split_names):
line_split = tf.strings.split(line, ' ')
audio_fn = line_split[0]
transcription = tf.py_function(
lambda x: b' '.join(x.numpy()).decode('utf8'),
inp=[line_split[1:]],
Tout=tf.string)
speaker_id, chapter_id, _ = tf.unstack(tf.strings.split(audio_fn, '-'), 3)
all_fps = tf.map_fn(
lambda split_name: tf.strings.join([data_dir, split_name, speaker_id, chapter_id, audio_fn], '/') + '.flac',
tf.constant(split_names))
audio_filepath_idx = tf.where(
tf.map_fn(tf_file_exists, all_fps, dtype=tf.bool))[0][0]
audio_filepath = all_fps[audio_filepath_idx]
audio, sr = tf_load_audio(audio_filepath)
return audio, sr, transcription
示例10: _build_mwf_output_waveform
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def _build_mwf_output_waveform(self):
""" Perform separation with multichannel Wiener Filtering using Norbert.
Note: multichannel Wiener Filtering is not coded in Tensorflow and thus
may be quite slow.
:returns: dictionary of separated waveforms (key: instrument name,
value: estimated waveform of the instrument)
"""
import norbert # pylint: disable=import-error
output_dict = self.model_outputs
x = self.stft_feature
v = tf.stack(
[
pad_and_reshape(
output_dict[f'{instrument}_spectrogram'],
self._frame_length,
self._F)[:tf.shape(x)[0], ...]
for instrument in self._instruments
],
axis=3)
input_args = [v, x]
stft_function = tf.py_function(
lambda v, x: norbert.wiener(v.numpy(), x.numpy()),
input_args,
tf.complex64),
return {
instrument: self._inverse_stft(stft_function[0][:, :, :, k])
for k, instrument in enumerate(self._instruments)
}
示例11: auc
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def auc(y, p):
return tf.py_function(roc_auc_score, (y, p), tf.double)
示例12: load_textline_dataset
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def load_textline_dataset(file_pattern, size):
dataset = tf.data.TextLineDataset(file_pattern)
return dataset.map(lambda x: tf.py_function(func=decode_line, inp=[x, size], Tout=(tf.float32, tf.float32)))
示例13: test_minimize_callback
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def test_minimize_callback(self):
"""Callback should run at the end of every iteration."""
variables = np.random.uniform(low=-1.0, high=1.0, size=[1])
saved_objective = [None]
saved_iteration = [None]
iteration_count = [0]
def callback(iteration, objective_value, variables):
del variables
def save(iteration, objective_value):
saved_objective[0] = objective_value
saved_iteration[0] = iteration
iteration_count[0] += 1
return tf.py_function(save, [iteration, objective_value], [])
final_objective, _ = levenberg_marquardt.minimize(
lambda x: x, variables, max_iterations=5, callback=callback)
final_saved_objective = tf.py_function(lambda: saved_objective[0], [],
tf.float64)
final_saved_iteration = tf.py_function(lambda: saved_iteration[0], [],
tf.int32)
final_iteration_count = tf.py_function(lambda: iteration_count[0], [],
tf.int32)
with self.subTest(name="objective_value"):
self.assertAllClose(final_objective, final_saved_objective)
with self.subTest(name="iterations"):
self.assertAllEqual(final_saved_iteration, final_iteration_count)
示例14: _map_function
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def _map_function(self, images_path, masks_path):
image, mask = self._parse_data(images_path, masks_path)
def _augmentation_func(image_f, mask_f):
if self.augment:
if self.compose:
image_f, mask_f = self._corrupt_brightness(image_f, mask_f)
image_f, mask_f = self._corrupt_contrast(image_f, mask_f)
image_f, mask_f = self._corrupt_saturation(image_f, mask_f)
image_f, mask_f = self._crop_random(image_f, mask_f)
image_f, mask_f = self._flip_left_right(image_f, mask_f)
else:
options = [self._corrupt_brightness,
self._corrupt_contrast,
self._corrupt_saturation,
self._crop_random,
self._flip_left_right]
augment_func = random.choice(options)
image_f, mask_f = augment_func(image_f, mask_f)
if self.one_hot_encoding:
if self.palette is None:
raise ValueError('No Palette for one-hot encoding specified in the data loader! \
please specify one when initializing the loader.')
image_f, mask_f = self._one_hot_encode(image_f, mask_f)
image_f, mask_f = self._resize_data(image_f, mask_f)
return image_f, mask_f
return tf.py_function(_augmentation_func, [image, mask], [tf.float32, tf.uint8])
示例15: tf_encode
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import py_function [as 别名]
def tf_encode(self, feature, target):
return tf.py_function(self.encode, [feature, target], [tf.int64, tf.int64])