Skip to content

Commit 2939f53

Browse files
ebrevdotensorflower-gardener
authored andcommitted
Move SparseTensor into its own framework.py, add get_shape().
Change: 137887950
1 parent 0a170e4 commit 2939f53

51 files changed

Lines changed: 638 additions & 485 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tensorflow/contrib/factorization/python/ops/factorization_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def _process_input_helper(self, update_row_factors,
655655
update_op: An op that assigns the newly computed values to the row/column
656656
factors.
657657
"""
658-
assert isinstance(sp_input, ops.SparseTensor)
658+
assert isinstance(sp_input, tf.SparseTensor)
659659

660660
if update_row_factors:
661661
left = self._row_factors

tensorflow/contrib/framework/python/framework/tensor_util.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import numpy as np
2121
from tensorflow.python.framework import dtypes
2222
from tensorflow.python.framework import ops
23+
from tensorflow.python.framework import sparse_tensor
2324
from tensorflow.python.ops import array_ops
2425
from tensorflow.python.ops import control_flow_ops
2526
from tensorflow.python.ops import math_ops
@@ -283,7 +284,7 @@ def is_tensor(x):
283284
Returns:
284285
`True` if `x` is a tensor, `False` if not.
285286
"""
286-
tensor_types = (ops.Tensor, ops.SparseTensor, variables.Variable)
287+
tensor_types = (ops.Tensor, sparse_tensor.SparseTensor, variables.Variable)
287288
return isinstance(x, tensor_types)
288289

289290

@@ -303,7 +304,7 @@ def with_shape(expected_shape, tensor):
303304
Raises:
304305
ValueError: if tensor has an invalid shape.
305306
"""
306-
if isinstance(tensor, ops.SparseTensor):
307+
if isinstance(tensor, sparse_tensor.SparseTensor):
307308
raise ValueError('SparseTensor not supported.')
308309

309310
# Shape type must be 1D int32.
@@ -376,9 +377,9 @@ def convert_to_tensor_or_sparse_tensor(
376377
"""
377378
if dtype is not None:
378379
dtype = dtypes.as_dtype(dtype)
379-
if isinstance(value, ops.SparseTensorValue):
380-
value = ops.SparseTensor.from_value(value)
381-
if isinstance(value, ops.SparseTensor):
380+
if isinstance(value, sparse_tensor.SparseTensorValue):
381+
value = sparse_tensor.SparseTensor.from_value(value)
382+
if isinstance(value, sparse_tensor.SparseTensor):
382383
if dtype and not dtype.is_compatible_with(value.dtype):
383384
raise RuntimeError(
384385
'Sparse dtype: requested = %s, actual = %s' % (

tensorflow/contrib/framework/python/ops/prettyprint_ops.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from tensorflow.python.framework import constant_op
2323
from tensorflow.python.framework import dtypes
2424
from tensorflow.python.framework import ops
25+
from tensorflow.python.framework import sparse_tensor
2526
from tensorflow.python.ops import array_ops
2627
from tensorflow.python.ops import logging_ops
2728
from tensorflow.python.ops import math_ops
@@ -43,15 +44,15 @@ def _get_tensor_repr(t,
4344
if print_tensor_type:
4445
if isinstance(t, ops.Tensor):
4546
t_type_str = "Type: Tensor ({})".format(t.dtype.name)
46-
elif isinstance(t, ops.SparseTensor):
47+
elif isinstance(t, sparse_tensor.SparseTensor):
4748
t_type_str = "Type: SparseTensor ({})".format(t.dtype.name)
4849
elif isinstance(t, tensor_array_ops.TensorArray):
4950
t_type_str = "Type: TensorArray ({})".format(t.dtype.name)
5051

5152
tensor_list.append(constant_op.constant(t_type_str))
5253

5354
if print_shape:
54-
if isinstance(t, ops.SparseTensor):
55+
if isinstance(t, sparse_tensor.SparseTensor):
5556
tensor_list.append(constant_op.constant("Shape:"))
5657
tensor_list.append(t.shape)
5758
elif isinstance(t, ops.Tensor):
@@ -66,7 +67,7 @@ def _get_tensor_repr(t,
6667
tensor_list.append(constant_op.constant("First True in Boolean tensor at:"))
6768
tensor_list.append(math_ops.argmax(int_tensor, 0))
6869

69-
if isinstance(t, ops.SparseTensor):
70+
if isinstance(t, sparse_tensor.SparseTensor):
7071
tensor_list.append(constant_op.constant("Sparse indices:"))
7172
tensor_list.append(t.indices)
7273
tensor_list.append(constant_op.constant("Sparse values:"))
@@ -137,15 +138,15 @@ def print_op(input_,
137138
if isinstance(input_, ops.Tensor):
138139
input_ = logging_ops.Print(input_, tensor_list, message, first_n, summarize,
139140
name)
140-
elif isinstance(input_, ops.SparseTensor):
141+
elif isinstance(input_, sparse_tensor.SparseTensor):
141142
p = logging_ops.Print(
142143
constant_op.constant([]), tensor_list, message, first_n, summarize,
143144
name)
144145

145146
with ops.control_dependencies([p]):
146-
input_ = ops.SparseTensor(array_ops.identity(input_.indices),
147-
array_ops.identity(input_.values),
148-
array_ops.identity(input_.shape))
147+
input_ = sparse_tensor.SparseTensor(array_ops.identity(input_.indices),
148+
array_ops.identity(input_.values),
149+
array_ops.identity(input_.shape))
149150
elif isinstance(input_, tensor_array_ops.TensorArray):
150151
p = logging_ops.Print(
151152
constant_op.constant([]), tensor_list, message, first_n, summarize,

tensorflow/contrib/layers/python/layers/embedding_ops.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from tensorflow.python.framework import dtypes
2424
from tensorflow.python.framework import ops
25+
from tensorflow.python.framework import sparse_tensor
2526
from tensorflow.python.framework import tensor_shape
2627
from tensorflow.python.ops import array_ops
2728
from tensorflow.python.ops import embedding_ops
@@ -114,8 +115,9 @@ def safe_embedding_lookup_sparse(embedding_weights,
114115
array_ops.slice(original_shape, [0], [original_rank - 1])),
115116
array_ops.gather(original_shape, original_rank - 1)])
116117
if sparse_weights is not None:
117-
sparse_weights = ops.SparseTensor(sparse_ids.indices,
118-
sparse_weights.values, sparse_ids.shape)
118+
sparse_weights = sparse_tensor.SparseTensor(
119+
sparse_ids.indices,
120+
sparse_weights.values, sparse_ids.shape)
119121

120122
# Prune invalid ids and weights.
121123
sparse_ids, sparse_weights = _prune_invalid_ids(sparse_ids, sparse_weights)
@@ -302,7 +304,7 @@ def hashed_embedding_lookup_sparse(params,
302304
params = list(params)
303305
if not isinstance(params, list):
304306
params = [params]
305-
if not isinstance(sparse_values, ops.SparseTensor):
307+
if not isinstance(sparse_values, sparse_tensor.SparseTensor):
306308
raise TypeError("sparse_values must be SparseTensor")
307309

308310
with ops.name_scope(name, "hashed_sparse_embedding_lookup",

tensorflow/contrib/layers/python/layers/encoders.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from tensorflow.contrib.framework.python.ops import variables
2222
from tensorflow.contrib.layers.python.layers import embedding_ops as contrib_embedding_ops
2323
from tensorflow.contrib.layers.python.ops import sparse_ops
24-
from tensorflow.python.framework import ops
24+
from tensorflow.python.framework import sparse_tensor
2525
from tensorflow.python.ops import embedding_ops
2626
from tensorflow.python.ops import math_ops
2727
from tensorflow.python.ops import variable_scope
@@ -74,14 +74,14 @@ def bow_encoder(ids,
7474
initializer=initializer, regularizer=regularizer,
7575
trainable=trainable)
7676
if sparse_lookup:
77-
if isinstance(ids, ops.SparseTensor):
77+
if isinstance(ids, sparse_tensor.SparseTensor):
7878
sparse_ids = ids
7979
else:
8080
sparse_ids = sparse_ops.dense_to_sparse_tensor(ids)
8181
return contrib_embedding_ops.safe_embedding_lookup_sparse(
8282
[embeddings], sparse_ids, combiner='mean', default_id=0)
8383
else:
84-
if isinstance(ids, ops.SparseTensor):
84+
if isinstance(ids, sparse_tensor.SparseTensor):
8585
raise TypeError('ids are expected to be dense Tensor, got: %s', ids)
8686
return math_ops.reduce_mean(
8787
embedding_ops.embedding_lookup(embeddings, ids),

tensorflow/contrib/layers/python/layers/feature_column.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
from tensorflow.contrib.layers.python.ops import sparse_feature_cross_op
8282
from tensorflow.contrib.lookup import lookup_ops as contrib_lookup_ops
8383
from tensorflow.python.framework import dtypes
84-
from tensorflow.python.framework import ops
84+
from tensorflow.python.framework import sparse_tensor as sparse_tensor_py
8585
from tensorflow.python.ops import array_ops
8686
from tensorflow.python.ops import init_ops
8787
from tensorflow.python.ops import math_ops
@@ -390,7 +390,7 @@ def insert_transformed_feature(self, columns_to_tensors):
390390
sparse_id_values = math_ops.mod(columns_to_tensors[self.name].values,
391391
self.bucket_size,
392392
name="mod")
393-
columns_to_tensors[self] = ops.SparseTensor(
393+
columns_to_tensors[self] = sparse_tensor_py.SparseTensor(
394394
columns_to_tensors[self.name].indices, sparse_id_values,
395395
columns_to_tensors[self.name].shape)
396396

@@ -464,7 +464,7 @@ def insert_transformed_feature(self, columns_to_tensors):
464464

465465
sparse_id_values = string_ops.string_to_hash_bucket_fast(
466466
sparse_values, self.bucket_size, name="lookup")
467-
columns_to_tensors[self] = ops.SparseTensor(
467+
columns_to_tensors[self] = sparse_tensor_py.SparseTensor(
468468
sparse_tensor.indices, sparse_id_values, sparse_tensor.shape)
469469

470470

@@ -1452,7 +1452,8 @@ def to_sparse_tensor(self, input_tensor):
14521452

14531453
indices = math_ops.to_int64(array_ops.transpose(array_ops.pack((i1, i2))))
14541454
shape = math_ops.to_int64(array_ops.pack([batch_size, dimension]))
1455-
sparse_id_values = ops.SparseTensor(indices, bucket_indices, shape)
1455+
sparse_id_values = sparse_tensor_py.SparseTensor(
1456+
indices, bucket_indices, shape)
14561457

14571458
return sparse_id_values
14581459

tensorflow/contrib/layers/python/layers/feature_column_ops.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from tensorflow.contrib.layers.python.layers import layers
2727
from tensorflow.python.framework import dtypes
2828
from tensorflow.python.framework import ops
29+
from tensorflow.python.framework import sparse_tensor as sparse_tensor_py
2930
from tensorflow.python.ops import array_ops
3031
from tensorflow.python.ops import init_ops
3132
from tensorflow.python.ops import math_ops
@@ -362,9 +363,9 @@ def _create_joint_embedding_lookup(columns_to_tensors,
362363
values = t.values + prev_size
363364
prev_size += a.vocab_size
364365
sparse_tensors.append(
365-
ops.SparseTensor(t.indices,
366-
values,
367-
t.shape))
366+
sparse_tensor_py.SparseTensor(t.indices,
367+
values,
368+
t.shape))
368369
sparse_tensor = sparse_ops.sparse_concat(1, sparse_tensors)
369370
with variable_scope.variable_scope(
370371
None, default_name='linear_weights', values=columns_to_tensors.values()):
@@ -695,7 +696,7 @@ def _log_variable(variable):
695696

696697
def _infer_real_valued_column_for_tensor(name, tensor):
697698
"""Creates a real_valued_column for given tensor and name."""
698-
if isinstance(tensor, ops.SparseTensor):
699+
if isinstance(tensor, sparse_tensor_py.SparseTensor):
699700
raise ValueError(
700701
'SparseTensor is not supported for auto detection. Please define '
701702
'corresponding FeatureColumn for tensor {} {}.', name, tensor)

tensorflow/contrib/layers/python/layers/layers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from tensorflow.python.framework import dtypes
3333
from tensorflow.python.framework import ops
34+
from tensorflow.python.framework import sparse_tensor
3435
from tensorflow.python.ops import array_ops
3536
from tensorflow.python.ops import check_ops
3637
from tensorflow.python.ops import init_ops
@@ -1217,7 +1218,7 @@ def _inner_flatten(inputs, new_rank, output_collections=None, scope=None):
12171218
TypeError: `inputs` is not a `Tensor` or `SparseTensor`.
12181219
"""
12191220
with ops.name_scope(scope, 'InnerFlatten', [inputs, new_rank]) as sc:
1220-
if isinstance(inputs, ops.SparseTensor):
1221+
if isinstance(inputs, sparse_tensor.SparseTensor):
12211222
flattened = _sparse_inner_flatten(inputs, new_rank)
12221223
else:
12231224
inputs = ops.convert_to_tensor(inputs)

tensorflow/contrib/layers/python/ops/sparse_feature_cross_op.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from tensorflow.python.framework import common_shapes
2424
from tensorflow.python.framework import dtypes
2525
from tensorflow.python.framework import ops
26+
from tensorflow.python.framework import sparse_tensor
2627
from tensorflow.python.ops import math_ops
2728
from tensorflow.python.platform import resource_loader
2829

@@ -69,12 +70,14 @@ def sparse_feature_cross(inputs, hashed_output=False, num_buckets=0,
6970
"""
7071
if not isinstance(inputs, list):
7172
raise TypeError("Inputs must be a list")
72-
if not all(isinstance(i, ops.SparseTensor) or
73+
if not all(isinstance(i, sparse_tensor.SparseTensor) or
7374
isinstance(i, ops.Tensor) for i in inputs):
7475
raise TypeError("All inputs must be SparseTensors")
7576

76-
sparse_inputs = [i for i in inputs if isinstance(i, ops.SparseTensor)]
77-
dense_inputs = [i for i in inputs if not isinstance(i, ops.SparseTensor)]
77+
sparse_inputs = [i for i in inputs
78+
if isinstance(i, sparse_tensor.SparseTensor)]
79+
dense_inputs = [i for i in inputs
80+
if not isinstance(i, sparse_tensor.SparseTensor)]
7881

7982
indices = [sp_input.indices for sp_input in sparse_inputs]
8083
values = [sp_input.values for sp_input in sparse_inputs]
@@ -117,7 +120,7 @@ def sparse_feature_cross(inputs, hashed_output=False, num_buckets=0,
117120
internal_type=internal_type,
118121
name=name))
119122

120-
return ops.SparseTensor(indices_out, values_out, shape_out)
123+
return sparse_tensor.SparseTensor(indices_out, values_out, shape_out)
121124

122125

123126
ops.RegisterShape("SparseFeatureCross")(common_shapes.call_cpp_shape_fn)

tensorflow/contrib/layers/python/ops/sparse_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from tensorflow.python.framework import dtypes
2222
from tensorflow.python.framework import ops
23+
from tensorflow.python.framework import sparse_tensor
2324
from tensorflow.python.ops import array_ops
2425
from tensorflow.python.ops import math_ops
2526

@@ -78,4 +79,4 @@ def dense_to_sparse_tensor(dense_tensor, ignore_value=None):
7879
math_ops.mul(higher_dims, shape_multipliers), reduction_indices=[1])
7980
flat_indices = math_ops.add(flat_indices, offsets)
8081
values = array_ops.gather(flat_tensor, flat_indices)
81-
return ops.SparseTensor(indices, values, dense_shape)
82+
return sparse_tensor.SparseTensor(indices, values, dense_shape)

0 commit comments

Comments
 (0)