Tensorflow MNIST Estimator:批量大小会影响图形预期输入吗?

Tensorflow MNIST Estimator: batch size affects the graph expected input?

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

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

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

input_layer = tf.reshape(features["x"], [-1, 28, 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 'prefix/input_layer:0' 提供形状 (1, 28, 28, 1) 的值,其形状为“(100, 28, 28, 1)”

这让我很担心,因为在生产中我确实需要冻结、优化模型并将其转换为 运行 在 TensorFlow Lite 上的模型。 所以我不会使用 Estimator 界面。

我错过了什么?

tf.reshape 不会丢弃 -1 维度的形状信息。那只是 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:

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