TensorFlow 1.6报错unsupported callable:自定义数据集适配问题求助
unsupported callable Error in TensorFlow 1.6's train_input_fn_custom Let's break down the most likely causes and fixes for this error when adapting the TensorFlow Layers tutorial to your custom dataset:
Check how you're passing
train_input_fn_customto the Estimator
The Estimator'strain()method expects an input function that takes no arguments. If yourtrain_input_fn_customrequires parameters (like dataset paths, batch size, etc.), you need to wrap it in a lambda or usefunctools.partialto create a parameterless callable. For example:# Wrong: Passing the result of calling the function instead of the callable itself estimator.train(train_input_fn_custom(my_dataset_path, batch_size=32), steps=1000) # Correct: Use a lambda to wrap your parameterized function estimator.train(lambda: train_input_fn_custom(my_dataset_path, batch_size=32), steps=1000)If you're already passing the function directly (not calling it), double-check that the function is defined correctly—no typos in the function name, and it's properly imported if it's in another file.
Verify the return value of
train_input_fn_custom
In TensorFlow 1.6, the input function must return one of two valid formats:- A tuple
(features, labels)wherefeaturesis a dictionary of feature names to tensors, andlabelsis a tensor of target values. - A
tf.data.Datasetobject that produces elements matching the(features, labels)structure.
If your function returns something else (like a single tensor, or a non-tensor object), the Estimator can't process it and may throw this error. For example, make sure you're not accidentally returning a numpy array instead of converting it to a TensorFlow tensor withtf.convert_to_tensor().
- A tuple
Check for hidden errors inside the input function
Sometimes theunsupported callableerror is a red herring—your function might be throwing an exception when it's called (like a missing file, invalid tensor shape, or undefined variable), which the Estimator interprets as the callable being unsupported. Add print statements or usetf.debuggingto inspect what's happening insidetrain_input_fn_custom. For example:def train_input_fn_custom(): # Add debug prints to check data loading print("Loading custom dataset...") features = load_features() labels = load_labels() print(f"Features shape: {features.shape}, Labels shape: {labels.shape}") # Convert to tensors if needed features_tensor = tf.convert_to_tensor(features, dtype=tf.float32) labels_tensor = tf.convert_to_tensor(labels, dtype=tf.int32) return features_tensor, labels_tensorEnsure compatibility with TensorFlow 1.6 specifics
TensorFlow 1.6 was an early version for the Estimator API, so some newer Dataset operations might not be supported. If you're usingtf.datamethods introduced after 1.6 (likebatch()withdrop_remainderin later versions), replace them with compatible alternatives. For example, usetf.contrib.data.batch_and_drop_remainder()if you need that behavior in 1.6.
内容的提问来源于stack exchange,提问作者Geoffrey Pruvost

