ValueError: Passed Tensor(...) 应该具有等于当前图的图属性 [英] ValueError: Passed Tensor(...) should have graph attribute that is equal to current graph

查看:34
本文介绍了ValueError: Passed Tensor(...) 应该具有等于当前图的图属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根本找不到 tensorflow 的问题.应该是简单的东西.下面的示例(带有噪声分类的简单 XOR)提出了以下问题:

I simply can't find the issue with tensorflow. Should be something simple. The example (simple XOR with noise classification) below raises the following issue:

ValueError: Passed Tensor("training_loss:0", shape=(), dtype=float32) 的图形属性应该等于当前图形 ;.

我根本没有看到问题.

import numpy as np
import pandas as pd
import tensorflow as tf 

def xor_data():
    np.random.seed(423)
    rows = 1000
    base_cases = [(1, 1, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1)]
    frames = list()

    for case in base_cases:
        tmp_df = pd.DataFrame(
            np.random.uniform(low=-0.3, high=0.3, size=(rows, 2)),
                              columns=['x_1', 'x_2'])
        tmp_df['x_1'] += case[0]
        tmp_df['x_2'] += case[1]
        tmp_df['y'] = case[2]
        frames.append(tmp_df)

    return pd.concat(frames, ignore_index=True)

def xor_fun():
    x_1 = tf.contrib.layers.real_valued_column("x_1")
    x_2 = tf.contrib.layers.real_valued_column("x_2")

    model = tf.contrib.learn.DNNClassifier(hidden_units=[2,2 ],
                                          feature_columns=[x_1, x_2])
    df = xor_data()
    feature_cols = {
        'x_1': tf.constant(value=df['x_1'].values),
        'x_2': tf.constant(value=df['x_2'].values)}

    labels = tf.constant(value=df['y'].values)

    def input_fn():
        return feature_cols, labels

    model.fit(input_fn=input_fn, steps=50)

if __name__ == '__main__':
    xor_fun()

推荐答案

从闭包返回特征或标签失败,因为在调用 model.fit 时创建了一个新的 tf.Graph,因此对图的任何修改(例如 tf.contrib 调用)都需要在 input_fn 内进行(因此在新图实例化之后)).

Returning the features or labels from a closure fails because a new tf.Graph is created when you call model.fit, so any modifications to the graph (e.g. tf.contrib calls) need to be made from within the input_fn (and therefore after the new graph has been instantiated).

为了演示,这是有效的

import numpy as np
import tensorflow as tf

def input_fn():
    x = np.array([1., 2., 3., 4.])
    y = np.array([0., -1., -2., -3.])
    feature_cols = {'x': tf.constant(x)}
    labels = tf.constant(y)
    return feature_cols, labels

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
estimator.fit(input_fn=input_fn, steps=100)
print(estimator.evaluate(input_fn=input_fn, steps=1))

但这没有

import numpy as np
import tensorflow as tf

x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
feature_cols = {'x': tf.constant(x)}
labels = tf.constant(y)
input_fn = lambda: feature_cols, labels

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
estimator.fit(input_fn=input_fn, steps=100)
print(estimator.evaluate(input_fn=input_fn, steps=1))

另见此答案https://stackoverflow.com/a/39400592/6536722

这篇关于ValueError: Passed Tensor(...) 应该具有等于当前图的图属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆