本文整理汇总了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函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:jon-sch,项目名称:tensorflow,代码行数:25,代码来源:sparse_ops_test.py
示例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
开发者ID:yanssy,项目名称:PoseFaceGAN,代码行数:25,代码来源:trainer.py
示例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)
开发者ID:LUTAN,项目名称:tensorflow,代码行数:31,代码来源:data_ops.py
示例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)
开发者ID:821760408-sp,项目名称:tensorflow,代码行数:29,代码来源:sparse_ops_test.py
示例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())
开发者ID:1000sprites,项目名称:tensorflow,代码行数:10,代码来源:prettyprint_ops_test.py
示例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
开发者ID:ChaitanyaCixLive,项目名称:tensorflow,代码行数:15,代码来源:tfexample_decoder.py
示例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
开发者ID:AnishShah,项目名称:tensorflow,代码行数:33,代码来源:extenders.py
示例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)
开发者ID:abhinav-upadhyay,项目名称:tensorflow,代码行数:29,代码来源:sequence_feature_column_v2.py
示例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())
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:26,代码来源:sparse_ops_test.py
示例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)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:26,代码来源:sequence_feature_column.py
示例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)
开发者ID:aritratony,项目名称:tensorflow,代码行数:18,代码来源:ctc_loss_op_test.py
示例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__)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:10,代码来源:composite_tensor_support_test.py
示例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())
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:43,代码来源:batch_sequences_with_states_test.py
示例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
开发者ID:821760408-sp,项目名称:tensorflow,代码行数:19,代码来源:tfexample_decoder.py
示例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())
开发者ID:clsung,项目名称:tensorflow,代码行数:11,代码来源:sparse_tensor_test.py
示例16: test_hashed__has_no_collision
def test_hashed__has_no_collision(self):
"""Tests that fingerprint concatenation has no collisions."""
# Although the last 10 bits of 359 and 1024+359 are identical.
# As a result, all the crosses shouldn't collide.
t1 = constant_op.constant([[359], [359 + 1024]])
t2 = constant_op.constant([list(range(10)), list(range(10))])
cross = sparse_ops.sparse_cross_hashed(
[t2, t1], num_buckets=1024, hash_key=sparse_ops._DEFAULT_HASH_KEY + 1)
cross_dense = sparse_ops.sparse_tensor_to_dense(cross)
with session.Session():
values = cross_dense.eval()
self.assertTrue(numpy.not_equal(values[0], values[1]).all())
开发者ID:HughKu,项目名称:tensorflow,代码行数:12,代码来源:sparse_cross_op_test.py
示例17: test_hashed_output_v1_has_collision
def test_hashed_output_v1_has_collision(self):
"""Tests the old version of the fingerprint concatenation has collisions.
"""
# The last 10 bits of 359 and 1024+359 are identical.
# As a result, all the crosses collide.
t1 = constant_op.constant([[359], [359 + 1024]])
t2 = constant_op.constant([list(range(10)), list(range(10))])
cross = sparse_feature_cross_op.sparse_feature_cross(
[t2, t1], hashed_output=True, num_buckets=1024)
cross_dense = sparse_ops.sparse_tensor_to_dense(cross)
with session.Session():
values = cross_dense.eval()
self.assertTrue(numpy.equal(values[0], values[1]).all())
开发者ID:AnishShah,项目名称:tensorflow,代码行数:13,代码来源:sparse_feature_cross_op_test.py
示例18: testTranspose
def testTranspose(self):
with self.test_session(use_gpu=False):
np.random.seed(1618)
shapes = [np.random.randint(1, 10, size=rank) for rank in range(1, 6)]
for shape in shapes:
for dtype in [np.int32, np.int64, np.float32, np.float64]:
dn_input = np.random.randn(*shape).astype(dtype)
rank = array_ops.rank(dn_input).eval()
perm = np.random.choice(rank, rank, False)
sp_input, unused_a_nnz = _sparsify(dn_input)
sp_trans = sparse_ops.sparse_transpose(sp_input, perm=perm)
dn_trans = sparse_ops.sparse_tensor_to_dense(sp_trans).eval()
expected_trans = array_ops.transpose(dn_input, perm=perm).eval()
self.assertAllEqual(dn_trans, expected_trans)
开发者ID:ajaybhat,项目名称:tensorflow,代码行数:14,代码来源:sparse_ops_test.py
示例19: test_hashed_output_v2_has_no_collision
def test_hashed_output_v2_has_no_collision(self):
"""Tests the new version of the fingerprint concatenation has no collisions.
"""
# Although the last 10 bits of 359 and 1024+359 are identical.
# As a result, all the crosses shouldn't collide.
t1 = constant_op.constant([[359], [359 + 1024]])
t2 = constant_op.constant([list(range(10)), list(range(10))])
cross = sparse_feature_cross_op.sparse_feature_cross(
[t2, t1],
hashed_output=True,
num_buckets=1024,
hash_key=layers.SPARSE_FEATURE_CROSS_DEFAULT_HASH_KEY)
cross_dense = sparse_ops.sparse_tensor_to_dense(cross)
with session.Session():
values = cross_dense.eval()
self.assertTrue(numpy.not_equal(values[0], values[1]).all())
开发者ID:AnishShah,项目名称:tensorflow,代码行数:16,代码来源:sparse_feature_cross_op_test.py
示例20: _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)
sequence_length = fc._sequence_length_from_sparse_tensor(
sp_tensor, num_elements=self._variable_shape.num_elements())
return fc._SequenceDenseColumn.TensorSequenceLengthPair(
dense_tensor=dense_tensor, sequence_length=sequence_length)
开发者ID:LiuCKind,项目名称:tensorflow,代码行数:18,代码来源:sequence_feature_column.py
注:本文中的tensorflow.python.ops.sparse_ops.sparse_tensor_to_dense函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论