【问题标题】:Error feeding float value tensorflow错误输入浮点值张量流
【发布时间】:2017-05-30 17:30:06
【问题描述】:

我正在尝试建立一个基本的网络,

    # Suppress OS related warnings
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


# Import tensorflow library
import tensorflow as tf
import numpy as np

sess = tf.Session()


# Input Data X : of placeholder Value 1.0 tf.float32
x = tf.placeholder(tf.float32, name="input")

# Variable Weight : Arbitary Value
w = tf.Variable(0.8, name='weight')

# Neuron : y = w * x
with tf.name_scope('operation'):
    y = tf.multiply(w, x, name='output')

# Actual Output
actual_output = tf.constant(0.0, name="actual_output")

# Loss function , delta square
with tf.name_scope("loss"):
    loss = tf.pow(y - actual_output, 2, name='loss')

# Training Step : Algorithm -> GradientDescentOptimizer
with tf.name_scope("training"):
    train_step = tf.train.GradientDescentOptimizer(0.025).minimize(loss)

# Ploting graph : Tensorboard
for value in [x, w, y, actual_output, loss]:
    tf.summary.scalar(value.op.name, value)

# Merging all summaries : Tensorboard
summaries = tf.summary.merge_all()

# Printing the graph : Tensorboard
summary_writer = tf.summary.FileWriter('log_simple_stats', sess.graph)

# Initialize all variables
sess.run(tf.global_variables_initializer())

for i in range(300):
    summary_writer.add_summary(sess.run(summaries), i)
    sample = np.random.uniform(low=0.0, high=400.0)
    print(sample)
    sess.run(train_step, feed_dict={x: sample})

# Output
print(sess.run([w]))

错误是

您必须使用 dtype 浮点数为占位符张量“输入”提供一个值 [[节点:输入=Placeholderdtype=DT_FLOAT,shape=[],_device="/job:localhost/replica:0/task:0/cpu:0"]]

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    feed dict 的键应该是占位符本身,而不是字符串。另一个问题是,您在运行摘要时使用的提要数据与训练时不同。

    x = tf.placeholder(tf.float32, name="input") ...更多代码... _, 合并 = sess.run([train_step, summaries], feed_dict={x: sample}) summary_writer.add_summary(合并,我)

    【讨论】:

    • 刚刚运行了您的代码。您的另一个问题是您在运行摘要时没有提供相同的数据。让我编辑答案以包含它。
    • 很好地解决了这个问题,但是代码没有按预期工作,你有工作代码吗,你能发布吗?
    • 我将“w”值设为“nan”
    • 不,我所做的只是修复错误。让我看看能否提供一些额外的见解。
    • 你的学习率太高了。如果你运行并打印你的损失或你的输出,你会发现你的网络并没有收敛,而是非常快速地发散,超出了类型范围(因此到处都是 NaN 值)。通过将学习率设置为 0.00001,我让它在我的笔记本电脑上收敛。
    猜你喜欢
    • 2018-10-31
    • 2018-07-06
    • 2017-02-11
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多