【问题标题】:Tensorflow: Tensor must be from the same graph as TensorTensorflow:张量必须来自与张量相同的图
【发布时间】:2018-12-02 19:26:31
【问题描述】:

张量流的第一步,我正在尝试训练一个用于图像分类的 DNN 模型。

我当前的代码是:

folder_path = Path('cropped_images/cropped')
df['filename'] = df['tag_id'].map(lambda tag: str(folder_path / (tag + '.png')))

def database_input_fn():

    def parse_image(filename, label):
        image_decoded = tf.image.decode_png(tf.read_file(filename), channels=3)
        image_resized = tf.image.resize_images(image_decoded, [64, 64])
        label = label == 'large vehicle'
        return image_resized, label

    filenames = tf.constant(df['filename'])
    labels = tf.constant(df['general_class'])
    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
    dataset = dataset.map(parse_image)
    dataset = dataset.shuffle()
    dataset = dataset.batch(32)
    dataset = dataset.repeat()
    return dataset

images_fc = tf.feature_column.numeric_column('image', shape=[64, 64, 3])

estimator = tf.estimator.DNNClassifier(feature_columns=[images_fc],
                                     hidden_units=[32, 32, 32, 32])
metrics = estimator.train(lambda : dataset, steps=10000)

其中dfpandas.DataFrame,其中包含图像路径及其对应的标签。图像存储在磁盘上的上述文件夹路径中。

我收到以下错误:

ValueError: Tensor("IteratorV2:0", shape=(), dtype=resource) must be from the same graph as Tensor("BatchDatasetV2_4:0", shape=(), dtype=variant).

我错过了什么?为什么不是所有东西都在同一张图上构建?

【问题讨论】:

    标签: tensorflow tensorflow-estimator


    【解决方案1】:

    我认为

    metrics = estimator.train(lambda : dataset, steps=10000)
    

    可能是问题所在。如果您检查 estimator train 的参数,则 input_fn 构造并返回一个数据集,这意味着为估计器和输入函数创建了一个新图。在您的情况下,您已经在此范围之外创建了该图。可能将您的代码更改为:

    metrics = estimator.train(input_fn=database_input_fn, steps=10000) 
    

    可能会解决这个问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2023-03-22
      • 2018-09-20
      相关资源
      最近更新 更多