【发布时间】:2019-10-22 04:33:33
【问题描述】:
我正在尝试检查 mini-batch 输出是否等于将 mini-batch 的所有元素一一给出以评估 Mobilenet 的特征向量。
看下面的代码:
model = tf.keras.models.Sequential(
(
hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4",
output_shape=[1280],
trainable=False
),
)
)
images = tf.random.uniform(shape=(20, 224, 224, 3))
features = model.predict(images)
for i in range(20):
image = tf.reshape(images[i, ...], (1, 224, 224, 3))
image_feature = model.predict(image)
self.assertTrue((image_feature == features[i, ...]).all())
assertTrue 在我的测试中失败。是否应该为所有图像提供相同的特征向量,无论它们是作为小批量还是一张一张地提供?
【问题讨论】:
-
结果应该相同,但不一定到最后一位。 (这是针对动态速度优化的数值计算。)您看到了多大的差异?要进行调查,请尝试
np.max(np.abs(... - ...))之类的代码。要编写实际的单元测试,请查看np.allclose()或np.testing.assert_allclose()及其参数atol和rtol。 -
是的,我注意到它们并没有太大的不同。事实上,它们不大于 10e-4。所以这就是感谢的原因!
标签: python tensorflow2.0 tensorflow-hub