本文整理汇总了Python中tensorflow.compat.v1.to_int64方法的典型用法代码示例。如果您正苦于以下问题:Python v1.to_int64方法的具体用法?Python v1.to_int64怎么用?Python v1.to_int64使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.to_int64方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: argmax_with_score
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def argmax_with_score(logits, axis=None):
"""Argmax along with the value."""
axis = axis or len(logits.get_shape()) - 1
predictions = tf.argmax(logits, axis=axis)
logits_shape = shape_list(logits)
prefix_shape, vocab_size = logits_shape[:-1], logits_shape[-1]
prefix_size = 1
for d in prefix_shape:
prefix_size *= d
# Flatten to extract scores
flat_logits = tf.reshape(logits, [prefix_size, vocab_size])
flat_predictions = tf.reshape(predictions, [prefix_size])
flat_indices = tf.stack(
[tf.range(tf.to_int64(prefix_size)),
tf.to_int64(flat_predictions)],
axis=1)
flat_scores = tf.gather_nd(flat_logits, flat_indices)
# Unflatten
scores = tf.reshape(flat_scores, prefix_shape)
return predictions, scores
示例2: _get_action_id
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def _get_action_id(extended_indices, action_types, output_vocab_size,
model_config):
"""Returns action_id tensor."""
# This initial value will be broadcast to the length of decode_steps.
action_ids = tf.constant(0, dtype=tf.int64)
for action_type_range in _get_action_types_to_range(output_vocab_size,
model_config):
is_type = tf.equal(
tf.constant(action_type_range.action_type, dtype=tf.int64),
action_types)
# For each timestep, exactly one of the action_type_ranges will be added,
# so this sum will populate each entry on exactly one iteration.
action_ids += (
tf.to_int64(is_type) *
(extended_indices - action_type_range.start_index))
return action_ids
示例3: tensors_to_item
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def tensors_to_item(self, keys_to_tensors):
"""Maps the given dictionary of tensors to a concatenated list of bboxes.
Args:
keys_to_tensors: a mapping of TF-Example keys to parsed tensors.
Returns:
[time, num_boxes, 4] tensor of bounding box coordinates, in order
[y_min, x_min, y_max, x_max]. Whether the tensor is a SparseTensor
or a dense Tensor is determined by the return_dense parameter. Empty
positions in the sparse tensor are filled with -1.0 values.
"""
sides = []
for key in self._full_keys:
value = keys_to_tensors[key]
expanded_dims = tf.concat(
[tf.to_int64(tf.shape(value)),
tf.constant([1], dtype=tf.int64)], 0)
side = tf.sparse_reshape(value, expanded_dims)
sides.append(side)
bounding_boxes = tf.sparse_concat(2, sides)
if self._return_dense:
bounding_boxes = tf.sparse_tensor_to_dense(
bounding_boxes, default_value=self._default_value)
return bounding_boxes
示例4: compute_lengths
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def compute_lengths(symbols_list, eos_symbol, name=None,
dtype=tf.int64):
"""Computes sequence lengths given end-of-sequence symbol.
Args:
symbols_list: list of [batch_size] tensors of symbols (e.g. integers).
eos_symbol: end of sequence symbol (e.g. integer).
name: name for the name scope of this op.
dtype: type of symbols, default: tf.int64.
Returns:
Tensor [batch_size] of lengths of sequences.
"""
with tf.name_scope(name, 'compute_lengths'):
max_len = len(symbols_list)
eos_symbol_ = tf.constant(eos_symbol, dtype=dtype)
# Array with max_len-time where we have EOS, 0 otherwise. Maximum of this is
# the first EOS in that example.
ends = [tf.constant(max_len - i, dtype=tf.int64)
* tf.to_int64(tf.equal(s, eos_symbol_))
for i, s in enumerate(symbols_list)]
# Lengths of sequences, or max_len for sequences that didn't have EOS.
# Note: examples that don't have EOS will have max value of 0 and value of
# max_len+1 in lens_.
lens_ = max_len + 1 - tf.reduce_max(tf.stack(ends, 1), axis=1)
# For examples that didn't have EOS decrease max_len+1 to max_len as the
# length.
lens = tf.subtract(lens_, tf.to_int64(tf.equal(lens_, max_len + 1)))
return tf.stop_gradient(tf.reshape(lens, [-1]))
示例5: infer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def infer(self, features, *args, **kwargs):
"""Produce predictions from the model by running it."""
del args, kwargs
if "targets" not in features:
if "infer_targets" in features:
targets_shape = common_layers.shape_list(features["infer_targets"])
elif "inputs" in features:
targets_shape = common_layers.shape_list(features["inputs"])
targets_shape[1] = self.hparams.video_num_target_frames
else:
raise ValueError("no inputs are given.")
features["targets"] = tf.zeros(targets_shape, dtype=tf.float32)
output, _ = self(features) # pylint: disable=not-callable
if not isinstance(output, dict):
output = {"targets": output}
x = output["targets"]
if self.is_per_pixel_softmax:
x_shape = common_layers.shape_list(x)
x = tf.reshape(x, [-1, x_shape[-1]])
x = tf.argmax(x, axis=-1)
x = tf.reshape(x, x_shape[:-1])
else:
x = tf.squeeze(x, axis=-1)
x = tf.to_int64(tf.round(x))
output["targets"] = x
if self.hparams.reward_prediction:
output["target_reward"] = tf.argmax(output["target_reward"], axis=-1)
# only required for decoding.
output["outputs"] = output["targets"]
output["scores"] = output["targets"]
return output
示例6: index_last_dim_with_indices
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def index_last_dim_with_indices(x, indices):
"""Use indices to index into the last axis of x.
This can be useful for recovering the actual probabilities of a sample from a
probability distribution.
Args:
x: Tensor, n-d.
indices: Tensor, (n-1)-d, where the dimension sizes match the first (n-1)
dimensions of x. The values of indices will be used to index into the last
axis of x.
Returns:
Tensor, (n-1)-d.
"""
assert len(x.shape) == len(indices.shape) + 1
x_shape = shape_list(x)
vocab_size = x_shape[-1]
flat_x = tf.reshape(x, [list_product(x_shape[:-1]), vocab_size])
flat_indices = tf.reshape(indices, [list_product(x_shape[:-1])])
idx = tf.stack(
[
tf.range(tf.to_int64(shape_list(flat_indices)[0])),
tf.to_int64(flat_indices)
],
axis=1)
flat_x_idx = tf.gather_nd(flat_x, idx)
x_idx = tf.reshape(flat_x_idx, x_shape[:-1])
return x_idx
示例7: preprocess_example
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def preprocess_example(self, example, mode, _):
# Resize from usual size ~1350x60 to 90x4 in this test.
img = example["inputs"]
img = tf.to_int64(
tf.image.resize_images(img, [90, 4], tf.image.ResizeMethod.AREA))
img = tf.image.per_image_standardization(img)
example["inputs"] = img
return example
示例8: resize_video_frames
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def resize_video_frames(images, size):
return [tf.to_int64(tf.image.resize_images(
image, [size, size], tf.image.ResizeMethod.BILINEAR)) for image in images]
示例9: preprocess_example
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def preprocess_example(self, example, mode, unused_hparams):
example["inputs"].set_shape([_CIFAR10_IMAGE_SIZE, _CIFAR10_IMAGE_SIZE, 3])
example["inputs"] = tf.to_int64(example["inputs"])
example["inputs"] = tf.reshape(example["inputs"], (-1,))
del example["targets"] # Ensure unconditional generation
return example
示例10: resize_by_area
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def resize_by_area(img, size):
"""image resize function used by quite a few image problems."""
return tf.to_int64(
tf.image.resize_images(img, [size, size], tf.image.ResizeMethod.AREA))
示例11: make_multiscale_dilated
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def make_multiscale_dilated(image, resolutions, num_channels=3):
"""Returns list of scaled images, one for each resolution.
Resizes by skipping every nth pixel.
Args:
image: Tensor of shape [height, height, num_channels].
resolutions: List of heights that image's height is resized to. The function
assumes VALID padding, so the original image's height must be divisible
by each resolution's height to return the exact resolution size.
num_channels: Number of channels in image.
Returns:
List of Tensors, one for each resolution with shape given by
[resolutions[i], resolutions[i], num_channels] if resolutions properly
divide the original image's height; otherwise shape height and width is up
to valid skips.
"""
image_height = common_layers.shape_list(image)[0]
scaled_images = []
for height in resolutions:
dilation_rate = image_height // height # assuming height = width
scaled_image = image[::dilation_rate, ::dilation_rate]
scaled_image = tf.to_int64(scaled_image)
scaled_image.set_shape([None, None, num_channels])
scaled_images.append(scaled_image)
return scaled_images
示例12: preprocess_example
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def preprocess_example(self, example, mode, _):
# Just resize with area.
if self._was_reversed:
example["inputs"] = tf.to_int64(
tf.image.resize_images(example["inputs"], self.rescale_size,
tf.image.ResizeMethod.AREA))
else:
example = imagenet_preprocess_example(example, mode)
example["inputs"] = tf.to_int64(
tf.image.resize_images(example["inputs"], self.rescale_size))
return example
示例13: _get_action_type
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def _get_action_type(extended_indices, output_vocab_size, model_config):
"""Returns action_type tensor."""
action_type = tf.constant(0, dtype=tf.int64)
for action_type_range in _get_action_types_to_range(output_vocab_size,
model_config):
index_in_range = tf.logical_and(
tf.greater_equal(extended_indices, action_type_range.start_index),
tf.less(extended_indices, action_type_range.end_index))
action_type += (
tf.to_int64(index_in_range) * tf.constant(
action_type_range.action_type, dtype=tf.int64))
return action_type
示例14: get_decode_steps
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def get_decode_steps(extended_indices, output_vocab_size, model_config):
"""Convert Tensor of indices in extended vocabulary to DecodeStep."""
extended_indices = tf.to_int64(extended_indices)
action_types = _get_action_type(extended_indices, output_vocab_size,
model_config)
action_ids = _get_action_id(extended_indices, action_types, output_vocab_size,
model_config)
return DecodeSteps(action_types=action_types, action_ids=action_ids)
示例15: get_extended_indices
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import to_int64 [as 别名]
def get_extended_indices(decode_steps, output_vocab_size, model_config):
"""Convert DecodeSteps into a tensor of extended action ids."""
# This initial value will be broadcast to the length of decode_steps.
extended_action_indices = tf.constant(0, dtype=tf.int64)
for action_type_range in _get_action_types_to_range(output_vocab_size,
model_config):
is_type = tf.equal(
tf.constant(action_type_range.action_type, dtype=tf.int64),
decode_steps.action_types)
# For each timestep, exactly one of the action_type_ranges will be added,
# so this sum will populate each entry on exactly one iteration.
extended_action_indices += (
tf.to_int64(is_type) *
(decode_steps.action_ids + action_type_range.start_index))
return extended_action_indices