Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Passed Tensor(...) should have graph attribute that is equal to current graph

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) should have graph attribute that is equal to current graph <tensorflow.python.framework.ops.Graph object at 0x0000018F142D9AC8>.

I simply do not see the problem.

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()
like image 695
MMCM_ Avatar asked Dec 07 '22 19:12

MMCM_


1 Answers

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).

To demonstrate, this works

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))

but this doesn't

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))

See also this answer https://stackoverflow.com/a/39400592/6536722

like image 187
alcorn Avatar answered Dec 10 '22 18:12

alcorn