gpt4 book ai didi

python - 如何使用 tensorflow 编写摘要日志以对 MNIST 数据进行逻辑回归?

转载 作者:行者123 更新时间:2023-12-01 08:14:46 25 4
gpt4 key购买 nike

我是 tensorflowtensorboard 实现的新手。这是我第一次使用 tensorflow 在 MNIST 数据上实现逻辑回归。我已经成功地对数据实现了逻辑回归,现在我尝试使用 tf.summary .fileWriter 将摘要记录到日志文件中。

这是我的代码,它影响摘要参数

x = tf.placeholder(dtype=tf.float32, shape=(None, 784))
y = tf.placeholder(dtype=tf.float32, shape=(None, 10))

loss_op = tf.losses.mean_squared_error(y, pred)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

tf.summary.scalar("loss", loss_op)
tf.summary.scalar("training_accuracy", accuracy_op)
summary_op = tf.summary.merge_all()

这就是我训练模型的方式

with tf.Session() as sess:   
sess.run(init)
writer = tf.summary.FileWriter('./graphs', sess.graph)

for iter in range(50):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_, loss, tr_acc,summary = sess.run([optimizer_op, loss_op, accuracy_op, summary_op], feed_dict={x: batch_x, y: batch_y})
summary = sess.run(summary_op, feed_dict={x: batch_x, y: batch_y})
writer.add_summary(summary, iter)

添加摘要行以获取合并摘要后,出现以下错误


InvalidArgumentError (see above for traceback):
You must feed a value for placeholder tensor 'Placeholder_37'
with dtype float and shape [?,10]

此错误指向Y的声明

y = tf.placeholder(dtype=tf.float32, shape=(None, 10)) 

你能帮我看看我做错了什么吗?

最佳答案

从错误消息来看,您似乎正在某种 jupyter 环境中运行代码。尝试重新启动内核/运行时并再次运行所有内容。在图形模式下运行代码两次在 jupyter 中效果不佳。如果我运行下面的代码,第一次它不会返回任何错误,当我第二次运行它时(不重新启动内核/运行时),它会像你的那样崩溃。

我懒得在实际模型上检查它,所以我的 pred=y. ;)但下面的代码不会崩溃,因此您应该能够根据您的需要进行调整。我已经在 Google Colab 中对其进行了测试。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

x = tf.placeholder(dtype=tf.float32, shape=(None, 784), name='x-input')
y = tf.placeholder(dtype=tf.float32, shape=(None, 10), name='y-input')

pred = y
loss_op = tf.losses.mean_squared_error(y, pred)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.name_scope('summaries'):
tf.summary.scalar("loss", loss_op, collections=["train_summary"])
tf.summary.scalar("training_accuracy", accuracy_op, collections=["train_summary"])

with tf.Session() as sess:
summary_op = tf.summary.merge_all(key='train_summary')
train_writer = tf.summary.FileWriter('./graphs', sess.graph)
sess.run([tf.global_variables_initializer(),tf.local_variables_initializer()])

for iter in range(50):
batch_x, batch_y = mnist.train.next_batch(1)
loss, acc, summary = sess.run([loss_op, accuracy_op, summary_op], feed_dict={x:batch_x, y:batch_y})
train_writer.add_summary(summary, iter)

关于python - 如何使用 tensorflow 编写摘要日志以对 MNIST 数据进行逻辑回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55049376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com