python - 将 Tensorflow 输入管道与 skflow/tf learn 结合使用

标签 python tensorflow skflow

我关注了 Tensorflow Reading Data指南以 TFRecord 的形式获取我的应用程序数据,并在我的输入管道中使用 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 调用需要一个真正的数组,而不是张量。我可以做些什么来将我的数据整理成正确的形状吗?

最佳答案

看起来您使用的 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

我认为文档可以帮助您完成剩下的工作。从这里开始,我很难说你应该如何在没有看到你的数据的情况下继续。

关于python - 将 Tensorflow 输入管道与 skflow/tf learn 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37516684/

相关文章:

python - 将对象传递给 python SQL

python - 无法保存和恢复经过训练的 TensorFlow 模型

machine-learning - 重置 tensorflow 元图中输入占位符的形状

python - 如何使用 `input_fn` 和 `read_batch_examples` 设置来创建 `num_epochs`?

python - 在 skflow 的 model_fn 中使用 batch_size

python - Linux CI 服务器上的 GAE

python - 使用python计算长序列

python - Django + PostgreSQL - 1000 次插入/秒,如何加速?

python - Tensorflow:ValueError:在单次检测中为 None 时无法加载 save_path