【问题标题】:ValueError: Tensor must be from the same graph as TensorValueError:张量必须来自与张量相同的图
【发布时间】:2017-03-01 08:46:13
【问题描述】:

我正在尝试在 tensorflow 中构建图形,但遇到以下错误:

ValueError: Tensor(transformation_0/output/output: 0", shape=(), dtype=float32 ) 必须来自同一图表 Tensor("variables/total_output: 0", shape=(), dtype=float32_ref)

代码如下:

import tensorflow as tf
# Explicitly create a Graph object
graph =tf.Graph() 
with graph.as_default():

    with tf.name_scope("variables"):
    # Variable to keep track of how many times the graph has been run
    global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")

    # Variable that keeps track of the sum of all output values over time:
    total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")

   # Primary transformation Operations
   with tf.name_scope("transformation"):

       # Separate input layer
       with tf.name_scope("input"):
       # Create input placeholder- takes in a Vector
       a = tf.placeholder(tf.float32, shape=[None],name="input_placeholder_a")

       # Separate middle layer
       with tf.name_scope("intermediate_layer"):
        b = tf.reduce_prod(a, name="product_b")
        c = tf.reduce_sum(a, name="sum_c")

       # Separate output layer
       with tf.name_scope("output"):
        output = tf.add(b, c, name="output")

   with tf.name_scope("update"):
       # Increments the total_output Variable by the latest output
    update_total = total_output.assign_add(output)

      # Increments the above `global_step` Variable, should be run whenever     #the graph is run
    increment_step = global_step.assign_add(1)

# Summary Operations
with tf.name_scope("summaries"):
    avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")

    # Creates summaries for output node
    tf.scalar_summary(b'Output', output, name="output_summary")
    tf.scalar_summary(b'Sum of outputs over time', update_total, name="total_summary")
    tf.scalar_summary(b'Average of outputs over time', avg, name="average_summary")

# Global Variables and Operations
with tf.name_scope("global_ops"):
    # Initialization Op
 init = tf.initialize_all_variables()
    # Merge all summaries into one Operation
    merged_summaries = tf.merge_all_summaries()

# Start a Session, using the explicitly created Graph
sess = tf.Session(graph=graph)

# Open a SummaryWriter to save summaries
writer = tf.train.SummaryWriter('./improved_graph', graph)

# Initialize Variables
sess.run(init)


def run_graph(input_tensor):
"""
Helper function; runs the graph with given input tensor and saves summaries
"""
feed_dict = {a: input_tensor}
_, step, summary = sess.run([output, increment_step, merged_summaries],
                              feed_dict=feed_dict)
writer.add_summary(summary, global_step=step)
# Run the graph with various inputs
run_graph([2,8])
run_graph([3,1,3,3])
run_graph([8])
run_graph([1,2,3])
run_graph([11,4])
run_graph([4,1])
run_graph([7,3,1])
run_graph([6,3])
run_graph([0,2])
run_graph([4,5,6])

# Write the summaries to disk
writer.flush()

# Close the SummaryWriter
writer.close()

# Close the session
sess.close()

【问题讨论】:

    标签: python-2.7 tensorflow


    【解决方案1】:

    你试过了吗:

    1) 变化

    graph =tf.Graph() 
    with graph.as_default()
    

    为:

    with tf.Session() as sess:
    

    2) 并删除:

    sess = tf.Session(graph=graph)
    

    我遇到了同样的错误,这些更改解决了它。

    【讨论】:

      【解决方案2】:

      试试这个,删除 shape=[None]

      a = tf.placeholder(tf.float32, name="input_placeholder_a")
      

      【讨论】:

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