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])) [ops.GraphKeys.VARIABLES]))
for column in sorted(set(feature_columns), key=lambda x: x.key): for column in sorted(set(feature_columns), key=lambda x: x.key):
transformed_tensor = transformer.transform(column) try:
output_tensors.append(column.to_dnn_input_layer( transformed_tensor = transformer.transform(column)
transformed_tensor, weight_collections, trainable)) 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) return array_ops.concat(1, output_tensors)
@ -174,11 +178,15 @@ def weighted_sum_from_feature_columns(columns_to_tensors,
column_to_variable = dict() column_to_variable = dict()
transformer = _Transformer(columns_to_tensors) transformer = _Transformer(columns_to_tensors)
for column in sorted(set(feature_columns), key=lambda x: x.key): for column in sorted(set(feature_columns), key=lambda x: x.key):
transformed_tensor = transformer.transform(column) try:
predictions, variable = column.to_weighted_sum(transformed_tensor, transformed_tensor = transformer.transform(column)
num_outputs, predictions, variable = column.to_weighted_sum(transformed_tensor,
weight_collections, num_outputs,
trainable) weight_collections,
trainable)
except ValueError as e:
raise ValueError('Error creating weighted sum for column: {}.\n'
'{}'.format(column.name, e))
output_tensors.append(predictions) output_tensors.append(predictions)
column_to_variable[column] = variable column_to_variable[column] = variable
_log_variable(variable) _log_variable(variable)
@ -305,7 +313,10 @@ def check_feature_columns(feature_columns):
for f in feature_columns: for f in feature_columns:
key = f.key key = f.key
if key in seen_keys: 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) 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 # Makes sure that trying to use different initializers with the same
# embedding column explicitly fails. # embedding column explicitly fails.
with self.assertRaises(ValueError): with self.test_session():
tf.contrib.layers.input_from_feature_columns( with self.assertRaisesRegexp(
features, [embedded_sparse, embedded_sparse_alternate]) 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): def testSparseColumn(self):
hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10) 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]], indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2]) shape=[2, 2])
features = {"wire": wire_tensor} features = {"wire": wire_tensor}
with self.assertRaises(ValueError): with self.test_session():
tf.initialize_all_variables().run() with self.assertRaisesRegexp(
tf.contrib.layers.input_layer(features, [hashed_sparse]) 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): def testCrossedColumn(self):
a = tf.contrib.layers.sparse_column_with_hash_bucket("aaa", 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]], indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2]) shape=[2, 2])
features = {"aaa": wire_tensor, "bbb": wire_tensor} features = {"aaa": wire_tensor, "bbb": wire_tensor}
with self.assertRaises(ValueError): with self.test_session():
tf.initialize_all_variables().run() with self.assertRaisesRegexp(
tf.contrib.layers.input_layer(features, [crossed]) 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): def testAllColumns(self):
real_valued = tf.contrib.layers.real_valued_column("income", 3) real_valued = tf.contrib.layers.real_valued_column("income", 3)
@ -477,10 +484,13 @@ class WeightedSumTest(tf.test.TestCase):
shape=[2, 2]) shape=[2, 2])
features = {"wire": wire_tensor} features = {"wire": wire_tensor}
embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10) embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
with self.assertRaises(ValueError): with self.test_session():
tf.initialize_all_variables().run() with self.assertRaisesRegexp(
tf.contrib.layers.weighted_sum_from_feature_columns(features, ValueError, "Error creating weighted sum for column: wire_embedding"):
[embeded_sparse]) tf.initialize_all_variables().run()
tf.contrib.layers.weighted_sum_from_feature_columns(features,
[embeded_sparse],
num_outputs=5)
def testRealValuedColumnWithMultiDimensions(self): def testRealValuedColumnWithMultiDimensions(self):
real_valued = tf.contrib.layers.real_valued_column("price", 2) real_valued = tf.contrib.layers.real_valued_column("price", 2)