加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Tensorflow MNIST Estimator:批量大小会影响图表的

发布时间:2020-12-16 22:31:08 所属栏目:Python 来源:网络整理
导读:我已经按照TensorFlow MNIST Estimator教程进行了训练,并且训练了我的MNIST模型. 它似乎工作正常,但如果我在Tensorboard上可视化它我看到一些奇怪的东西:模型所需的输入形状是100 x 784. 这是一个屏幕截图:正如您在右侧框中看到的,预期输入大小为100784.

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

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

see? the expected size for input is 100 x 784

现在,我确实在训练中使用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.shape
TensorShape([Dimension(3)])
>>> tf.reshape(a,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)])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读