2016-05-30 68 views
7

我已按照Tensorflow Reading Data指南以TFRecords的形式获取我的应用程序数据,并且在我的输入管道中使用TFRecordReader来读取此数据。使用带有skflow/tf的Tensorflow输入管道学习

我正在阅读使用skflow/tf.learn构建简单回归器的指南,但我无法看到如何使用这些工具输入数据。

在以下代码中,应用程序在regressor.fit(..)调用上失败,ValueError: setting an array element with a sequence.

错误:

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. 

代码:

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) 
     ... 

它看起来像check_array呼叫期待一个真正的数组,而不是一个张量。我有什么可以将我的数据按摩到正确的形状吗?

+0

如果在regressor.fit调用之前执行x = x.eval()和labels = labels.eval(),会发生什么情况?这应该评估张量到一个数组中,但我怀疑这是用skflow做这件事的正确方法...... – mathetes

+0

@mathetes,这似乎工作,但在我走下这条道路之前,就是'tf-y'方式做事情?我的直觉是,TF图应该移动数据,而不是我的程序。 –

+0

当然,我很抱歉没有指定,但它只是作为一种调试方式。这就是为什么我发表评论而不是回答。虽然我不能帮助你,但我并不熟悉skflow – mathetes

回答

1

看起来您正在使用的API已折旧。如果您使用更现代的tf.contrib.learn.LinearRegressor(我认为> = 1.0),您应该指定input_fn,它基本上会生成输入和标签。我想在你的榜样,这将是为改变你run功能一样简单:

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

然后定义名为my_input_fn输入功能。从the docs,该输入功能的形式为:

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 

我觉得文档可以让你的方式休息。从这里我很难说出如何在没有看到你的数据的情况下继续下去。

+1

你是对的,这是一个老问题,我现在已经过去了。感谢您提供有用的最新解决方案。 –