使用Tensorflow输入管道与skflow/tf learn

7
我已经按照Tensorflow 读取数据指南,将我的应用程序的数据转换为TFRecords格式,并在输入流水线中使用TFRecordReader读取这些数据。
我现在正在阅读有关使用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函数需要真正的数组,而不是张量。我能做些什么来调整我的数据,使其符合要求呢?


如果在调用regressor.fit之前执行x = x.eval()和labels = labels.eval()会发生什么?这将把张量计算为数组,但我怀疑这不是使用skflow的正确方法... - mathetes
@mathetes,这似乎可以工作,但在我继续之前,这是“tf-y”处理事情的方式吗?我的直觉是TF图应该移动数据,而不是我的程序。 - Mark McDonald
当然,非常抱歉我没有说明,但这只是一种调试的方式。这就是为什么我发布了一个评论而不是一个答案。不过我无法再帮你了,因为我不熟悉skflow。 - mathetes
1个回答

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的输入函数。根据文档,这个输入函数的形式如下:

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
你说得对,这是一个旧问题,我现在已经远离它了。感谢您提供一个有用的、当前的解决方案。 - Mark McDonald

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接