Guest User

Untitled

a guest
Dec 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. class CapsuleLayer(layers.Layer):
  2. """
  3. The capsule layer. It is similar to Dense layer. Dense layer has `in_num` inputs, each is a scalar, the output of the
  4. neuron from the former layer, and it has `out_num` output neurons. CapsuleLayer just expand the output of the neuron
  5. from scalar to vector. So its input shape = [None, input_num_capsule, input_dim_capsule] and output shape = \
  6. [None, num_capsule, dim_capsule]. For Dense Layer, input_dim_capsule = dim_capsule = 1.
  7.  
  8. :param num_capsule: number of capsules in this layer
  9. :param dim_capsule: dimension of the output vectors of the capsules in this layer
  10. :param routings: number of iterations for the routing algorithm
  11. """
  12. def __init__(self, num_capsule, dim_capsule, routings=3,
  13. kernel_initializer='glorot_uniform',
  14. **kwargs):
  15. super(CapsuleLayer, self).__init__(**kwargs)
  16. self.num_capsule = num_capsule
  17. self.dim_capsule = dim_capsule
  18. self.routings = routings
  19. self.kernel_initializer = initializers.get(kernel_initializer)
  20.  
  21. def build(self, input_shape):
  22. assert len(input_shape) >= 3, "The input Tensor should have shape=[None, input_num_capsule, input_dim_capsule]"
  23. self.input_num_capsule = input_shape[1]
  24. self.input_dim_capsule = input_shape[2]
  25.  
  26. # Transform matrix
  27. self.W = self.add_weight(shape=[self.num_capsule, self.input_num_capsule,
  28. self.dim_capsule, self.input_dim_capsule],
  29. initializer=self.kernel_initializer,
  30. name='W')
  31.  
  32. self.built = True
Add Comment
Please, Sign In to add comment