Capsule Net的基本代码和问题代码都在下面。
问题是,当我将Part I
切换为Part II
代码时,会出现无法兼容的尺寸匹配错误。
在我看来,两部分之间的区别是Part II
代码没有为u_vecs
计算维数(input_num_capsule)之一。
Keras无法支持交换两个None
尺寸吗?
如果您想自己尝试,请在Kaggle上拨叉this code。
``` 类Capsule(Layer):
.......
def call(self, u_vecs):
if self.share_weights:
u_hat_vecs = K.conv1d(u_vecs, self.W)
else:
# Part I ###########################
## `local_conv1d`' logic when set kernel_size=1 and stride=1
####################################
# u_vecs: [batch_size, input_num_capsule, input_dim_capsule]
# immediate value : [1, batch_size, input_dim_capsule] # slice_len = 1
# concate immediate value, got X: [input_num_capsule, batch_size, input_dim_capsule]
# W : [input_num_capsule, input_dim_capsule, num_capsule * dim_capsule]
# K.batch_dot(X, W) [input_num_capsule, batch_size, num_capsule * dim_capsule]
# [batch_size, input_num_capsule, num_capsule * dim_capsule]
u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])
# Part II ###################
### In my idea, this is identical to the `local_conv1d(u_vecs, self.W, [1], [1])`
### , but the first dim of `x_aggregate` is determined.
##############################
u_vecs = K.permute_dimensions(u_vecs, (1, 0, 2))
u_hat_vecs = K.batch_dot(u_vecs, self.W)
u_hat_vecs = K.permute_dimensions(u_hat_vecs, (1, 0, 2))
....
```