我只是找不到tensorflow的问题.应该是简单的事情.以下示例(带噪声分类的简单XOR)引发了以下问题:
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>.
我根本没有看到问题.
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()
Run Code Online (Sandbox Code Playgroud)
alc*_*orn 11
从闭包中返回特征或标签失败,因为tf.Graph在调用时会创建一个new model.fit,因此需要对图形(例如tf.contrib调用)进行任何修改input_fn(因此在新图形实例化之后).
为了证明,这是有效的
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))
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
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))
Run Code Online (Sandbox Code Playgroud)
另请参阅此答案/sf/answers/2758041471/
| 归档时间: |
|
| 查看次数: |
2086 次 |
| 最近记录: |