本文整理汇总了Python中tensorflow.python.ops.sparse_ops.sparse_tensor_to_dense函数的典型用法代码示例。如果您正苦于以下问题:Python sparse_tensor_to_dense函数的具体用法?Python sparse_tensor_to_dense怎么用?Python sparse_tensor_to_dense使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sparse_tensor_to_dense函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testRandom
def testRandom(self):
np.random.seed(1618)
shapes = [(13,), (6, 8), (1, 7, 1)]
for shape in shapes:
for dtype in [np.int32, np.int64, np.float16, np.float32, np.float64]:
a_np = np.random.randn(*shape).astype(dtype)
b_np = np.random.randn(*shape).astype(dtype)
sp_a, unused_a_nnz = _sparsify(a_np, thresh=-.5)
sp_b, unused_b_nnz = _sparsify(b_np, thresh=-.5)
with self.test_session(use_gpu=False):
maximum_tf = sparse_ops.sparse_maximum(sp_a, sp_b)
maximum_tf_densified = sparse_ops.sparse_tensor_to_dense(
maximum_tf).eval()
minimum_tf = sparse_ops.sparse_minimum(sp_a, sp_b)
minimum_tf_densified = sparse_ops.sparse_tensor_to_dense(
minimum_tf).eval()
a_densified = sparse_ops.sparse_tensor_to_dense(sp_a).eval()
b_densified = sparse_ops.sparse_tensor_to_dense(sp_b).eval()
self.assertAllEqual(
np.maximum(a_densified, b_densified), maximum_tf_densified)
self.assertAllEqual(
np.minimum(a_densified, b_densified), minimum_tf_densified)
示例2: _load_batch_pair_pose
def _load_batch_pair_pose(self, dataset):
data_provider = slim.dataset_data_provider.DatasetDataProvider(dataset, common_queue_capacity=32, common_queue_min=8)
image_raw_0, image_raw_1, label, pose_0, pose_1, mask_0, mask_1 = data_provider.get([
'image_raw_0', 'image_raw_1', 'label', 'pose_sparse_r4_0', 'pose_sparse_r4_1', 'pose_mask_r4_0', 'pose_mask_r4_1'])
print("trainer--_load_batch_pair_pose:")
print(pose_0)
pose_0 = sparse_ops.sparse_tensor_to_dense(pose_0, default_value=0, validate_indices=False)
pose_1 = sparse_ops.sparse_tensor_to_dense(pose_1, default_value=0, validate_indices=False)
image_raw_0 = tf.reshape(image_raw_0, [128, 64, 3])
image_raw_1 = tf.reshape(image_raw_1, [128, 64, 3])
pose_0 = tf.cast(tf.reshape(pose_0, [128, 64, self.keypoint_num]), tf.float32) # 数据类型转换
pose_1 = tf.cast(tf.reshape(pose_1, [128, 64, self.keypoint_num]), tf.float32) #
mask_0 = tf.cast(tf.reshape(mask_0, [128, 64, 1]), tf.float32)
mask_1 = tf.cast(tf.reshape(mask_1, [128, 64, 1]), tf.float32)
images_0, images_1, poses_0, poses_1, masks_0, masks_1 = tf.train.batch([image_raw_0, image_raw_1, pose_0, pose_1, mask_0, mask_1],
batch_size=self.batch_size, num_threads=self.num_threads, capacity=self.capacityCoff * self.batch_size)
images_0 = utils_wgan.process_image(tf.to_float(images_0), 127.5, 127.5)
images_1 = utils_wgan.process_image(tf.to_float(images_1), 127.5, 127.5)
poses_0 = poses_0*2-1
poses_1 = poses_1*2-1
return images_0, images_1, poses_0, poses_1, masks_0, masks_1
示例3: ParseLabelTensorOrDict
def ParseLabelTensorOrDict(labels):
"""Return a tensor to use for input labels to tensor_forest.
The incoming targets can be a dict where keys are the string names of the
columns, which we turn into a single 1-D tensor for classification or
2-D tensor for regression.
Converts sparse tensors to dense ones.
Args:
labels: `Tensor` or `dict` of `Tensor` objects.
Returns:
A 2-D tensor for labels/outputs.
"""
if isinstance(labels, dict):
return math_ops.to_float(
array_ops.concat(
[
sparse_ops.sparse_tensor_to_dense(
labels[k], default_value=-1) if isinstance(
labels, sparse_tensor.SparseTensor) else labels[k]
for k in sorted(labels.keys())
],
1))
else:
if isinstance(labels, sparse_tensor.SparseTensor):
return math_ops.to_float(sparse_ops.sparse_tensor_to_dense(
labels, default_value=-1))
else:
return math_ops.to_float(labels)
示例4: _compare
def _compare(self, sp_t, reduction_axes, ndims, keep_dims):
densified = sparse_ops.sparse_tensor_to_dense(sp_t).eval()
np_ans = densified
if reduction_axes is None:
np_ans = np.sum(np_ans, keepdims=keep_dims)
else:
if not isinstance(reduction_axes, list): # Single scalar.
reduction_axes = [reduction_axes]
reduction_axes = np.array(reduction_axes).astype(np.int32)
# Handles negative axes.
reduction_axes = (reduction_axes + ndims) % ndims
# Loop below depends on sorted.
reduction_axes.sort()
for ra in reduction_axes.ravel()[::-1]:
np_ans = np.sum(np_ans, axis=ra, keepdims=keep_dims)
with self.test_session():
tf_dense_ans = sparse_ops.sparse_reduce_sum(sp_t, reduction_axes,
keep_dims)
out_dense = tf_dense_ans.eval()
tf_sparse_ans = sparse_ops.sparse_reduce_sum_sparse(sp_t, reduction_axes,
keep_dims)
# Convert to dense for comparison purposes.
out_sparse = sparse_ops.sparse_tensor_to_dense(tf_sparse_ans).eval()
self.assertAllClose(np_ans, out_dense)
self.assertAllClose(np_ans, out_sparse)
示例5: testPrintSparseTensorPassthrough
def testPrintSparseTensorPassthrough(self):
a = sparse_tensor.SparseTensor(
indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
b = sparse_tensor.SparseTensor(
indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
a = prettyprint_ops.print_op(a)
with self.test_session():
self.assertAllEqual(
sparse_ops.sparse_tensor_to_dense(a).eval(),
sparse_ops.sparse_tensor_to_dense(b).eval())
示例6: tensors_to_item
def tensors_to_item(self, keys_to_tensors):
tensor = keys_to_tensors[self._tensor_key]
shape = self._shape
if self._shape_key:
shape = keys_to_tensors[self._shape_key]
if isinstance(shape, ops.SparseTensor):
shape = sparse_ops.sparse_tensor_to_dense(shape)
if isinstance(tensor, ops.SparseTensor):
if shape is not None:
tensor = sparse_ops.sparse_reshape(tensor, shape)
tensor = sparse_ops.sparse_tensor_to_dense(tensor, self._default_value)
else:
if shape is not None:
tensor = array_ops.reshape(tensor, shape)
return tensor
示例7: new_model_fn
def new_model_fn(features, labels, mode, config): # pylint: disable=missing-docstring
spec = estimator.model_fn(features, labels, mode, config)
predictions = spec.predictions
if predictions is None:
return spec
verify_keys_and_predictions(features, predictions)
for key in get_keys(features):
feature = sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(
features[key])
if sparse_default_values and (key in sparse_default_values):
if not isinstance(feature, sparse_tensor_lib.SparseTensor):
raise ValueError(
'Feature ({}) is expected to be a `SparseTensor`.'.format(key))
feature = sparse_ops.sparse_tensor_to_dense(
feature, default_value=sparse_default_values[key])
if not isinstance(feature, ops.Tensor):
raise ValueError(
'Feature ({}) should be a Tensor. Please use `keys` '
'argument of forward_features to filter unwanted features, or'
'add key to argument `sparse_default_values`.'
'Type of features[{}] is {}.'.format(key, key, type(feature)))
predictions[key] = feature
spec = spec._replace(predictions=predictions)
if spec.export_outputs:
for ekey in ['predict', 'serving_default']:
if (ekey in spec.export_outputs and
isinstance(spec.export_outputs[ekey],
PredictOutput)):
export_outputs = spec.export_outputs[ekey].outputs
for key in get_keys(features):
export_outputs[key] = predictions[key]
return spec
示例8: get_sequence_dense_tensor
def get_sequence_dense_tensor(self, transformation_cache, state_manager):
"""Returns a `TensorSequenceLengthPair`.
Args:
transformation_cache: A `FeatureTransformationCache` object to access
features.
state_manager: A `StateManager` to create / access resources such as
lookup tables.
"""
sp_tensor = transformation_cache.get(self, state_manager)
dense_tensor = sparse_ops.sparse_tensor_to_dense(
sp_tensor, default_value=self.default_value)
# Reshape into [batch_size, T, variable_shape].
dense_shape = array_ops.concat(
[array_ops.shape(dense_tensor)[:1], [-1], self.variable_shape],
axis=0)
dense_tensor = array_ops.reshape(dense_tensor, shape=dense_shape)
# Get the number of timesteps per example
# For the 2D case, the raw values are grouped according to num_elements;
# for the 3D case, the grouping happens in the third dimension, and
# sequence length is not affected.
num_elements = (self.variable_shape.num_elements()
if sp_tensor.shape.ndims == 2 else 1)
seq_length = fc_old._sequence_length_from_sparse_tensor(
sp_tensor, num_elements=num_elements)
return fc.SequenceDenseColumn.TensorSequenceLengthPair(
dense_tensor=dense_tensor, sequence_length=seq_length)
示例9: _testSparseReduceShape
def _testSparseReduceShape(self, sp_t, reduction_axes, ndims, keep_dims,
do_sum):
densified = self.evaluate(sparse_ops.sparse_tensor_to_dense(sp_t))
np_op = np.sum
tf_op = sparse_ops.sparse_reduce_sum
if not do_sum:
np_op = np.max
tf_op = sparse_ops.sparse_reduce_max
np_ans = densified
if reduction_axes is None:
np_ans = np_op(np_ans, keepdims=keep_dims)
else:
if not isinstance(reduction_axes, list): # Single scalar.
reduction_axes = [reduction_axes]
reduction_axes = np.array(reduction_axes).astype(np.int32)
# Handles negative axes.
reduction_axes = (reduction_axes + ndims) % ndims
# Loop below depends on sorted.
reduction_axes.sort()
for ra in reduction_axes.ravel()[::-1]:
np_ans = np_op(np_ans, axis=ra, keepdims=keep_dims)
tf_ans = tf_op(sp_t, reduction_axes, keep_dims)
self.assertAllEqual(np_ans.shape, tf_ans.get_shape().as_list())
示例10: _get_sequence_dense_tensor
def _get_sequence_dense_tensor(
self, inputs, weight_collections=None, trainable=None):
# Do nothing with weight_collections and trainable since no variables are
# created in this function.
del weight_collections
del trainable
sp_tensor = inputs.get(self)
dense_tensor = sparse_ops.sparse_tensor_to_dense(
sp_tensor, default_value=self.default_value)
# Reshape into [batch_size, T, variable_shape].
dense_shape = array_ops.concat(
[array_ops.shape(dense_tensor)[:1], [-1], self._variable_shape],
axis=0)
dense_tensor = array_ops.reshape(dense_tensor, shape=dense_shape)
# Get the number of timesteps per example
# For the 2D case, the raw values are grouped according to num_elements;
# for the 3D case, the grouping happens in the third dimension, and
# sequence length is not affected.
num_elements = (self._variable_shape.num_elements()
if sp_tensor.shape.ndims == 2 else 1)
seq_length = fc._sequence_length_from_sparse_tensor(
sp_tensor, num_elements=num_elements)
return fc._SequenceDenseColumn.TensorSequenceLengthPair(
dense_tensor=dense_tensor, sequence_length=seq_length)
示例11: testDenseSequencesToSparse
def testDenseSequencesToSparse(self):
labels = [[1, 3, 3, 3, 0],
[1, 4, 4, 4, 0],
[4, 2, 2, 9, 4]]
length = [4, 5, 5]
sparse = ctc_ops.dense_labels_to_sparse(labels, length)
new_dense = sparse_ops.sparse_tensor_to_dense(sparse)
self.assertAllEqual(labels, new_dense)
padded_labels = [[1, 3, 3, 3, 0, 0, 0, 0],
[1, 4, 4, 4, 0, 0, 0, 0],
[4, 2, 2, 9, 4, 0, 0, 0]]
length = [4, 5, 5]
sparse = ctc_ops.dense_labels_to_sparse(padded_labels, length)
padded_dense = sparse_ops.sparse_tensor_to_dense(sparse)
self.assertAllEqual(padded_dense, new_dense)
示例12: call
def call(self, inputs):
if isinstance(inputs, ragged_tensor.RaggedTensor):
return inputs.to_tensor(default_value=self._default_value)
elif isinstance(inputs, sparse_tensor.SparseTensor):
return sparse_ops.sparse_tensor_to_dense(
inputs, default_value=self._default_value)
elif isinstance(inputs, ops.Tensor):
return inputs
else:
raise TypeError("Unexpected tensor type %s" % type(inputs).__name__)
示例13: testPaddingOnlySparse
def testPaddingOnlySparse(self):
ind1 = np.array([[0], [2]])
val1 = np.array([3, 4])
shape1 = np.array([4])
ind2 = np.array([[1], [2]])
val2 = np.array([9, 12])
shape2 = np.array([5])
with ops.Graph().as_default() as g, self.test_session(graph=g):
sp_tensor1 = sparse_tensor.SparseTensor(
indices=array_ops.constant(ind1, dtypes.int64),
values=array_ops.constant(val1, dtypes.int64),
dense_shape=array_ops.constant(shape1, dtypes.int64))
sp_tensor2 = sparse_tensor.SparseTensor(
indices=array_ops.constant(ind2, dtypes.int64),
values=array_ops.constant(val2, dtypes.int64),
dense_shape=array_ops.constant(shape2, dtypes.int64))
sp_tensor1_expected = sparse_tensor.SparseTensor(
indices=sp_tensor1.indices,
values=sp_tensor1.values,
dense_shape=[8])
sp_tensor2_expected = sparse_tensor.SparseTensor(
indices=sp_tensor2.indices,
values=sp_tensor2.values,
dense_shape=[8])
sequences = {
"key_1": sp_tensor1,
"key_2": sp_tensor2,
}
_, padded_seq = sqss._padding(sequences, 4)
expected_padded_seq = {
"key_1": sp_tensor1_expected,
"key_2": sp_tensor2_expected,
}
for key, val in expected_padded_seq.items():
self.assertAllEqual(
sparse_ops.sparse_tensor_to_dense(val).eval(),
sparse_ops.sparse_tensor_to_dense(padded_seq[key]).eval())
示例14: tensors_to_item
def tensors_to_item(self, keys_to_tensors):
tensor = keys_to_tensors[self._tensor_key]
shape = self._shape
if self._shape_keys:
shape_dims = []
for k in self._shape_keys:
shape_dim = keys_to_tensors[k]
if isinstance(shape_dim, ops.SparseTensor):
shape_dim = sparse_ops.sparse_tensor_to_dense(shape_dim)
shape_dims.append(shape_dim)
shape = array_ops.squeeze(array_ops.pack(shape_dims))
if isinstance(tensor, ops.SparseTensor):
if shape is not None:
tensor = sparse_ops.sparse_reshape(tensor, shape)
tensor = sparse_ops.sparse_tensor_to_dense(tensor, self._default_value)
else:
if shape is not None:
tensor = array_ops.reshape(tensor, shape)
return tensor
示例15: testConsumers
def testConsumers(self):
sp = sparse_tensor.SparseTensor([[0, 0], [1, 2]], [1.0, 3.0], [3, 4])
w = ops.convert_to_tensor(np.ones([4, 1], np.float32))
out = sparse_ops.sparse_tensor_dense_matmul(sp, w)
self.assertEqual(len(sp.consumers()), 1)
self.assertEqual(sp.consumers()[0], out.op)
dense = sparse_ops.sparse_tensor_to_dense(sp)
self.assertEqual(len(sp.consumers()), 2)
self.assertTrue(dense.op in sp.consumers())
self.assertTrue(out.op in sp.consumers())