Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 似乎无法获得与估计器一起工作的read\u batch\u示例_Python_Python 2.7_Tensorflow - Fatal编程技术网

Python 似乎无法获得与估计器一起工作的read\u batch\u示例

Python 似乎无法获得与估计器一起工作的read\u batch\u示例,python,python-2.7,tensorflow,Python,Python 2.7,Tensorflow,编辑:我正在使用TensorFlow版本0.10.0rc0 我目前正在尝试使用tf.contrib.learn.read\u batch\u示例同时使用TensorFlow(SKFlow/tf.contrib)估计器,特别是线性分类器。我使用tf.decode\u CSV在CSV文件中为parse\u fn参数创建一个read\u batch\u examplesop feed,并使用适当的默认记录。然后,我将该op输入到我的输入\u fn,以拟合估计器,但运行该操作时,我收到以下错误: Val

编辑:我正在使用TensorFlow版本0.10.0rc0

我目前正在尝试使用
tf.contrib.learn.read\u batch\u示例
同时使用TensorFlow(SKFlow/tf.contrib)估计器,特别是
线性分类器
。我使用
tf.decode\u CSV
在CSV文件中为
parse\u fn
参数创建一个
read\u batch\u examples
op feed,并使用适当的默认记录。然后,我将该op输入到我的
输入\u fn
,以拟合估计器,但运行该操作时,我收到以下错误:

ValueError: Tensor("centered_bias_weight:0", shape=(1,), dtype=float32_ref) must be from the same graph as Tensor("linear/linear/BiasAdd:0", shape=(?, 1), dtype=float32).
我很困惑,因为这两个张量似乎都不是来自
read\u batch\u示例
op。如果我事先运行op,然后将输入作为一个值数组馈送,那么代码就会工作。虽然存在这种变通方法,但它没有任何帮助,因为我正在处理需要批量输入的大型数据集。目前正在检查
Estimator.fit
(目前相当于
Estimator.partial_fit
在迭代中的速度比不上能够在训练时输入数据的速度,因此这项工作是理想的。有什么想法吗?我将在下面发布不起作用的代码

def input_fn(examples_dict):
    continuous_cols = {k: tf.cast(examples_dict[k], dtype=tf.float32)
                       for k in CONTINUOUS_FEATURES}
    categorical_cols = {
    k: tf.SparseTensor(
        indices=[[i, 0] for i in xrange(examples_dict[k].get_shape()[0])],
        values=examples_dict[k],
        shape=[int(examples_dict[k].get_shape()[0]), 1])
    for k in CATEGORICAL_FEATURES}
    feature_cols = dict(continuous_cols)
    feature_cols.update(categorical_cols)
    label = tf.contrib.layers.one_hot_encoding(labels=examples_dict[LABEL],
                                               num_classes=2,
                                               on_value=1,
                                               off_value=0)
    return feature_cols, label

filenames = [...]
csv_headers = [...] # features and label headers
batch_size = 50
min_after_dequeue = int(num_examples * min_fraction_of_examples_in_queue)
queue_capacity = min_after_dequeue + 3 * batch_size
examples = tf.contrib.learn.read_batch_examples(
    filenames,
    batch_size=batch_size,
    reader=tf.TextLineReader,
    randomize_input=True,
    queue_capacity=queue_capacity,
    num_threads=1,
    read_batch_size=1,
    parse_fn=lambda x: tf.decode_csv(x, [tf.constant([''], dtype=tf.string) for _ in xrange(csv_headers)]))

examples_dict = {}
for i, header in enumerate(csv_headers):
    examples_dict[header] = examples[:, i]

categorical_cols = []
for header in CATEGORICAL_FEATURES:
    categorical_cols.append(tf.contrib.layers.sparse_column_with_keys(
        header,
        keys  # Keys for that particular feature, source not shown here
    ))
continuous_cols = []
for header in CONTINUOUS_FEATURES:
    continuous_cols.append(tf.contrib.layers.real_valued_column(header))
feature_columns = categorical_cols + continuous_cols

model = tf.contrib.learn.LinearClassifier(
            model_dir=model_dir,
            feature_columns=feature_columns,
            optimizer=optimizer,
            n_classes=num_classes)
# Above code is ok up to this point
model.fit(input_fn=lambda: input_fn(examples_dict),
          steps=200) # This line causes the error ****

任何批处理的替代方案都将受到欢迎!

通过伟大的TensorFlow团队的帮助,我能够找出我的错误!
阅读批处理示例必须在
input\fn
中调用,否则操作必须事先运行,因为它将来自不同的图形

编辑 以下是修改后的代码,适用于感兴趣的人:

def input_fn(file_names, batch_size):
    examples_dict = read_csv_examples(file_names, batch_size)

    # Continuous features
    feature_cols = {k: tf.string_to_number(examples_dict[k], dtype=tf.float32)
                       for k in CONTINUOUS_FEATURES}
    # Categorical features
    feature_cols.update({
        k: tf.SparseTensor(
            indices=[[i, 0] for i in range(examples_dict[k].get_shape()[0])],
            values=examples_dict[k],
            shape=[int(examples_dict[k].get_shape()[0]), 1])
        for k in CATEGORICAL_FEATURES})

    # Change out type for classification/regression
    out_type = tf.int32 if CLASSIFICATION else tf.float32
    label = tf.string_to_number(examples_dict[LABEL], out_type=out_type)

    return feature_cols, label


def read_csv_examples(file_names, batch_size):

    def parse_fn(record):
        record_defaults = [tf.constant(['']), dtype=tf.string] * len(FEATURE_HEADERS)
        return tf.decode_csv(record, record_defaults)

    examples_op = tf.contrib.learn.read_batch_examples(
        file_names,
        batch_size=batch_size,
        reader=tf.TextLineReader,
        parse_fn=parse_fn)

    # Important: convert examples to dict for ease of use in `input_fn`
    # Map each header to its respective column (FEATURE_HEADERS order
    # matters!
    examples_dict_op = {}
    for i, header in enumerate(FEATURE_HEADERS):
        examples_dict_op[header] = examples_op[:, i]

    return examples_dict_op

对于为数据生成通用输入函数而言,此代码几乎是最小的。另外请注意,如果要将
num\u epochs
传递到
read\u batch\u examples
,则需要对分类功能执行一些不同的操作(有关详细信息,请参阅)。免责声明:我写了那个答案。希望这能有所帮助!

我能够通过伟大的TensorFlow团队的帮助找出我的错误!
必须在
input\fn
中调用read\u batch\u示例
,否则必须事先运行op,因为它将来自不同的图形

编辑 以下是修改后的代码,适用于感兴趣的人:

def input_fn(file_names, batch_size):
    examples_dict = read_csv_examples(file_names, batch_size)

    # Continuous features
    feature_cols = {k: tf.string_to_number(examples_dict[k], dtype=tf.float32)
                       for k in CONTINUOUS_FEATURES}
    # Categorical features
    feature_cols.update({
        k: tf.SparseTensor(
            indices=[[i, 0] for i in range(examples_dict[k].get_shape()[0])],
            values=examples_dict[k],
            shape=[int(examples_dict[k].get_shape()[0]), 1])
        for k in CATEGORICAL_FEATURES})

    # Change out type for classification/regression
    out_type = tf.int32 if CLASSIFICATION else tf.float32
    label = tf.string_to_number(examples_dict[LABEL], out_type=out_type)

    return feature_cols, label


def read_csv_examples(file_names, batch_size):

    def parse_fn(record):
        record_defaults = [tf.constant(['']), dtype=tf.string] * len(FEATURE_HEADERS)
        return tf.decode_csv(record, record_defaults)

    examples_op = tf.contrib.learn.read_batch_examples(
        file_names,
        batch_size=batch_size,
        reader=tf.TextLineReader,
        parse_fn=parse_fn)

    # Important: convert examples to dict for ease of use in `input_fn`
    # Map each header to its respective column (FEATURE_HEADERS order
    # matters!
    examples_dict_op = {}
    for i, header in enumerate(FEATURE_HEADERS):
        examples_dict_op[header] = examples_op[:, i]

    return examples_dict_op

对于为数据生成通用输入函数而言,此代码几乎是最小的。另外请注意,如果要将
num\u epochs
传递到
read\u batch\u examples
,则需要对分类功能执行一些不同的操作(有关详细信息,请参阅)。免责声明:我写了这个答案。希望这有帮助!

我在python3.5上遇到了tensorflow的同样问题,但无法通过移动
读取批处理示例
内部
输入fn
来解决。你能发布(完整)吗更正了代码?这样我就可以知道我的错误在哪里了。@Gersee当然,请查看更新代码的更新答案。希望有帮助!非常感谢。这非常有帮助。我在python3.5上遇到了tensorflow的相同问题,但无法通过移动
读取批处理示例
内部
输入
来修复。请发布(完整)该问题更正代码?这样我就可以知道我的错误在哪里了。@Gersee当然可以,查看更新代码的更新答案。希望有帮助!非常感谢。这非常有帮助。