本文整理汇总了Python中tensorflow.SparseTensor方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.SparseTensor方法的具体用法?Python tensorflow.SparseTensor怎么用?Python tensorflow.SparseTensor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.SparseTensor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _reshape_instance_masks
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def _reshape_instance_masks(self, keys_to_tensors):
"""Reshape instance segmentation masks.
The instance segmentation masks are reshaped to [num_instances, height,
width] and cast to boolean type to save memory.
Args:
keys_to_tensors: a dictionary from keys to tensors.
Returns:
A 3-D boolean tensor of shape [num_instances, height, width].
"""
masks = keys_to_tensors['image/segmentation/object']
if isinstance(masks, tf.SparseTensor):
masks = tf.sparse_tensor_to_dense(masks)
height = keys_to_tensors['image/height']
width = keys_to_tensors['image/width']
to_shape = tf.cast(tf.stack([-1, height, width]), tf.int32)
return tf.cast(tf.reshape(masks, to_shape), tf.bool)
示例2: _reshape_keypoints
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def _reshape_keypoints(self, keys_to_tensors):
"""Reshape keypoints.
The instance segmentation masks are reshaped to [num_instances,
num_keypoints, 2].
Args:
keys_to_tensors: a dictionary from keys to tensors.
Returns:
A 3-D float tensor of shape [num_instances, num_keypoints, 2] with values
in {0, 1}.
"""
y = keys_to_tensors['image/object/keypoint/y']
if isinstance(y, tf.SparseTensor):
y = tf.sparse_tensor_to_dense(y)
y = tf.expand_dims(y, 1)
x = keys_to_tensors['image/object/keypoint/x']
if isinstance(x, tf.SparseTensor):
x = tf.sparse_tensor_to_dense(x)
x = tf.expand_dims(x, 1)
keypoints = tf.concat([y, x], 1)
keypoints = tf.reshape(keypoints, [-1, self._num_keypoints, 2])
return keypoints
示例3: _reshape_instance_masks
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def _reshape_instance_masks(self, keys_to_tensors):
"""Reshape instance segmentation masks.
The instance segmentation masks are reshaped to [num_instances, height,
width].
Args:
keys_to_tensors: a dictionary from keys to tensors.
Returns:
A 3-D float tensor of shape [num_instances, height, width] with values
in {0, 1}.
"""
height = keys_to_tensors['image/height']
width = keys_to_tensors['image/width']
to_shape = tf.cast(tf.stack([-1, height, width]), tf.int32)
masks = keys_to_tensors['image/object/mask']
if isinstance(masks, tf.SparseTensor):
masks = tf.sparse_tensor_to_dense(masks)
masks = tf.reshape(tf.to_float(tf.greater(masks, 0.0)), to_shape)
return tf.cast(masks, tf.float32)
示例4: extract_dense_weights
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def extract_dense_weights(sess):
for key in dense_layers.keys():
layer = dense_layers[key]
# sparse kernel
dense_kernel = layer.kernel
dense_kernel_shape = dense_kernel.get_shape().as_list()
# dense_kernel = tf.reshape(dense_kernel, [dense_kernel_shape[0] * dense_kernel_shape[1] * dense_kernel_shape[2],
# dense_kernel_shape[3]])
# dense_kernel = tf.transpose(dense_kernel)
idx = tf.where(tf.not_equal(dense_kernel, 0))
sparse_kernel = tf.SparseTensor(idx, tf.gather_nd(dense_kernel, idx), dense_kernel.get_shape())
if layer.bias is not None:
dk, k, b = sess.run([dense_kernel, sparse_kernel, layer.bias])
else:
dk, k = sess.run([dense_kernel, sparse_kernel])
b = None
dense_weights['%s/%s' % (key, 'kernel_dense')] = dk
dense_weights['%s/%s' % (key, 'kernel')] = k
dense_weights['%s/%s' % (key, 'kernel_shape')] = dense_kernel_shape
dense_weights['%s/%s' % (key, 'bias')] = b
示例5: compute_edit_distance
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def compute_edit_distance(session, labels_true_st, labels_pred_st):
"""Compute edit distance per mini-batch.
Args:
session:
labels_true_st: A `SparseTensor` of ground truth
labels_pred_st: A `SparseTensor` of prediction
Returns:
edit_distances: list of edit distance of each uttearance
"""
indices, values, dense_shape = labels_true_st
labels_pred_pl = tf.SparseTensor(indices, values, dense_shape)
indices, values, dense_shape = labels_pred_st
labels_true_pl = tf.SparseTensor(indices, values, dense_shape)
edit_op = tf.edit_distance(labels_pred_pl, labels_true_pl, normalize=True)
edit_distances = session.run(edit_op)
return edit_distances
示例6: compute_ler
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def compute_ler(self, labels_true, labels_pred):
"""Operation for computing LER (Label Error Rate).
Args:
labels_true: A SparseTensor of target labels
labels_pred: A SparseTensor of predicted labels
Returns:
ler_op: operation for computing LER
"""
# Compute LER (normalize by label length)
ler_op = tf.reduce_mean(tf.edit_distance(
labels_pred, labels_true, normalize=True))
# TODO: consider <EOS>
# Add a scalar summary for the snapshot of LER
# with tf.name_scope("ler"):
# self.summaries_train.append(tf.summary.scalar(
# 'ler_train', ler_op))
# self.summaries_dev.append(tf.summary.scalar(
# 'ler_dev', ler_op))
# TODO: feed_dictのタイミング違うからエラーになる
# global_stepをupdateする前にする?
return ler_op
示例7: create_placeholders
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def create_placeholders(self):
"""Create placeholders and append them to list."""
self.inputs_pl_list.append(
tf.placeholder(tf.float32, shape=[None, None, self.input_size],
name='input'))
self.labels_pl_list.append(
tf.SparseTensor(tf.placeholder(tf.int64, name='indices'),
tf.placeholder(tf.int32, name='values'),
tf.placeholder(tf.int64, name='shape')))
self.labels_sub_pl_list.append(
tf.SparseTensor(tf.placeholder(tf.int64, name='indices_sub'),
tf.placeholder(tf.int32, name='values_sub'),
tf.placeholder(tf.int64, name='shape_sub')))
self.inputs_seq_len_pl_list.append(
tf.placeholder(tf.int32, shape=[None], name='inputs_seq_len'))
self.keep_prob_pl_list.append(
tf.placeholder(tf.float32, name='keep_prob'))
示例8: compute_ler
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def compute_ler(self, decode_op, labels):
"""Operation for computing LER (Label Error Rate).
Args:
decode_op: operation for decoding
labels: A SparseTensor of target labels
Return:
ler_op: operation for computing LER
"""
# Compute LER (normalize by label length)
ler_op = tf.reduce_mean(tf.edit_distance(
decode_op, labels, normalize=True))
# Add a scalar summary for the snapshot of LER
self.summaries_train.append(tf.summary.scalar('ler_train', ler_op))
self.summaries_dev.append(tf.summary.scalar('ler_dev', ler_op))
return ler_op
示例9: decoderOutputToText
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def decoderOutputToText(self, ctcOutput):
""" Extract texts from output of CTC decoder """
# Contains string of labels for each batch element
encodedLabelStrs = [[] for i in range(Model.batchSize)]
# Word beam search: label strings terminated by blank
if self.decoderType == DecoderType.WordBeamSearch:
blank = len(self.charList)
for b in range(Model.batchSize):
for label in ctcOutput[b]:
if label == blank:
break
encodedLabelStrs[b].append(label)
# TF decoders: label strings are contained in sparse tensor
else:
# Ctc returns tuple, first element is SparseTensor
decoded = ctcOutput[0][0]
# Go over all indices and save mapping: batch -> values
idxDict = {b : [] for b in range(Model.batchSize)}
for (idx, idx2d) in enumerate(decoded.indices):
label = decoded.values[idx]
batchElement = idx2d[0] # index according to [b,t]
encodedLabelStrs[batchElement].append(label)
# Map labels to chars for all batch elements
return [str().join([self.charList[c] for c in labelStr]) for labelStr in encodedLabelStrs]
开发者ID:sushant097,项目名称:Handwritten-Line-Text-Recognition-using-Deep-Learning-with-Tensorflow,代码行数:26,代码来源:Model.py
示例10: shape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def shape(input, name=None, out_type=dtypes.int32):
# pylint: disable=redefined-builtin
"""Returns the shape of a tensor.
This operation returns a 1-D integer tensor representing the shape of `input`.
For example:
```python
# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
shape(t) ==> [2, 2, 3]
```
Args:
input: A `Tensor` or `SparseTensor`.
name: A name for the operation (optional).
out_type: (Optional) The specified output type of the operation
(`int32` or `int64`). Defaults to `tf.int32`.
Returns:
A `Tensor` of type `out_type`.
"""
return shape_internal(input, name, optimize=True, out_type=out_type)
示例11: shape_internal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def shape_internal(input, name=None, optimize=True, out_type=dtypes.int32):
# pylint: disable=redefined-builtin
"""Returns the shape of a tensor.
Args:
input: A `Tensor` or `SparseTensor`.
name: A name for the operation (optional).
optimize: if true, encode the shape as a constant when possible.
out_type: (Optional) The specified output type of the operation
(`int32` or `int64`). Defaults to tf.int32.
Returns:
A `Tensor` of type `out_type`.
"""
with ops.name_scope(name, "Shape", [input]) as name:
if isinstance(
input, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)):
return gen_math_ops.cast(input.dense_shape, out_type)
else:
input_tensor = ops.convert_to_tensor(input)
input_shape = input_tensor.get_shape()
if optimize and input_shape.is_fully_defined():
return constant(input_shape.as_list(), out_type, name=name)
return gen_array_ops.shape(input, name=name, out_type=out_type)
示例12: size
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def size(input, name=None, out_type=dtypes.int32):
# pylint: disable=redefined-builtin
"""Returns the size of a tensor.
This operation returns an integer representing the number of elements in
`input`.
For example:
```python
# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]
size(t) ==> 12
```
Args:
input: A `Tensor` or `SparseTensor`.
name: A name for the operation (optional).
out_type: (Optional) The specified output type of the operation
(`int32` or `int64`). Defaults to tf.int32.
Returns:
A `Tensor` of type `out_type`. Defaults to tf.int32.
"""
return size_internal(input, name, optimize=True, out_type=out_type)
示例13: size_internal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def size_internal(input, name=None, optimize=True, out_type=dtypes.int32):
# pylint: disable=redefined-builtin,protected-access
"""Returns the size of a tensor.
Args:
input: A `Tensor` or `SparseTensor`.
name: A name for the operation (optional).
optimize: if true, encode the size as a constant when possible.
out_type: (Optional) The specified output type of the operation
(`int32` or `int64`). Defaults to tf.int32.
Returns:
A `Tensor` of type `out_type`.
"""
with ops.name_scope(name, "Size", [input]) as name:
if isinstance(
input, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)):
return gen_math_ops._prod(
gen_math_ops.cast(input.dense_shape, out_type), 0, name=name)
else:
input_tensor = ops.convert_to_tensor(input)
input_shape = input_tensor.get_shape()
if optimize and input_shape.is_fully_defined():
return constant(input_shape.num_elements(), out_type, name=name)
return gen_array_ops.size(input, name=name, out_type=out_type)
示例14: sp_matrix_to_sp_tensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def sp_matrix_to_sp_tensor(x):
"""
Converts a Scipy sparse matrix to a SparseTensor.
:param x: a Scipy sparse matrix.
:return: a SparseTensor.
"""
if not hasattr(x, 'tocoo'):
try:
x = sp.coo_matrix(x)
except:
raise TypeError('x must be convertible to scipy.coo_matrix')
else:
x = x.tocoo()
out = tf.SparseTensor(
indices=np.array([x.row, x.col]).T,
values=x.data,
dense_shape=x.shape
)
return tf.sparse.reorder(out)
示例15: sp_batch_to_sp_tensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import SparseTensor [as 别名]
def sp_batch_to_sp_tensor(a_list):
"""
Converts a list of Scipy sparse matrices to a rank 3 SparseTensor.
:param a_list: list of Scipy sparse matrices with the same shape.
:return: SparseTensor of rank 3.
"""
tensor_data = []
for i, a in enumerate(a_list):
values = a.tocoo().data
row = a.row
col = a.col
batch = np.ones_like(col) * i
tensor_data.append((values, batch, row, col))
tensor_data = list(map(np.concatenate, zip(*tensor_data)))
out = tf.SparseTensor(
indices=np.array(tensor_data[1:]).T,
values=tensor_data[0],
dense_shape=(len(a_list), ) + a_list[0].shape
)
return out