Tensorflow MNIST估计器:批量大小会影响图形的预期输入吗? [英] Tensorflow MNIST Estimator: batch size affects the graph expected input?

查看:157
本文介绍了Tensorflow MNIST估计器:批量大小会影响图形的预期输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have followed the TensorFlow MNIST Estimator tutorial and I have trained my MNIST model.
It seems to work fine, but if I visualize it on Tensorboard I see something weird: the input shape that the model requires is 100 x 784.

这是屏幕截图:如右图所示,预期的输入大小为100x784.
我以为我会在那里看到x784.

Here is a screenshot: as you can see in the right box, expected input size is 100x784.
I thought I would see ?x784 there.

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

Now, I did use 100 as a batch size in training, but in the Estimator model function I also specified that the amount of input samples size is variable. So I expected ? x 784 to be shown in Tensorboard.

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

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

I tried to use the estimator.train and estimator.evaluate methods on the same model with different batch sizes (e.g. 50), and to use the Estimator.predict method passing a single sample at a time. In these cases, everything seemed to works fine.

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

On the contrary, I do get problems if I try to use the model without passing through the Estimator interface. For example, if I freeze my model and try to load it in a GraphDef and run it in a session, like this:

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:无法为形状为((100,28,28,1)''的Tensor'prefix/input_layer:0'输入形状(1、28、28、1)的值

I will get the following error:
ValueError: Cannot feed value of shape (1, 28, 28, 1) for Tensor 'prefix/input_layer:0', which has shape '(100, 28, 28, 1)'

这让我很担心,因为在生产中,我确实需要冻结,优化和转换我的模型以在TensorFlow Lite上运行它们. 因此,我不会使用Estimator界面.

This worries me a lot, because in production I do need to freeze, optimize and convert my models to run them on TensorFlow Lite. So I won't be using the Estimator interface.

我想念什么?

推荐答案

tf.reshape不会丢弃-1尺寸的形状信息.那只是剩下的东西"的简写:

tf.reshape won't discard shape information for -1 dimensions. That's just a shorthand for "whatever's left over":

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

如果要破坏静态形状信息,请参见 tf.placeholder_with_default :

If you want to destroy static shape information, see tf.placeholder_with_default:

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

这篇关于Tensorflow MNIST估计器:批量大小会影响图形的预期输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆