在tf 2.0 中,您可以轻松检查这些操作是否相同。唯一的区别是您可以使用dim == 1 删除所有轴而不指定它们。所以在最后一行你可以使用tf.squeeze(x_resh) 而不是tf.squeeze(x_resh, [1, 2])。
size = [2, 3]
tf.random.set_seed(42)
x = tf.random.normal(size)
x
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[ 0.3274685, -0.8426258, 0.3194337],
[-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>
x_resh = tf.reshape(x, [2, 1, 1, 3])
x_resh
<tf.Tensor: shape=(2, 1, 1, 3), dtype=float32, numpy=
array([[[[ 0.3274685, -0.8426258, 0.3194337]]],
[[[-1.4075519, -2.3880599, -1.0392479]]]], dtype=float32)>
tf.reshape(x_resh, [2, 3])
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[ 0.3274685, -0.8426258, 0.3194337],
[-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>
tf.squeeze(x_resh, [1, 2])
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[ 0.3274685, -0.8426258, 0.3194337],
[-1.4075519, -2.3880599, -1.0392479]], dtype=float32)>