首页 文章

张量流输入变量误差

提问于
浏览
0

我正在创建张量流代码,并在尝试使用变量运行时出错 .

基本代码是

import tensor flow as tf
import numpy as np
graph = tf.Graph()
with graph.as_default():
with tf.name_scope("variables"):
    # keep track of how many times the model has been run
    global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")
    # keep track of sum of all outputs over time
    total_output = tf.Variable(0, dtype=tf.float32, trainable=False, name="total_output")
with tf.name_scope("transformation"):
    # separate input layer
    with tf.name_scope("input"):
        # create input placeholder which takes in a vector
        a = tf.placeholder(tf.float32, shape=[None], name = "input_placeholder_A")
    #separate the middle layer
    with tf.name_scope("middle"):
        b = tf.reduce_prod(a, name = "product_b")
        c = tf.reduce_sum(a, name = "sum_c")
    # separate the output layer
    with tf.name_scope("output"):
        output = tf.add(b,c, name="output")
# separate the update layer and store the variables
with tf.name_scope("update"):
    update_total = total_output.assign(output)
    increment_step = global_step.assign_add(1)
# now create namescope summaries and store these in the summary
with tf.name_scope("summaries"):
    avg = tf.divide(update_total, tf.cast(increment_step, tf.float32), name = "average")
    # create summary for output node
    tf.summary.scalar("output_summary", output)
    tf.summary.scalar("total_summary",update_total)
    tf.summary.scalar("average_summary",avg)
with tf.name_scope("global_ops"):
    init = tf.initialize_all_variables()
    merged_summaries = tf.summary.merge_all()
sess = tf.Session(graph=graph)
writer = tf.summary.FileWriter('./improved_graph', graph)
sess.run(init)
def run_graph(input_tensor):
    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_graph([2,8])

我收到了错误


InvalidArgumentError Traceback(最近一次调用最后一次)InvalidArgumentError(请参见上面的回溯):您必须为占位符张量'transformation_2 / input / input_placeholder_A'提供一个值,其中dtype为float和shape [?] [[Node:transformation_2 / input / input_placeholder_A = Placeholderdtype = DT_FLOAT,shape = [?],_ device =“/ job:localhost / replica:0 / task:0 / device:CPU:0”]]

我不明白我在做错了什么,因为代码已经针对所安装的张量流的版本进行了修正 .

1 回答

  • 1

    您的占位符 a 定义为 float32 类型,但 [5, 8] 包含 int 值 .

    run_graph([2., 8.])run_graph(np.array([5, 8], dtype=np.float32)) 应该有效 .

相关问题