Improve error messages in feature_column_ops.

Change: 126416482
This commit is contained in:
A. Unique TensorFlower 2016-07-01 08:21:31 -08:00 committed by TensorFlower Gardener
parent 242fe922e4
commit 487252db6b
2 changed files with 43 additions and 22 deletions

View File

@ -98,9 +98,13 @@ def input_from_feature_columns(columns_to_tensors,
[ops.GraphKeys.VARIABLES]))
for column in sorted(set(feature_columns), key=lambda x: x.key):
transformed_tensor = transformer.transform(column)
output_tensors.append(column.to_dnn_input_layer(
transformed_tensor, weight_collections, trainable))
try:
transformed_tensor = transformer.transform(column)
output_tensors.append(column.to_dnn_input_layer(
transformed_tensor, weight_collections, trainable))
except ValueError as e:
raise ValueError('Error creating input layer for column: {}.\n'
'{}'.format(column.name, e))
return array_ops.concat(1, output_tensors)
@ -174,11 +178,15 @@ def weighted_sum_from_feature_columns(columns_to_tensors,
column_to_variable = dict()
transformer = _Transformer(columns_to_tensors)
for column in sorted(set(feature_columns), key=lambda x: x.key):
transformed_tensor = transformer.transform(column)
predictions, variable = column.to_weighted_sum(transformed_tensor,
num_outputs,
weight_collections,
trainable)
try:
transformed_tensor = transformer.transform(column)
predictions, variable = column.to_weighted_sum(transformed_tensor,
num_outputs,
weight_collections,
trainable)
except ValueError as e:
raise ValueError('Error creating weighted sum for column: {}.\n'
'{}'.format(column.name, e))
output_tensors.append(predictions)
column_to_variable[column] = variable
_log_variable(variable)
@ -305,7 +313,10 @@ def check_feature_columns(feature_columns):
for f in feature_columns:
key = f.key
if key in seen_keys:
raise ValueError('Duplicate feature column key found: %s' % key)
raise ValueError('Duplicate feature column key found for column: {}. '
'This usually means that the column is almost identical '
'to another column, and one must be discarded.'.format(
f.name))
seen_keys.add(key)

View File

@ -341,9 +341,12 @@ class InputLayerTest(tf.test.TestCase):
# Makes sure that trying to use different initializers with the same
# embedding column explicitly fails.
with self.assertRaises(ValueError):
tf.contrib.layers.input_from_feature_columns(
features, [embedded_sparse, embedded_sparse_alternate])
with self.test_session():
with self.assertRaisesRegexp(
ValueError,
"Duplicate feature column key found for column: wire_embedding"):
tf.contrib.layers.input_from_feature_columns(
features, [embedded_sparse, embedded_sparse_alternate])
def testSparseColumn(self):
hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
@ -351,9 +354,11 @@ class InputLayerTest(tf.test.TestCase):
indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2])
features = {"wire": wire_tensor}
with self.assertRaises(ValueError):
tf.initialize_all_variables().run()
tf.contrib.layers.input_layer(features, [hashed_sparse])
with self.test_session():
with self.assertRaisesRegexp(
ValueError, "Error creating input layer for column: wire"):
tf.initialize_all_variables().run()
tf.contrib.layers.input_from_feature_columns(features, [hashed_sparse])
def testCrossedColumn(self):
a = tf.contrib.layers.sparse_column_with_hash_bucket("aaa",
@ -366,9 +371,11 @@ class InputLayerTest(tf.test.TestCase):
indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2])
features = {"aaa": wire_tensor, "bbb": wire_tensor}
with self.assertRaises(ValueError):
tf.initialize_all_variables().run()
tf.contrib.layers.input_layer(features, [crossed])
with self.test_session():
with self.assertRaisesRegexp(
ValueError, "Error creating input layer for column: aaa_X_bbb"):
tf.initialize_all_variables().run()
tf.contrib.layers.input_from_feature_columns(features, [crossed])
def testAllColumns(self):
real_valued = tf.contrib.layers.real_valued_column("income", 3)
@ -477,10 +484,13 @@ class WeightedSumTest(tf.test.TestCase):
shape=[2, 2])
features = {"wire": wire_tensor}
embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
with self.assertRaises(ValueError):
tf.initialize_all_variables().run()
tf.contrib.layers.weighted_sum_from_feature_columns(features,
[embeded_sparse])
with self.test_session():
with self.assertRaisesRegexp(
ValueError, "Error creating weighted sum for column: wire_embedding"):
tf.initialize_all_variables().run()
tf.contrib.layers.weighted_sum_from_feature_columns(features,
[embeded_sparse],
num_outputs=5)
def testRealValuedColumnWithMultiDimensions(self):
real_valued = tf.contrib.layers.real_valued_column("price", 2)