【发布时间】:2017-03-01 04:27:33
【问题描述】:
我想检查两个张量是否具有相同的形状。
假设我有一些这样的张量:
a = tf.placeholder(tf.float32, shape=[None, 3])
b = tf.placeholder(tf.float32, shape=[None, 3])
我添加了assert a.shape == b.shape。但是,这失败了,可能是由于 None。确实a.shape = (?, 1),而且b.shape 是(?, 1)。它们在我看来是一样的。
如果没有None,它工作正常。
a = tf.placeholder(tf.float32, shape=[1, 3])
b = tf.placeholder(tf.float32, shape=[1, 3])
assert a.shape == b.shape # True
如何在形状检查中忽略 None?
总结:
1: a = tf.placeholder(tf.float32, shape=[1, 3])
2: b = tf.placeholder(tf.float32, shape=[1, 3])
3: assert a.shape == b.shape # True
4:
5: a = tf.placeholder(tf.float32, shape=[None, 3])
6: b = tf.placeholder(tf.float32, shape=[None, 3])
7: assert a.shape == b.shape # False
我想让第 7 行中的断言为 True。
【问题讨论】:
标签: numpy tensorflow