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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With