python–Tensorflow MNIST Estimator:批量大小会影响图表的预期输入吗?

python–Tensorflow MNIST Estimator:批量大小会影响图表的预期输入吗?,第1张

概述我已经按照TensorFlow MNIST Estimator教程进行了训练,并且训练了我的MNIST模型.它似乎工作正常,但如果我在Tensorboard上可视化它我看到一些奇怪的东西:模型所需的输入形状是100 x 784.这是一个屏幕截图:正如您在右侧框中看到的,预期输入大小为100x784.我以为我会看到?x784那里.现在,我确实在训练中使用100

我已经按照TensorFlow MNIST Estimator教程进行了训练,并且训练了我的MNIST模型.
它似乎工作正常,但如果我在Tensorboard上可视化它我看到一些奇怪的东西:模型所需的输入形状是100 x 784.

这是一个屏幕截图:正如您在右侧框中看到的,预期输入大小为100×784.
我以为我会看到?x784那里.

现在,我确实在训练中使用100作为批量大小,但在Estimator模型函数中我还指定了输入样本量的大小是可变的.
所以我期待? x 784将在Tensorboard中显示.

input_layer = tf.reshape(features["x"],[-1,28,1],name="input_layer")

我尝试在不同批量大小(例如50)的同一模型上使用estimator.train和estimator.evaluate方法,并使用Estimator.predict方法一次传递一个样本.
在这些情况下,一切似乎都很好.

相反,如果我尝试使用模型而不通过Estimator接口,我确实会遇到问题.
例如,如果我冻结我的模型并尝试在GraphDef中加载它并在会话中运行它,如下所示:

with tf.gfile.Gfile("/path/to/my/froZen/model.pb","rb") as f:    graph_def = tf.GraphDef()    graph_def.ParseFromString(f.read())with tf.Graph().as_default() as graph:    tf.import_graph_def(graph_def,name="prefix")    x = graph.get_tensor_by_name('prefix/input_layer:0')    y = graph.get_tensor_by_name('prefix/softmax_tensor:0')    with tf.Session(graph=graph) as sess:        y_out = sess.run(y,Feed_dict={x: 28_x_28_image})

我会收到以下错误:
ValueError:无法为Tensor’前缀/ input_layer:0’提供形状值(1,1),其形状为'(100,1)’

这让我很担心,因为在生产中我需要冻结,优化和转换我的模型以在TensorFlow lite上运行它们.
所以我不会使用Estimator接口.

我错过了什么?

最佳答案tf.reshape不会丢弃-1维的形状信息.这只是“剩下的东西”的简写:

>>> import tensorflow as tf>>> a = tf.constant([1.,2.,3.])>>> a.shapeTensorShape([Dimension(3)])>>> tf.reshape(a,3]).shapeTensorShape([Dimension(1),Dimension(3)])>>> 

如果要销毁静态形状信息,请参阅tf.placeholder_with_default

>>> tf.placeholder_with_default(a[None,:],shape=[None,3]).shapeTensorShape([Dimension(None),Dimension(3)])
总结

以上是内存溢出为你收集整理的python – Tensorflow MNIST Estimator:批量大小会影响图表的预期输入吗?全部内容,希望文章能够帮你解决python – Tensorflow MNIST Estimator:批量大小会影响图表的预期输入吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1206074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存