【问题标题】:tensor flow input variable error张量流输入变量错误
【发布时间】:2018-10-31 06:20:33
【问题描述】:

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

基本代码是

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 浮点数和 形状 [?][[节点:transformation_2/input/input_placeholder_A = Placeholderdtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

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

【问题讨论】:

  • 首先导入 tensorflow 而不是 tensor flow
  • 对不起,它导入了 tensorflow

标签: python tensorflow


【解决方案1】:

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

run_graph([2., 8.])run_graph(np.array([5, 8], dtype=np.float32)) 应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 2017-02-11
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    相关资源
    最近更新 更多