Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CapsuleLayer(layers.Layer):
- """
- The capsule layer. It is similar to Dense layer. Dense layer has `in_num` inputs, each is a scalar, the output of the
- neuron from the former layer, and it has `out_num` output neurons. CapsuleLayer just expand the output of the neuron
- from scalar to vector. So its input shape = [None, input_num_capsule, input_dim_capsule] and output shape = \
- [None, num_capsule, dim_capsule]. For Dense Layer, input_dim_capsule = dim_capsule = 1.
- :param num_capsule: number of capsules in this layer
- :param dim_capsule: dimension of the output vectors of the capsules in this layer
- :param routings: number of iterations for the routing algorithm
- """
- def __init__(self, num_capsule, dim_capsule, routings=3,
- kernel_initializer='glorot_uniform',
- **kwargs):
- super(CapsuleLayer, self).__init__(**kwargs)
- self.num_capsule = num_capsule
- self.dim_capsule = dim_capsule
- self.routings = routings
- self.kernel_initializer = initializers.get(kernel_initializer)
- def build(self, input_shape):
- assert len(input_shape) >= 3, "The input Tensor should have shape=[None, input_num_capsule, input_dim_capsule]"
- self.input_num_capsule = input_shape[1]
- self.input_dim_capsule = input_shape[2]
- # Transform matrix
- self.W = self.add_weight(shape=[self.num_capsule, self.input_num_capsule,
- self.dim_capsule, self.input_dim_capsule],
- initializer=self.kernel_initializer,
- name='W')
- self.built = True
Add Comment
Please, Sign In to add comment