- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在训练一些模型,我注意到当我显式定义图形变量时,我的保护程序对象的创建位置很重要。首先我的代码如下所示:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("tmp_MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.truncated_normal([784, 10], mean=0.0, stddev=0.1),name='w')
b = tf.Variable(tf.constant(0.1, shape=[10]),name='b')
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) # list of booleans indicating correct predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1001):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(fetches=train_step, feed_dict={x: batch_xs, y_: batch_ys})
if i % 100 == 0:
saver.save(sess=sess,save_path='./tmp/mdl_ckpt')
print(sess.run(fetches=accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
然后我决定将其更改为类似的内容,在我定义变量和定义保护程序的位置似乎非常敏感。例如,如果在创建图形变量之后没有准确定义它们,则会出现错误。同样,我注意到必须在一个变量之后准确定义保护程序(注意在图形定义之后不够),以便保护程序一起捕获所有变量(这并没有'对我来说这没有意义,要求它在所有变量的定义后面而不是单个变量的定义后面才有意义)。
这就是代码现在的样子(注释显示了我定义保护程序的位置):
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("tmp_MNIST_data/", one_hot=True)
graph = tf.Graph()
with tf.Session(graph=graph) as sess:
#saver = tf.train.Saver()
x = tf.placeholder(tf.float32, [None, 784])
saver = tf.train.Saver()
y_ = tf.placeholder(tf.float32, [None, 10])
#saver = tf.train.Saver()
W = tf.Variable(tf.truncated_normal([784, 10], mean=0.0, stddev=0.1),name='w')
#saver = tf.train.Saver()
b = tf.Variable(tf.constant(0.1, shape=[10]),name='b')
y = tf.nn.softmax(tf.matmul(x, W) + b)
#saver = tf.train.Saver()
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) # list of booleans indicating correct predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#saver = tf.train.Saver()
step = tf.Variable(0, name='step')
#saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
#saver = tf.train.Saver()
for i in range(1001):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(fetches=train_step, feed_dict={x: batch_xs, y_: batch_ys})
if i % 100 == 0:
step_assign = step.assign(i)
sess.run(step_assign)
saver.save(sess=sess,save_path='./tmp/mdl_ckpt')
print(step.eval())
print( [ op.name for op in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)] )
print(sess.run(fetches=accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
上面的代码应该可以工作,但是我很难理解为什么它会这样,或者为什么会发生这种情况。有人知道正确的做法是什么吗?
最佳答案
我不完全确定这里发生了什么,但我怀疑问题与变量没有进入错误的图表有关,或者 session 具有过时的图表版本。您创建一个图表,但不将其设置为默认值,然后使用该图表创建一个 session ...但是当您创建变量时,您没有指定它们应该进入哪个图表。也许 session 的创建将指定的图形设置为默认值,但这不是 tensorflow 的设计使用方式,因此如果它没有在这种情况下经过彻底测试,我不会感到惊讶。
虽然我没有解释或发生了什么,但我可以建议一个简单的解决方案:将图形构建与 session 运行分开。
graph = tf.Graph()
with graph.as_default():
build_graph()
saver = tf.train.Saver()
with tf.Session(graph=graph) as sess:
do_stuff_with(sess)
saver.save(sess, path)
关于machine-learning - 当 TensorFlow 中有图形对象时,为什么脚本中 saver 的位置很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41865482/
重要说明:我只是在笔记本环境中运行这一部分,图形定义。我还没有运行实际的 session 。 运行这段代码时: with graph.as_default(): #took out " , tf.de
将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情。tf里面提供模型保存的是tf.train.Saver()模块。 模型保存,先要创建一个Saver对象:如
我尝试运行一个简单的程序来将 Tensorflow session 保存到磁盘上作为“spikes.cpkt”。虽然在交互式程序中,系统输出显示我已成功创建该文件,但我在文件系统中找不到该文件。 我使
我有一个带有Autoencoder的docker容器,可以通过Flask-Server启动它。所有脚本都被复制到Docker的/ root中,并且还可以访问共享卷/ data,如下所示: /数据 -/
我需要将我的 iPad 应用程序完全按照它在设备上运行的方式镜像到外部屏幕上,但在一段时间不事件后,外部屏幕应显示静态图像,直到设备再次接收触摸输入。我今天阅读了很多关于在 iOS 中处理多个屏幕的内
我正在尝试在我的 Angular 2 应用程序中实现下载按钮。 我用过这些教程: http://alferov.github.io/angular-file-saver/ https://coryry
根据有关此 API 的文档和大量 SO 帖子,必须使用 创建保护程序对象 saver = tf.train.Saver(...variables...) 我想知道是否有任何方法可以自动填充(...va
Android 7.0 Nougat 添加了 Data Saver 功能,允许用户限制某些应用程序(包括推送通知)的后台数据。当 Data Saver 开启时,只有在 中找到的列表中的应用程序 Set
我正在为 Android 开发一个音乐流媒体应用。 在这个应用程序中,我有一个 Service 负责播放来自 HTTP 服务器的音频。在播放之前,我确保 startForeground() 并获取部分
var MyFiles = []; if (val == "Address") { MyFiles.push({ 'file': 'http://-
我发现如果要保存某个图的所有变量,必须在图的最后定义tf.train.Saver,否则saver无法获取所有变量变量。 这是我的测试代码: def how_saver_work(): g =
是否可以通过 drop-in saver 将 localhost 文件上传到 dropbox?( https://www.dropbox.com/developers/dropins/saver )
我有一个训练好的模型,使用tf.train.Saver保存,生成4个相关文件 检查点 model_iter-315000.data-00000-of-00001 model_iter-315000.i
我刚刚将本地安装的 Tensorflow 更新为 0.11rc2,我收到一条消息说我应该向我的保护程序添加一个参数以使其保存在版本 2 中。我更新了它,现在我无法加载保存在版本 2 中的模型这种格式。
关于模型保存的一点心得 ? 1
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我创建了一些按钮,并且单击按钮 url 和文件名发送到动态数组,当我单击 dropbox saver 按钮时,我希望 saver 函数使用该动态数组将文件发送到 dropbox。我当前的代码出现此错误
我想知道在每次训练后使用 tf.train.Saver() 保存我的模型时到底保存了什么。与我习惯使用 Keras 模型的文件相比,该文件似乎有点大。现在我的 RNN 在每次保存时占用 900 MB。
我们可以保存一个检查点 saver = tf.train.Saver() saver.save(sess, FLAGS.train_dir, global_step=step) 然后,稍后,我可以恢复
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我是一名优秀的程序员,十分优秀!