首页 文章

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

提问于
浏览
5

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

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

see? the expected size for input is 100 x 784

现在,我确实在训练中使用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: 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接口 .

我错过了什么?

1 回答

  • 3

    tf.reshape 不会丢弃 -1 尺寸的形状信息 . 这只是"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)])
    

相关问题