我正在阅读TensorFlow MNIST official model中的测试。第49行具有:
self.assertEqual(loss.shape, ())
和导致它的所选行是:
BATCH_SIZE = 100
def dummy_input_fn():
image = tf.random_uniform([BATCH_SIZE, 784])
labels = tf.random_uniform([BATCH_SIZE, 1], maxval=9, dtype=tf.int32)
return image, labels
def make_estimator():
return tf.estimator.Estimator(
model_fn=mnist.model_fn, params={
'data_format': 'channels_last'
})
class Tests(tf.test.TestCase):
"""Run tests for MNIST model."""
def test_mnist(self):
classifier = make_estimator()
classifier.train(input_fn=dummy_input_fn, steps=2)
loss = eval_results['loss']
self.assertEqual(loss.shape, ())
但是TensorFlow documentation暗示形状是数字数组:
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.shape(t) # [2, 2, 3]
这两个打印对象形状的语句没有太大帮助:
print(loss.shape)
# prints `()`
print(tf.shape(loss))
# prints `Tensor("Shape:0", shape=(0,), dtype=int32)`
()
形状是什么意思?
答案 0 :(得分:0)
您的loss
是一个NumPy对象,而不是TensorFlow对象:
print(type(loss))
# prints <class 'numpy.float32'>
print(loss)
# prints 2.2745261
我假设NumPy中()
的形状表示标量,尽管我找不到它的文档。您可以通过以下方式查看对象属性(字段和方法)的列表:
print(dir(loss))
# prints `['T', '__abs__', '__add__', '__and__',
# ... 'shape', 'size', 'sort', ... ]`