张量流输入变量误差

时间:2018-05-21 14:55:11

标签: python tensorflow

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

基本代码是

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_placeholder_A'与dtype浮动和   shape [?] [[Node:transformation_2 / input / input_placeholder_A =   Placeholderdtype = DT_FLOAT,shape = [?],   _device =" /作业:本地主机/复制:0 /任务:0 /装置:CPU:0"]]

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

1 个答案:

答案 0 :(得分:1)

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

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