tf.keras.backend.batch_flatten(x)
将 nD 张量,转换成 2D 张量,并且第 0 维与x第0维相同
当x为2维时,没有变化
c=np.random.randint(0,10,(2,3))
print(c)
result=tf.keras.backend.batch_flatten(c)
print(result)
执行结果
array([[2, 3, 5],
[8, 3, 2]])
tf.Tensor(
[[2 3 5]
[8 3 2]], shape=(2, 3), dtype=int32)
当x为3维以上时,转成2维
d=np.random.randint(0,10,(2,3,4))
print(d)
result=tf.keras.backend.batch_flatten(d)
print(result)
执行结果
array([[[6, 8, 9, 2],
[2, 0, 2, 5],
[3, 4, 9, 0]],
[[8, 1, 4, 9],
[2, 7, 3, 0],
[5, 5, 7, 4]]])
tf.Tensor(
[[6 8 9 2 2 0 2 5 3 4 9 0]
[8 1 4 9 2 7 3 0 5 5 7 4]], shape=(2, 12), dtype=int32)