Using the Tensorflow input pipeline with skflow / tf learn

I followed the Tensorflow Read Data instruction to retrieve my application data in the form of TFRecords, and use TFRecordReader in my input pipelines to read this data.

Now I am reading manuals on using skflow / tf.learn to create a simple regressor, but I don’t see how to use my input with these tools.

In the following code, the application does not work when calling regressor.fit(..) with ValueError: setting an array element with a sequence. .

Mistake:

 Traceback (most recent call last): File ".../tf.py", line 138, in <module> run() File ".../tf.py", line 86, in run regressor.fit(x, labels) File ".../site-packages/tensorflow/contrib/learn/python/learn/estimators/base.py", line 218, in fit self.batch_size) File ".../site-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 99, in setup_train_data_feeder return data_feeder_cls(X, y, n_classes, batch_size) File ".../site-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 191, in __init__ self.X = check_array(X, dtype=x_dtype) File ".../site-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 161, in check_array array = np.array(array, dtype=dtype, order=None, copy=False) ValueError: setting an array element with a sequence. 

the code:

 import tensorflow as tf import tensorflow.contrib.learn as learn def inputs(): with tf.name_scope('input'): filename_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, feature_spec) labels = features.pop('actual') some_feature = features['some_feature'] features_batch, labels_batch = tf.train.shuffle_batch( [some_feature, labels], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue) return features_batch, labels_batch def run(): with tf.Graph().as_default(): x, labels = inputs() # regressor = learn.TensorFlowDNNRegressor(hidden_units=[10, 20, 10]) regressor = learn.TensorFlowLinearRegressor() regressor.fit(x, labels) ... 

It appears that calling check_array expecting a real array, not a tensor. Is there anything I can do to massage my data into the right form?

+7
python tensorflow skflow
source share
1 answer

It looks like the API you worked with is depreciating. If you are using the more modern tf.contrib.learn.LinearRegressor (think> = 1.0), you should specify input_fn , which basically creates inputs and labels. I think in your example, it will be as simple as changing your run function to:

 def run(): with tf.Graph().as_default(): regressor = tf.contrib.learn.LinearRegressor() regressor.fit(input_fn=my_input_fn) 

and then define an input function called my_input_fn . From docs, this input function takes the form:

 def my_input_fn(): # Preprocess your data here... # ...then return 1) a mapping of feature columns to Tensors with # the corresponding feature data, and 2) a Tensor containing labels return feature_cols, labels 

I think the documentation can bring you to the end. It’s hard for me to say how you should proceed without seeing your data.

+1
source share

All Articles