本文整理汇总了Python中tensorflow.map_fn方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.map_fn方法的具体用法?Python tensorflow.map_fn怎么用?Python tensorflow.map_fn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.map_fn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_noise
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def add_noise(ids, sequence_length):
"""Wraps add_noise_python for a batch of tensors."""
def _add_noise_single(ids, sequence_length):
noisy_ids = add_noise_python(ids[:sequence_length])
noisy_sequence_length = len(noisy_ids)
ids[:noisy_sequence_length] = noisy_ids
ids[noisy_sequence_length:] = 0
return ids, np.int32(noisy_sequence_length)
noisy_ids, noisy_sequence_length = tf.map_fn(
lambda x: tf.py_func(_add_noise_single, x, [ids.dtype, tf.int32]),
[ids, sequence_length],
dtype=[ids.dtype, tf.int32],
back_prop=False)
noisy_ids.set_shape(ids.get_shape())
noisy_sequence_length.set_shape(sequence_length.get_shape())
return noisy_ids, noisy_sequence_length
# Step 3
示例2: preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def preprocess(self, inputs):
"""Feature-extractor specific preprocessing.
See base class.
Args:
inputs: a [batch, height_in, width_in, channels] float tensor representing
a batch of images with values between 0 and 255.0.
Returns:
preprocessed_inputs: a [batch, height_out, width_out, channels] float
tensor representing a batch of images.
Raises:
ValueError: if inputs tensor does not have type tf.float32
"""
if inputs.dtype is not tf.float32:
raise ValueError('`preprocess` expects a tf.float32 tensor')
with tf.name_scope('Preprocessor'):
# TODO: revisit whether to always use batch size as the number of
# parallel iterations vs allow for dynamic batching.
resized_inputs = tf.map_fn(self._image_resizer_fn,
elems=inputs,
dtype=tf.float32)
return self._feature_extractor.preprocess(resized_inputs)
示例3: get_logits
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def get_logits(self, x):
if x.name in self._logits_dict:
return self._logits_dict[x.name]
x = tf.map_fn(tf.image.per_image_standardization, x)
logits = self._model_fn(
{
"inputs": x
},
None,
"attack",
params=self._params,
config=self._config)
self._logits_dict[x.name] = logits
return tf.squeeze(logits)
示例4: main
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def main():
args = parse_args()
with tf.Session(graph=tf.Graph()) as session:
input_var = tf.placeholder(
tf.uint8, (None, 128, 64, 3), name="images")
image_var = tf.map_fn(
lambda x: _preprocess(x), tf.cast(input_var, tf.float32),
back_prop=False)
factory_fn = _network_factory()
features, _ = factory_fn(image_var, reuse=None)
features = tf.identity(features, name="features")
saver = tf.train.Saver(slim.get_variables_to_restore())
saver.restore(session, args.checkpoint_in)
output_graph_def = tf.graph_util.convert_variables_to_constants(
session, tf.get_default_graph().as_graph_def(),
[features.name.split(":")[0]])
with tf.gfile.GFile(args.graphdef_out, "wb") as file_handle:
file_handle.write(output_graph_def.SerializeToString())
示例5: from_config
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def from_config(cls, config, custom_objects=None):
model = super(CalibratedLinear, cls).from_config(
config, custom_objects=custom_objects)
try:
model_config = tf.keras.utils.deserialize_keras_object(
config.get('model_config'), custom_objects=custom_objects)
premade_lib.verify_config(model_config)
model.model_config = model_config
except ValueError:
logging.warning(
'Could not load model_config. Constructing model without it: %s',
str(config.get('model_config')))
return model
# TODO: add support for tf.map_fn and inputs of shape (B, ?, input_dim)
# as well as non-ragged inputs using padding/mask.
示例6: _encoded_image_string_tensor_input_placeholder
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def _encoded_image_string_tensor_input_placeholder():
"""Returns input that accepts a batch of PNG or JPEG strings.
Returns:
a tuple of input placeholder and the output decoded images.
"""
batch_image_str_placeholder = tf.placeholder(
dtype=tf.string,
shape=[None],
name='encoded_image_string_tensor')
def decode(encoded_image_string_tensor):
image_tensor = tf.image.decode_image(encoded_image_string_tensor,
channels=3)
image_tensor.set_shape((None, None, 3))
return image_tensor
return (batch_image_str_placeholder,
tf.map_fn(
decode,
elems=batch_image_str_placeholder,
dtype=tf.uint8,
parallel_iterations=32,
back_prop=False))
示例7: _preprocess_frames
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def _preprocess_frames(self, example, indices):
"""Instantiates the ops used to preprocess the frames data."""
frames = tf.concat(example['frames'], axis=0)
frames = tf.gather(frames, indices, axis=1)
frames = tf.map_fn(
_convert_frame_data, tf.reshape(frames, [-1]),
dtype=tf.float32, back_prop=False)
dataset_image_dimensions = tuple(
[self._dataset_info.frame_size] * 2 + [_NUM_CHANNELS])
frames = tf.reshape(
frames, (-1, self._example_size) + dataset_image_dimensions)
if (self._custom_frame_size and
self._custom_frame_size != self._dataset_info.frame_size):
frames = tf.reshape(frames, (-1,) + dataset_image_dimensions)
new_frame_dimensions = (self._custom_frame_size,) * 2 + (_NUM_CHANNELS,)
frames = tf.image.resize_bilinear(
frames, new_frame_dimensions[:2], align_corners=True)
frames = tf.reshape(
frames, (-1, self._example_size) + new_frame_dimensions)
return frames
示例8: _build
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def _build(self, X):
"""Build the graph of this layer."""
n_samples, input_shape = self._get_X_dims(X)
Wdim = input_shape + [self.output_dim]
W_init = initialise_weights(Wdim, self.init_fn)
W = tf.Variable(W_init, name="W_map")
summary_histogram(W)
# Tiling W is much faster than mapping (tf.map_fn) the matmul
Net = tf.matmul(X, _tile2samples(n_samples, W))
# Regularizers
penalty = self.l2 * tf.nn.l2_loss(W) + self.l1 * _l1_loss(W)
# Optional Bias
if self.use_bias is True:
b_init = initialise_weights((1, self.output_dim), self.init_fn)
b = tf.Variable(b_init, name="b_map")
summary_histogram(b)
Net += b
penalty += self.l2 * tf.nn.l2_loss(b) + self.l1 * _l1_loss(b)
return Net, penalty
示例9: image_serving_input_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def image_serving_input_fn():
"""Serving input fn for raw images."""
def _preprocess_image(image_bytes):
"""Preprocess a single raw image."""
image = resnet_preprocessing.preprocess_image(
image_bytes=image_bytes, is_training=False)
return image
image_bytes_list = tf.placeholder(
shape=[None],
dtype=tf.string,
)
images = tf.map_fn(
_preprocess_image, image_bytes_list, back_prop=False, dtype=tf.float32)
return tf.estimator.export.ServingInputReceiver(
images, {'image_bytes': image_bytes_list})
示例10: test_input_with_unknown_leading_dimension
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def test_input_with_unknown_leading_dimension(self):
def get_random_shape_input():
# Returns a Tensor of shape (?, 6)
return tf.map_fn(lambda x: x * tf.random.normal([6]),
test_utils.get_tensor_with_random_shape())
# Validate the premise of the test.
assert get_random_shape_input().shape.as_list() == [None, 6]
test_data = self.run_one_to_many_encode_decode(
self.default_encoding_stage(), get_random_shape_input)
self.common_asserts_for_test_data(test_data)
encoded_shape = test_data.encoded_x[
kashin.KashinHadamardEncodingStage.ENCODED_VALUES_KEY].shape
self.assertEqual(test_data.x.shape[0], encoded_shape[0])
self.assertEqual(8, encoded_shape[1])
示例11: test_input_with_unknown_leading_dimension
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def test_input_with_unknown_leading_dimension(self):
def get_random_shape_input():
# Returns a Tensor of shape (?, 6)
return tf.map_fn(lambda x: x * tf.random.normal([6]),
test_utils.get_tensor_with_random_shape())
# Validate the premise of the test.
assert get_random_shape_input().shape.as_list() == [None, 6]
test_data = self.run_one_to_many_encode_decode(
self.default_encoding_stage(), get_random_shape_input)
self.common_asserts_for_test_data(test_data)
encoded_shape = test_data.encoded_x[
stages_impl.HadamardEncodingStage.ENCODED_VALUES_KEY].shape
self.assertEqual(test_data.x.shape[0], encoded_shape[0])
self.assertEqual(8, encoded_shape[1])
示例12: build_detection
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def build_detection(self):
self.embeds = self.get_image_embedding(self.search_images, reuse=True)
with tf.variable_scope('detection'):
def _translation_match(x, z):
x = tf.expand_dims(x, 0) # [batch, in_height, in_width, in_channels]
z = tf.expand_dims(z, -1) # [filter_height, filter_width, in_channels, out_channels]
return tf.nn.conv2d(x, z, strides=[1, 1, 1, 1], padding='VALID', name='translation_match')
output = tf.map_fn(
lambda x: _translation_match(x[0], x[1]),
(self.embeds, self.templates), dtype=self.embeds.dtype) # of shape [3, 1, 17, 17, 1]
output = tf.squeeze(output, [1, 4]) # of shape e.g. [3, 17, 17]
bias = tf.get_variable('biases', [1],
dtype=tf.float32,
initializer=tf.constant_initializer(0.0, dtype=tf.float32),
trainable=False)
response = self.model_config['adjust_response_config']['scale'] * output + bias
self.response = response
示例13: build_detection
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def build_detection(self, reuse=False):
with tf.variable_scope('detection', reuse=reuse):
def _translation_match(x, z): # translation match for one example within a batch
x = tf.expand_dims(x, 0) # [1, in_height, in_width, in_channels]
z = tf.expand_dims(z, -1) # [filter_height, filter_width, in_channels, 1]
return tf.nn.conv2d(x, z, strides=[1, 1, 1, 1], padding='VALID', name='translation_match')
output = tf.map_fn(lambda x: _translation_match(x[0], x[1]),
(self.instance_embeds, self.templates),
dtype=self.instance_embeds.dtype)
output = tf.squeeze(output, [1, 4]) # of shape e.g., [8, 15, 15]
# Adjust score, this is required to make training possible.
config = self.model_config['adjust_response_config']
bias = tf.get_variable('biases', [1],
dtype=tf.float32,
initializer=tf.constant_initializer(0.0, dtype=tf.float32),
trainable=config['train_bias'])
response = config['scale'] * output + bias
self.response = response
示例14: _vectorised_get_cum_graph_size
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def _vectorised_get_cum_graph_size(nodes, graph_sizes):
"""
Takes a list of node ids and graph sizes ordered by segment ID and returns the
number of nodes contained in graphs with smaller segment ID.
:param nodes: List of node ids of shape (nodes)
:param graph_sizes: List of graph sizes (i.e. tf.math.segment_sum(tf.ones_like(I), I) where I are the
segment IDs).
:return: A list of shape (nodes) where each entry corresponds to the number of nodes contained in graphs
with smaller segment ID for each node.
"""
def get_cum_graph_size(node):
cum_graph_sizes = tf.cumsum(graph_sizes, exclusive=True)
indicator_if_smaller = tf.cast(node - cum_graph_sizes >= 0, tf.int32)
graph_id = tf.reduce_sum(indicator_if_smaller) - 1
return tf.cumsum(graph_sizes, exclusive=True)[graph_id]
return tf.map_fn(get_cum_graph_size, nodes)
示例15: _tf_example_input_placeholder
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import map_fn [as 别名]
def _tf_example_input_placeholder():
"""Returns input that accepts a batch of strings with tf examples.
Returns:
a tuple of input placeholder and the output decoded images.
"""
batch_tf_example_placeholder = tf.placeholder(
tf.string, shape=[None], name='tf_example')
def decode(tf_example_string_tensor):
tensor_dict = tf_example_decoder.TfExampleDecoder().decode(
tf_example_string_tensor)
image_tensor = tensor_dict[fields.InputDataFields.image]
return image_tensor
return (batch_tf_example_placeholder,
tf.map_fn(decode,
elems=batch_tf_example_placeholder,
dtype=tf.uint8,
parallel_iterations=32,
back_prop=False))