簡體   English   中英

使用帶有skflow / tf學習功能的Tensorflow輸入管道

[英]Using a Tensorflow input pipeline with skflow/tf learn

我按照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調用期望一個真正的數組,而不是一個張量。 有什么我可以做的來按摩我的數據到正確的形狀?

看起來您正在使用的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

我認為文檔可以讓你完成其余的工作。 對我來說,很難說如何在不看數據的情況下繼續進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM