本文整理汇总了Python中tensorflow.python.ops.sparse_ops.sparse_tensor_to_dense方法的典型用法代码示例。如果您正苦于以下问题:Python sparse_ops.sparse_tensor_to_dense方法的具体用法?Python sparse_ops.sparse_tensor_to_dense怎么用?Python sparse_ops.sparse_tensor_to_dense使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.sparse_ops
的用法示例。
在下文中一共展示了sparse_ops.sparse_tensor_to_dense方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tensors_to_item
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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, sparse_tensor.SparseTensor):
shape_dim = sparse_ops.sparse_tensor_to_dense(shape_dim)
shape_dims.append(shape_dim)
shape = array_ops.reshape(array_ops.stack(shape_dims), [-1])
if isinstance(tensor, sparse_tensor.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
示例2: to_dense
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def to_dense(tensor):
"""Converts a sparse tensor into a dense tensor and returns it.
Arguments:
tensor: A tensor instance (potentially sparse).
Returns:
A dense tensor.
Examples:
```python
>>> from keras import backend as K
>>> b = K.placeholder((2, 2), sparse=True)
>>> print(K.is_sparse(b))
True
>>> c = K.to_dense(b)
>>> print(K.is_sparse(c))
False
```
"""
if is_sparse(tensor):
return sparse_ops.sparse_tensor_to_dense(tensor)
else:
return tensor
示例3: testCwiseDivAndMul
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def testCwiseDivAndMul(self):
np.random.seed(1618)
sp_shapes = [(10, 10, 10), (5, 5), (1618,), (3, 3, 7)]
dense_shapes = [(10, 10, 1), (5, 5), (1,), (1, 7)]
with self.test_session(use_gpu=False):
for dtype in [np.float32, np.float64, np.int32, np.int64]:
for sp_shape, dense_shape in zip(sp_shapes, dense_shapes):
sp_vals_np = np.random.rand(*sp_shape).astype(dtype) + 1
dense_vals_np = np.random.rand(*dense_shape).astype(dtype) + 1
sp_t, unused_nnz = _sparsify(sp_vals_np, thresh=1.5)
sp_t_densified = sparse_ops.sparse_tensor_to_dense(sp_t).eval()
dense_t = tf.constant(dense_vals_np)
self._check(sp_t / dense_t, sp_t_densified / dense_vals_np, sp_t)
# Check commutative.
self._check(sp_t * dense_t, sp_t_densified * dense_vals_np, sp_t)
self._check(dense_t * sp_t, sp_t_densified * dense_vals_np, sp_t)
if dtype in [np.int32, np.int64]:
res = sp_t / dense_t # should invoke "__truediv__"
self.assertEqual(res.values.eval().dtype, np.float64)
示例4: testRandom
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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 = tf.sparse_maximum(sp_a, sp_b)
maximum_tf_densified = tf.sparse_tensor_to_dense(maximum_tf).eval()
minimum_tf = tf.sparse_minimum(sp_a, sp_b)
minimum_tf_densified = tf.sparse_tensor_to_dense(minimum_tf).eval()
a_densified = tf.sparse_tensor_to_dense(sp_a).eval()
b_densified = tf.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)
示例5: tensors_to_item
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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, sparse_tensor.SparseTensor):
shape_dim = sparse_ops.sparse_tensor_to_dense(shape_dim)
shape_dims.append(shape_dim)
shape = array_ops.reshape(array_ops.pack(shape_dims), [-1])
if isinstance(tensor, sparse_tensor.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
示例6: call
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def call(self, inputs):
if isinstance(inputs, ragged_tensor.RaggedTensor):
# Convert the ragged tensor to a padded uniform tensor
outputs = inputs.to_tensor(default_value=self._pad_value)
elif isinstance(inputs, sparse_tensor.SparseTensor):
# Fill in the missing value in the sparse_tensor
outputs = sparse_ops.sparse_tensor_to_dense(
inputs, default_value=self._pad_value)
elif isinstance(inputs, ops.Tensor):
outputs = inputs
else:
raise TypeError('Unexpected tensor type %s' % type(inputs).__name__)
if self._mask:
outputs = self.masking_layer(outputs)
return outputs
示例7: _load_batch_pair_pose
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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'])
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
示例8: _transform_feature
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def _transform_feature(self, inputs):
"""Returns dense `Tensor` representing feature.
Args:
inputs: A `_LazyBuilder` object to access inputs.
Returns:
Transformed feature `Tensor`.
"""
id_weight_pair = self.categorical_column._get_sparse_tensors(inputs) # pylint: disable=protected-access
id_tensor = id_weight_pair.id_tensor
weight_tensor = id_weight_pair.weight_tensor
# If the underlying column is weighted, return the input as a dense tensor.
if weight_tensor is not None:
weighted_column = sparse_ops.sparse_merge(
sp_ids=id_tensor,
sp_values=weight_tensor,
vocab_size=self._variable_shape[-1])
return sparse_ops.sparse_tensor_to_dense(weighted_column)
dense_id_tensor = sparse_ops.sparse_tensor_to_dense(
id_tensor, default_value=-1)
# One hot must be float for tf.concat reasons since all other inputs to
# input_layer are float32.
one_hot_id_tensor = array_ops.one_hot(
dense_id_tensor,
depth=self._variable_shape[-1],
on_value=1.0,
off_value=0.0)
# Reduce to get a multi-hot per example.
return math_ops.reduce_sum(one_hot_id_tensor, axis=[1])
示例9: ParseLabelTensorOrDict
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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)
示例10: _to_dense_tensor
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def _to_dense_tensor(self, input_tensor):
if isinstance(input_tensor, sparse_tensor_py.SparseTensor):
default_value = (self.default_value[0] if self.default_value is not None
else 0)
return sparse_ops.sparse_tensor_to_dense(
input_tensor, default_value=default_value)
return input_tensor
示例11: _compare
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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)
示例12: testTranspose
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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 = tf.rank(dn_input).eval()
perm = np.random.choice(rank, rank, False)
sp_input, unused_a_nnz = _sparsify(dn_input)
sp_trans = tf.sparse_transpose(sp_input, perm=perm)
dn_trans = tf.sparse_tensor_to_dense(sp_trans).eval()
expected_trans = tf.transpose(dn_input, perm=perm).eval()
self.assertAllEqual(dn_trans, expected_trans)
示例13: ParseLabelTensorOrDict
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
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(
1, [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())]))
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)
示例14: _transform_feature
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def _transform_feature(self, inputs):
"""Returns dense `Tensor` representing feature.
Args:
inputs: A `_LazyBuilder` object to access inputs.
Returns:
Transformed feature `Tensor`.
Raises:
ValueError: if input rank is not known at graph building time.
"""
id_weight_pair = self.categorical_column._get_sparse_tensors(inputs) # pylint: disable=protected-access
id_tensor = id_weight_pair.id_tensor
weight_tensor = id_weight_pair.weight_tensor
# If the underlying column is weighted, return the input as a dense tensor.
if weight_tensor is not None:
weighted_column = sparse_ops.sparse_merge(
sp_ids=id_tensor,
sp_values=weight_tensor,
vocab_size=int(self._variable_shape[-1]))
# Remove (?, -1) index
weighted_column = sparse_ops.sparse_slice(weighted_column, [0, 0],
weighted_column.dense_shape)
return sparse_ops.sparse_tensor_to_dense(weighted_column)
dense_id_tensor = sparse_ops.sparse_tensor_to_dense(
id_tensor, default_value=-1)
# One hot must be float for tf.concat reasons since all other inputs to
# input_layer are float32.
one_hot_id_tensor = array_ops.one_hot(
dense_id_tensor,
depth=self._variable_shape[-1],
on_value=1.0,
off_value=0.0)
# Reduce to get a multi-hot per example.
return math_ops.reduce_sum(one_hot_id_tensor, axis=[-2])
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:42,代码来源:feature_column.py
示例15: _to_dnn_input_layer
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_tensor_to_dense [as 别名]
def _to_dnn_input_layer(self,
transformed_input_tensor,
unused_weight_collections=None,
unused_trainable=False,
output_rank=2):
"""Returns a Tensor as an input to the first layer of neural network.
Args:
transformed_input_tensor: A tensor that has undergone the transformations
in `insert_transformed_feature`. Rank should be >= `output_rank`.
unused_weight_collections: Unused. One hot encodings are not variable.
unused_trainable: Unused. One hot encodings are not trainable.
output_rank: the desired rank of the output `Tensor`.
Returns:
A multihot Tensor to be fed into the first layer of neural network.
Raises:
ValueError: When using one_hot_column with weighted_sparse_column.
This is not yet supported.
"""
# Reshape ID column to `output_rank`.
sparse_id_column = self.sparse_id_column.id_tensor(transformed_input_tensor)
# pylint: disable=protected-access
sparse_id_column = layers._inner_flatten(sparse_id_column, output_rank)
weight_tensor = self.sparse_id_column.weight_tensor(
transformed_input_tensor)
if weight_tensor is not None:
weighted_column = sparse_ops.sparse_merge(sp_ids=sparse_id_column,
sp_values=weight_tensor,
vocab_size=self.length)
return sparse_ops.sparse_tensor_to_dense(weighted_column)
dense_id_tensor = sparse_ops.sparse_tensor_to_dense(sparse_id_column,
default_value=-1)
# One hot must be float for tf.concat reasons since all other inputs to
# input_layer are float32.
one_hot_id_tensor = array_ops.one_hot(
dense_id_tensor, depth=self.length, on_value=1.0, off_value=0.0)
# Reduce to get a multi-hot per example.
return math_ops.reduce_sum(
one_hot_id_tensor, reduction_indices=[output_rank - 1])