对K.reshape 和 K.permute_dimensions 进行简单的测试和理解
代码顺序如下:
- 定义一个shape=(1, 3, 4)的变量qw
- qw reshape 为 shape=(-1, 3, 1, 4) 的变量
- 将qw 进行装置
- 将qw 还原到reshape后
from keras import backend as K
qw=K.constant([
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
])
print(qw.shape)
print(K.eval(qw))
print("="*50)
qw=K.reshape(qw, (-1, K.shape(qw)[1], 1, 4))
print(qw.shape)
print(K.eval(qw))
print("="*50)
qw=K.permute_dimensions(qw, (0, 2, 1, 3))
print(qw.shape)
print(K.eval(qw))
print("="*50)
qw=K.permute_dimensions(qw, (0, 2, 1, 3))
print(qw.shape)
print(K.eval(qw))
运行结果:
总结:可以发现,在对矩阵进行置换后,若想得到置换前的矩阵,则需要使用相同的置换规则。相当“负负得正”的理解。