本文整理汇总了Python中tensorflow.to_int64方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.to_int64方法的具体用法?Python tensorflow.to_int64怎么用?Python tensorflow.to_int64使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.to_int64方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: argmax_with_score
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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: bilstm_representation_raw
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def bilstm_representation_raw(self, item, indices, re_use_lstm, name='lstm'):
"""Creates a representation graph which retrieves a text item (represented by its word embeddings) and returns
a vector-representation
:param item: the text item. Can be question or (good/bad) answer
:param sequence_length: maximum length of the text item
:param re_use_lstm: should be False for the first call, True for al subsequent ones to get the same lstm
variables
:return: representation tensor
"""
tensor_non_zero_token = non_zero_tokens(tf.to_float(indices))
sequence_length = tf.to_int64(tf.reduce_sum(tensor_non_zero_token, 1))
with tf.variable_scope(name, reuse=re_use_lstm):
output, _last = tf.nn.bidirectional_dynamic_rnn(
self.lstm_cell_forward,
self.lstm_cell_backward,
item,
dtype=tf.float32,
sequence_length=sequence_length
)
return tf.concat(2, output)
示例3: _build_training_graph
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def _build_training_graph(self, logits, labels, learning_rate):
"""
Build the training graph.
Args:
logits: Logits tensor, float - [batch_size, class_count].
labels: Labels tensor, int32 - [batch_size], with values in the range
[0, class_count).
learning_rate: The learning rate for the optimization.
Returns:
train_op: The Op for training.
loss: The Op for calculating loss.
"""
# Create an operation that calculates loss.
labels = tf.to_int64(labels)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=labels, name='xentropy')
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss)
correct_predict = tf.nn.in_top_k(logits, labels, 1)
accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))
return train_op, loss, accuracy
示例4: listMLE
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def listMLE(self, output, target_indexs, target_rels, name=None):
loss = None
with ops.name_scope(name, "listMLE",[output] + target_indexs + target_rels):
output = tf.nn.l2_normalize(output, 1)
loss = -1.0 * math_ops.reduce_sum(output,1)
print(loss.get_shape())
exp_output = tf.exp(output)
exp_output_table = tf.reshape(exp_output,[-1])
print(exp_output.get_shape())
print(exp_output_table.get_shape())
sum_exp_output = math_ops.reduce_sum(exp_output,1)
loss = tf.add(loss, tf.log(sum_exp_output))
#compute MLE
for i in xrange(self.rank_list_size-1):
idx = target_indexs[i] + tf.to_int64(self.batch_index_bias)
y_i = embedding_ops.embedding_lookup(exp_output_table, idx)
#y_i = tf.gather_nd(exp_output, idx)
sum_exp_output = tf.subtract(sum_exp_output, y_i)
loss = tf.add(loss, tf.log(sum_exp_output))
batch_size = tf.shape(target_rels[0])[0]
return math_ops.reduce_sum(loss) / math_ops.cast(batch_size, dtypes.float32)
开发者ID:QingyaoAi,项目名称:Deep-Listwise-Context-Model-for-Ranking-Refinement,代码行数:23,代码来源:RankLSTM_model.py
示例5: horizontal_cell
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def horizontal_cell(images, num_filters_out, cell_fw, cell_bw, keep_prob=1.0, scope=None):
"""Run an LSTM bidirectionally over all the rows of each image.
Args:
images: (num_images, height, width, depth) tensor
num_filters_out: output depth
scope: optional scope name
Returns:
(num_images, height, width, num_filters_out) tensor, where
"""
with tf.variable_scope(scope, "HorizontalGru", [images]):
sequence = images_to_sequence(images)
shapeT = tf.shape(sequence)
sequence_length = shapeT[0]
batch_sizeRNN = shapeT[1]
sequence_lengths = tf.to_int64(
tf.fill([batch_sizeRNN], sequence_length))
forward_drop1 = DropoutWrapper(cell_fw, output_keep_prob=keep_prob)
backward_drop1 = DropoutWrapper(cell_bw, output_keep_prob=keep_prob)
rnn_out1, _ = tf.nn.bidirectional_dynamic_rnn(forward_drop1, backward_drop1, sequence, dtype=tf.float32,
sequence_length=sequence_lengths, time_major=True,
swap_memory=True, scope=scope)
rnn_out1 = tf.concat(rnn_out1, 2)
rnn_out1 = tf.reshape(rnn_out1, shape=[-1, batch_sizeRNN, 2, num_filters_out])
output_sequence = tf.reduce_sum(rnn_out1, axis=2)
batch_size=tf.shape(images)[0]
output = sequence_to_images(output_sequence, batch_size)
return output
示例6: get_text
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def get_text(self, ids):
"""Returns a string corresponding to a sequence of character ids.
Args:
ids: a tensor with shape [batch_size, max_sequence_length]
"""
return tf.reduce_join(
self.table.lookup(tf.to_int64(ids)), reduction_indices=1)
示例7: tensors_to_item
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def tensors_to_item(self, keys_to_tensors):
return tf.to_int64(
self._num_of_views * keys_to_tensors[self._original_width_key] /
keys_to_tensors[self._width_key])
示例8: index_last_dim_with_indices
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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
示例9: preprocess_example
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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
示例10: resize_video_frames
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def resize_video_frames(images, size):
resized_images = []
for image in images:
resized_images.append(
tf.to_int64(tf.image.resize_images(
image, [size, size], tf.image.ResizeMethod.BILINEAR)))
return resized_images
示例11: preprocess_example
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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"])
return example
示例12: resize_by_area
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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))
示例13: make_multiscale
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import to_int64 [as 别名]
def make_multiscale(image, resolutions,
resize_method=tf.image.ResizeMethod.BICUBIC,
num_channels=3):
"""Returns list of scaled images, one for each resolution.
Args:
image: Tensor of shape [height, height, num_channels].
resolutions: List of heights that image's height is resized to.
resize_method: tf.image.ResizeMethod.
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].
"""
scaled_images = []
for height in resolutions:
scaled_image = tf.image.resize_images(
image,
size=[height, height], # assuming that height = width
method=resize_method)
scaled_image = tf.to_int64(scaled_image)
scaled_image.set_shape([height, height, num_channels])
scaled_images.append(scaled_image)
return scaled_images
示例14: make_multiscale_dilated
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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
示例15: preprocess_example
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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