Tensorflow 错误:“Tensor must be from the same graph as Tensor..."; [英] Tensorflow error: "Tensor must be from the same graph as Tensor..."

查看:33
本文介绍了Tensorflow 错误:“Tensor must be from the same graph as Tensor...";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以与 初学者教程 并且在拟合模型时遇到以下错误:

I am trying to train a simple binary logistic regression classifier using Tensorflow (version 0.9.0) in a very similar way to the beginner's tutorial and am encountering the following error when fitting the model:

ValueError: Tensor("centered_bias_weight:0", shape=(1,), dtype=float32_ref) must be from the same graph as Tensor("linear_14/BiasAdd:0", shape=(?, 1), dtype=float32).

这是我的代码:

import tempfile
import tensorflow as tf
import pandas as pd

# Customized training data parsing
train_data = read_train_data()
feature_names = get_feature_names(train_data)
labels = get_labels(train_data)

# Construct dataframe from training data features
x_train = pd.DataFrame(train_data , columns=feature_names)
x_train["label"] = labels

y_train = tf.constant(labels)

# Create SparseColumn for each feature (assume all feature values are integers and either 0 or 1)
feature_cols = [ tf.contrib.layers.sparse_column_with_integerized_feature(f,2) for f in feature_names ]

# Create SparseTensor for each feature based on data
categorical_cols = { f: tf.SparseTensor(indices=[[i,0] for i in range(x_train[f].size)],
               values=x_train[f].values,
               shape=[x_train[f].size,1]) for f in feature_names }

# Initialize logistic regression model
model_dir = tempfile.mkdtemp()
model = tf.contrib.learn.LinearClassifier(feature_columns=feature_cols, model_dir=model_dir)

def eval_input_fun():
    return categorical_cols, y_train

# Fit the model - similarly to the tutorial
model.fit(input_fn=eval_input_fun, steps=200)

我觉得我遗漏了一些关键的东西......也许是教程中假设但没有明确提到的东西?

I feel like I'm missing something critical... maybe something that was assumed in the tutorial but wasn't explicitly mentioned?

此外,每次调用 fit() 时都会收到以下警告:

Also, I get the following warning every time I call fit():

WARNING:tensorflow:create_partitioned_variables is deprecated.  Use tf.get_variable with a partitioner set, or tf.get_partitioned_variable_list, instead.

推荐答案

当你执行 model.fit 时,LinearClassifier创建一个单独的tf.Graph 基于 eval_input_fun 函数中包含的操作.但是,在创建此图的过程中,LinearClassifier 无法访问您全局保存的 categorical_colsy_train 的定义.

When you execute model.fit, the LinearClassifier is creating a separate tf.Graph based on the Ops contained in your eval_input_fun function. But, during the creation of this Graph, LinearClassifier doesn't have access to the definitions of categorical_cols and y_train you saved globally.

解决方案:将所有 Ops 定义(及其依赖项)移至 eval_input_fun

Solution: move all the Ops definitions (and their dependencies) inside eval_input_fun

这篇关于Tensorflow 错误:“Tensor must be from the same graph as Tensor...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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