And*_*ssi 6 python numpy mnist tensorflow tensorflow-estimator
我已经按照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")
Run Code Online (Sandbox Code Playgroud)
我尝试在具有不同批量大小的同一模型上使用estimator.train和estimator.evaluate方法(例如50),并使用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})
Run Code Online (Sandbox Code Playgroud)
我将得到以下错误:
ValueError:无法为Tensor'前缀/ input_layer:0'提供形状值(1,28,28,1),其形状为'(100,28,28,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, [-1, 3]).shape
TensorShape([Dimension(1), Dimension(3)])
>>>
Run Code Online (Sandbox Code Playgroud)
如果您想销毁静态形状信息,请参阅tf.placeholder_with_default:
>>> tf.placeholder_with_default(a[None, :], shape=[None, 3]).shape
TensorShape([Dimension(None), Dimension(3)])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
465 次 |
| 最近记录: |