本文整理汇总了Python中tensorflow.deserialize_many_sparse方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.deserialize_many_sparse方法的具体用法?Python tensorflow.deserialize_many_sparse怎么用?Python tensorflow.deserialize_many_sparse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.deserialize_many_sparse方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSerializeDeserializeMany
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def testSerializeDeserializeMany(self):
with self.test_session(use_gpu=False) as sess:
sp_input0 = self._SparseTensorValue_5x6(np.arange(6))
sp_input1 = self._SparseTensorValue_3x4(np.arange(6))
serialized0 = tf.serialize_sparse(sp_input0)
serialized1 = tf.serialize_sparse(sp_input1)
serialized_concat = tf.stack([serialized0, serialized1])
sp_deserialized = tf.deserialize_many_sparse(
serialized_concat, dtype=tf.int32)
combined_indices, combined_values, combined_shape = sess.run(
sp_deserialized)
self.assertAllEqual(combined_indices[:6, 0], [0] * 6) # minibatch 0
self.assertAllEqual(combined_indices[:6, 1:], sp_input0[0])
self.assertAllEqual(combined_indices[6:, 0], [1] * 6) # minibatch 1
self.assertAllEqual(combined_indices[6:, 1:], sp_input1[0])
self.assertAllEqual(combined_values[:6], sp_input0[1])
self.assertAllEqual(combined_values[6:], sp_input1[1])
self.assertAllEqual(combined_shape, [2, 5, 6])
示例2: testFeedSerializeDeserializeMany
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def testFeedSerializeDeserializeMany(self):
with self.test_session(use_gpu=False) as sess:
sp_input0 = self._SparseTensorPlaceholder()
sp_input1 = self._SparseTensorPlaceholder()
input0_val = self._SparseTensorValue_5x6(np.arange(6))
input1_val = self._SparseTensorValue_3x4(np.arange(6))
serialized0 = tf.serialize_sparse(sp_input0)
serialized1 = tf.serialize_sparse(sp_input1)
serialized_concat = tf.stack([serialized0, serialized1])
sp_deserialized = tf.deserialize_many_sparse(
serialized_concat, dtype=tf.int32)
combined_indices, combined_values, combined_shape = sess.run(
sp_deserialized, {sp_input0: input0_val, sp_input1: input1_val})
self.assertAllEqual(combined_indices[:6, 0], [0] * 6) # minibatch 0
self.assertAllEqual(combined_indices[:6, 1:], input0_val[0])
self.assertAllEqual(combined_indices[6:, 0], [1] * 6) # minibatch 1
self.assertAllEqual(combined_indices[6:, 1:], input1_val[0])
self.assertAllEqual(combined_values[:6], input0_val[1])
self.assertAllEqual(combined_values[6:], input1_val[1])
self.assertAllEqual(combined_shape, [2, 5, 6])
示例3: testSerializeManyDeserializeManyRoundTrip
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def testSerializeManyDeserializeManyRoundTrip(self):
with self.test_session(use_gpu=False) as sess:
# N == 4 because shape_value == [4, 5]
indices_value = np.array([[0, 0], [0, 1], [2, 0]], dtype=np.int64)
values_value = np.array([b"a", b"b", b"c"])
shape_value = np.array([4, 5], dtype=np.int64)
sparse_tensor = self._SparseTensorPlaceholder(dtype=tf.string)
serialized = tf.serialize_many_sparse(sparse_tensor)
deserialized = tf.deserialize_many_sparse(serialized, dtype=tf.string)
serialized_value, deserialized_value = sess.run(
[serialized, deserialized],
feed_dict={sparse_tensor.indices: indices_value,
sparse_tensor.values: values_value,
sparse_tensor.shape: shape_value})
self.assertEqual(serialized_value.shape, (4, 3))
self.assertAllEqual(deserialized_value.indices, indices_value)
self.assertAllEqual(deserialized_value.values, values_value)
self.assertAllEqual(deserialized_value.shape, shape_value)
示例4: testDeserializeFailsWrongType
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def testDeserializeFailsWrongType(self):
with self.test_session(use_gpu=False) as sess:
sp_input0 = self._SparseTensorPlaceholder()
sp_input1 = self._SparseTensorPlaceholder()
input0_val = self._SparseTensorValue_5x6(np.arange(6))
input1_val = self._SparseTensorValue_3x4(np.arange(6))
serialized0 = tf.serialize_sparse(sp_input0)
serialized1 = tf.serialize_sparse(sp_input1)
serialized_concat = tf.stack([serialized0, serialized1])
sp_deserialized = tf.deserialize_many_sparse(
serialized_concat, dtype=tf.int64)
with self.assertRaisesOpError(
r"Requested SparseTensor of type int64 but "
r"SparseTensor\[0\].values.dtype\(\) == int32"):
sess.run(
sp_deserialized, {sp_input0: input0_val, sp_input1: input1_val})
示例5: testDeserializeFailsInconsistentRank
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def testDeserializeFailsInconsistentRank(self):
with self.test_session(use_gpu=False) as sess:
sp_input0 = self._SparseTensorPlaceholder()
sp_input1 = self._SparseTensorPlaceholder()
input0_val = self._SparseTensorValue_5x6(np.arange(6))
input1_val = self._SparseTensorValue_1x1x1()
serialized0 = tf.serialize_sparse(sp_input0)
serialized1 = tf.serialize_sparse(sp_input1)
serialized_concat = tf.stack([serialized0, serialized1])
sp_deserialized = tf.deserialize_many_sparse(
serialized_concat, dtype=tf.int32)
with self.assertRaisesOpError(
r"Inconsistent rank across SparseTensors: rank prior to "
r"SparseTensor\[1\] was: 3 but rank of SparseTensor\[1\] is: 4"):
sess.run(
sp_deserialized, {sp_input0: input0_val, sp_input1: input1_val})
示例6: postbatch_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def postbatch_fn( image, width, label, length, text ):
"""Post-batching, postprocessing: packs raw tensors into a dictionary for
Dataset's iterator output"""
# Batching is complete, so now we can re-sparsify our labels for ctc_loss
label = tf.cast( tf.deserialize_many_sparse( label, tf.int64 ),
tf.int32 )
# Format relevant features for estimator ingestion
features = {
"image" : image,
"width" : width,
"length" : length,
"text" : text
}
return features, label
示例7: get_inputs
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def get_inputs(self):
"""
Return's tensors for inputs, sequence_lengths and labels
"""
with tf.device("/cpu:0"):
inputs, sequence_lengths, labels = self.queue.dequeue()
labels = tf.deserialize_many_sparse(labels, dtype=tf.int32)
return inputs, sequence_lengths, labels
示例8: threaded_input_pipeline
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def threaded_input_pipeline(base_dir,file_patterns,
num_threads=4,
batch_size=32,
batch_device=None,
preprocess_device=None,
num_epochs=None):
queue_capacity = num_threads*batch_size*2
# Allow a smaller final batch if we are going for a fixed number of epochs
final_batch = (num_epochs!=None)
data_queue = _get_data_queue(base_dir, file_patterns,
capacity=queue_capacity,
num_epochs=num_epochs)
# each thread has a subgraph with its own reader (sharing filename queue)
data_tuples = [] # list of subgraph [image, label, width, text] elements
with tf.device(preprocess_device):
for _ in range(num_threads):
image, width, label, length, text, filename = _read_word_record(
data_queue)
image = _preprocess_image(image) # move after batch?
data_tuples.append([image, width, label, length, text, filename])
with tf.device(batch_device): # Create batch queue
image, width, label, length, text, filename = tf.train.batch_join(
data_tuples,
batch_size=batch_size,
capacity=queue_capacity,
allow_smaller_final_batch=final_batch,
dynamic_pad=True)
label = tf.deserialize_many_sparse(label, tf.int64) # post-batching...
label = tf.cast(label, tf.int32) # for ctc_loss
return image, width, label, length, text, filename
示例9: ImageInput
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def ImageInput(input_pattern, num_threads, shape, using_ctc, reader=None):
"""Creates an input image tensor from the input_pattern filenames.
TODO(rays) Expand for 2-d labels, 0-d labels, and logistic targets.
Args:
input_pattern: Filenames of the dataset(s) to read.
num_threads: Number of preprocessing threads.
shape: ImageShape with the desired shape of the input.
using_ctc: Take the unpadded_class labels instead of padded.
reader: Function that returns an actual reader to read Examples from
input files. If None, uses tf.TFRecordReader().
Returns:
images: Float Tensor containing the input image scaled to [-1.28, 1.27].
heights: Tensor int64 containing the heights of the images.
widths: Tensor int64 containing the widths of the images.
labels: Serialized SparseTensor containing the int64 labels.
sparse_labels: Serialized SparseTensor containing the int64 labels.
truths: Tensor string of the utf8 truth texts.
Raises:
ValueError: if the optimizer type is unrecognized.
"""
data_files = tf.gfile.Glob(input_pattern)
assert data_files, 'no files found for dataset ' + input_pattern
queue_capacity = shape.batch_size * num_threads * 2
filename_queue = tf.train.string_input_producer(
data_files, capacity=queue_capacity)
# Create a subgraph with its own reader (but sharing the
# filename_queue) for each preprocessing thread.
images_and_label_lists = []
for _ in range(num_threads):
image, height, width, labels, text = _ReadExamples(filename_queue, shape,
using_ctc, reader)
images_and_label_lists.append([image, height, width, labels, text])
# Create a queue that produces the examples in batches.
images, heights, widths, labels, truths = tf.train.batch_join(
images_and_label_lists,
batch_size=shape.batch_size,
capacity=16 * shape.batch_size,
dynamic_pad=True)
# Deserialize back to sparse, because the batcher doesn't do sparse.
labels = tf.deserialize_many_sparse(labels, tf.int64)
sparse_labels = tf.cast(labels, tf.int32)
labels = tf.sparse_tensor_to_dense(labels)
labels = tf.reshape(labels, [shape.batch_size, -1], name='Labels')
# Crush the other shapes to just the batch dimension.
heights = tf.reshape(heights, [-1], name='Heights')
widths = tf.reshape(widths, [-1], name='Widths')
truths = tf.reshape(truths, [-1], name='Truths')
# Give the images a nice name as well.
images = tf.identity(images, name='Images')
tf.summary.image('Images', images)
return images, heights, widths, labels, sparse_labels, truths
示例10: benchmarkVeryLarge2DFloatSparseTensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def benchmarkVeryLarge2DFloatSparseTensor(self):
np.random.seed(127)
num_elements = 10000
batch_size = 64
indices_batch = np.random.randint(
batch_size, size=num_elements, dtype=np.int64)
indices_value = np.arange(num_elements, dtype=np.int64)
indices = np.asarray(
sorted(zip(indices_batch, indices_value)), dtype=np.int64)
values = ["feature_value_for_embedding_lookup"] * num_elements
shape = np.asarray([batch_size, num_elements], dtype=np.int64)
with tf.Session() as sess:
with tf.device("/cpu:0"):
indices = tf.Variable(indices)
values = tf.Variable(values)
shape = tf.Variable(shape)
st = tf.SparseTensor(indices, values, shape)
st_handles = add_many_sparse_to_tensors_map(st)
st_roundtrip = take_many_sparse_from_tensors_map(
sparse_map_op=st_handles.op, sparse_handles=st_handles)
st_roundtrip_op = st_roundtrip.values.op
st_serialized = tf.serialize_many_sparse(st)
st_deserialized = tf.deserialize_many_sparse(
st_serialized, dtype=values.dtype)
st_deserialized_op = st_deserialized.values.op
tf.global_variables_initializer().run()
st_roundtrip_values = sess.run(st_roundtrip)
st_deserialized_values = sess.run(st_deserialized)
np.testing.assert_equal(
st_roundtrip_values.values, st_deserialized_values.values)
np.testing.assert_equal(
st_roundtrip_values.indices, st_deserialized_values.indices)
np.testing.assert_equal(
st_roundtrip_values.shape, st_deserialized_values.shape)
self.run_op_benchmark(
sess, st_roundtrip_op, min_iters=2000,
name="benchmark_very_large_2d_float_st_tensor_maps")
self.run_op_benchmark(
sess, st_deserialized_op, min_iters=2000,
name="benchmark_very_large_2d_float_st_serialization")
示例11: bucketed_input_pipeline
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def bucketed_input_pipeline(base_dir,file_patterns,
num_threads=4,
batch_size=32,
boundaries=[32, 64, 96, 128, 160, 192, 224, 256],
input_device=None,
width_threshold=None,
length_threshold=None,
num_epochs=None):
"""Get input tensors bucketed by image width
Returns:
image : float32 image tensor [batch_size 32 ? 1] padded to batch max width
width : int32 image widths (for calculating post-CNN sequence length)
label : Sparse tensor with label sequences for the batch
length : Length of label sequence (text length)
text : Human readable string for the image
filename : Source file path
"""
queue_capacity = num_threads*batch_size*2
# Allow a smaller final batch if we are going for a fixed number of epochs
final_batch = (num_epochs!=None)
data_queue = _get_data_queue(base_dir, file_patterns,
capacity=queue_capacity,
num_epochs=num_epochs)
with tf.device(input_device): # Create bucketing batcher
image, width, label, length, text, filename = _read_word_record(
data_queue)
image = _preprocess_image(image) # move after batch?
keep_input = _get_input_filter(width, width_threshold,
length, length_threshold)
data_tuple = [image, label, length, text, filename]
width,data_tuple = tf.contrib.training.bucket_by_sequence_length(
input_length=width,
tensors=data_tuple,
bucket_boundaries=boundaries,
batch_size=batch_size,
capacity=queue_capacity,
keep_input=keep_input,
allow_smaller_final_batch=final_batch,
dynamic_pad=True)
[image, label, length, text, filename] = data_tuple
label = tf.deserialize_many_sparse(label, tf.int64) # post-batching...
label = tf.cast(label, tf.int32) # for ctc_loss
return image, width, label, length, text, filename
示例12: ImageInput
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import deserialize_many_sparse [as 别名]
def ImageInput(input_pattern, num_threads, shape, using_ctc, reader=None):
"""Creates an input image tensor from the input_pattern filenames.
TODO(rays) Expand for 2-d labels, 0-d labels, and logistic targets.
Args:
input_pattern: Filenames of the dataset(s) to read.
num_threads: Number of preprocessing threads.
shape: ImageShape with the desired shape of the input.
using_ctc: Take the unpadded_class labels instead of padded.
reader: Function that returns an actual reader to read Examples from
input files. If None, uses tf.TFRecordReader().
Returns:
images: Float Tensor containing the input image scaled to [-1.28, 1.27].
heights: Tensor int64 containing the heights of the images.
widths: Tensor int64 containing the widths of the images.
labels: Serialized SparseTensor containing the int64 labels.
sparse_labels: Serialized SparseTensor containing the int64 labels.
truths: Tensor string of the utf8 truth texts.
Raises:
ValueError: if the optimizer type is unrecognized.
"""
data_files = tf.gfile.Glob(input_pattern)
assert data_files, 'no files found for dataset ' + input_pattern
queue_capacity = shape.batch_size * num_threads * 2
filename_queue = tf.train.string_input_producer(
data_files, capacity=queue_capacity)
# Create a subgraph with its own reader (but sharing the
# filename_queue) for each preprocessing thread.
images_and_label_lists = []
for _ in range(num_threads):
image, height, width, labels, text = _ReadExamples(filename_queue, shape,
using_ctc, reader)
images_and_label_lists.append([image, height, width, labels, text])
# Create a queue that produces the examples in batches.
images, heights, widths, labels, truths = tf.train.batch_join(
images_and_label_lists,
batch_size=shape.batch_size,
capacity=16 * shape.batch_size,
dynamic_pad=True)
# Deserialize back to sparse, because the batcher doesn't do sparse.
labels = tf.deserialize_many_sparse(labels, tf.int64)
sparse_labels = tf.cast(labels, tf.int32)
labels = tf.sparse_tensor_to_dense(labels)
labels = tf.reshape(labels, [shape.batch_size, -1], name='Labels')
# Crush the other shapes to just the batch dimension.
heights = tf.reshape(heights, [-1], name='Heights')
widths = tf.reshape(widths, [-1], name='Widths')
truths = tf.reshape(truths, [-1], name='Truths')
# Give the images a nice name as well.
images = tf.identity(images, name='Images')
tf.image_summary('Images', images)
return images, heights, widths, labels, sparse_labels, truths