title: How to solve the error TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'
body: <p>I have a class called <code>MaskedDense</code>. Below is its structure.</p>
  <pre><code>class MaskedDense(Layer):
      def __init__(self, units, activation=None, use_bias=True, **kwargs):
          self.units = units
          self.activation = keras.activations.get(activation)
          self.use_bias = use_bias
          self.kernel_initializer = keras.initializers.glorot_uniform()
          self.bias_initializer = keras.initializers.Zeros()
          self.mask_initializer = keras.initializers.Ones()
          super(MaskedDense, self).__init__(**kwargs)
  
      def get_config(self):
          config = super(MaskedDense, self).get_config().copy()
          config.update({
              &quot;units&quot;: self.units,
              &quot;activation&quot;: self.activation,
              &quot;use_bias&quot;: self.use_bias,
              &quot;kernel_initializer&quot;: self.kernel_initializer,
              &quot;bias_initializer&quot;: self.bias_initializer,
              &quot;mask_initializer&quot;: self.mask_initializer
          })
          return config
  
      def build(self, input_shape):
          # Create a trainable weight variable for this layer.
          input_dim = input_shape[-1]
  
          self.kernel = self.add_weight(shape=(input_dim, self.units),
                                        initializer=self.kernel_initializer,
                                        name='kernel')
  
          # The mask is not trainable
          self.mask = self.add_weight(shape=(input_dim, self.units),
                                        initializer=self.mask_initializer,
                                        trainable=False,
                                        name='mask')
  
          if self.use_bias:
              self.bias = self.add_weight(shape=(self.units,),
                                          initializer=self.bias_initializer,
                                          name='bias')
          else:
              self.bias = None
          self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
          self.built = True
          super(MaskedDense, self).build(input_shape)  # Be sure to call this at the end
  
      def call(self, inputs):
          output = K.dot(inputs, self.kernel * self.mask)
          if self.use_bias:
              output = K.bias_add(output, self.bias, data_format='channels_last')
          if self.activation is not None:
              output = self.activation(output)
          return output
  
      def compute_output_shape(self, input_shape):
          assert input_shape and len(input_shape) &gt;= 2
          assert input_shape[-1]
          output_shape = list(input_shape)
          output_shape[-1] = self.units
          return tuple(output_shape)
  
      def set_mask(self, value, feature_idx, class_idx = None):
          &quot;&quot;&quot;
          Set the mask of [feature_idx, class_idx] to a value.
          feature_idx: index of the feature
          class_idx: index of the class (or a list of indices). None means setting the value to all the classes
          value: the value to set
          &quot;&quot;&quot;
          weights = K.get_value(self.mask)
          assert feature_idx &gt;= 0 and feature_idx &lt; weights.shape[0], f&quot;Feature index out of bound [0, ..., {weights.shape[0]-1}] -- {feature_idx} given&quot;
          if class_idx is not None:
              if isinstance(class_idx, list):
                  for idx in class_idx:
                      assert idx &gt;= 0 and idx &lt; weights.shape[1], f&quot;Class index out of bound [0, ..., {weights.shape[1]-1}] -- {idx} given&quot;
                      weights[feature_idx,idx] = value
              elif isinstance(class_idx, int):
                  idx = class_idx
                  assert idx &gt;= 0 and idx &lt; weights.shape[1], f&quot;Class index out of bound [0, ..., {weights.shape[1]-1}] -- {idx} given&quot;
                  weights[feature_idx,idx] = value
          else:
              weights[feature_idx,:] = value
          K.set_value(self.mask, weights)
  
      def disable_mask(self, feature_idx, class_idx = None):
          self.set_mask(value = 0, feature_idx = feature_idx, class_idx = class_idx)
  
      def enable_mask(self, feature_idx, class_idx = None):
          self.set_mask(value = 1, feature_idx = feature_idx, class_idx = class_idx)
  
      def get_masked_weights(self):
          return K.get_value(self.mask) * K.get_value(self.kernel)
  </code></pre>
  <p>Then I am trying to create the model. Below is the code.</p>
  <pre><code>text_input = Input(shape=(None,), dtype=&quot;int32&quot;)
  embedded_text = Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=150, trainable=True)(text_input)
  
  filters = [(10, 2), (10, 3), (10, 4)]
  filter_layers = [Conv1D(f[0], f[1], activation='relu', trainable=True)(embedded_text) for f in filters]
  max_pool_layers = [GlobalMaxPool1D()(result) for result in filter_layers]
  
  concatenated = concatenate(max_pool_layers,axis=-1)
  
  ans = MaskedDense(len(class_names), activation='softmax')(concatenated)
  
  model = Model(text_input, ans)
  model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  model.summary()
  </code></pre>
  <p>I am getting the following error:</p>
  <pre><code> TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'
  </code></pre>
  <p>What am I doing wrong?</p>
  <p>Here is the full error:</p>
  <pre><code> Traceback (most recent call last):   File &quot;find_1.py&quot;, line 227, in
   &lt;module&gt;
      ans = MaskedDense(len(class_names), activation='softmax')(concatenated)   File
   &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py&quot;,
   line 746, in __call__
       self.build(input_shapes)   File &quot;find_1.py&quot;, line 151, in build
       name='kernel')   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py&quot;,
   line 609, in add_weight
       aggregation=aggregation)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py&quot;,
   line 639, in _add_variable_with_custom_getter
       **kwargs_for_getter)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py&quot;,
   line 1977, in make_variable
       aggregation=aggregation)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py&quot;,
   line 183, in __call__
       return cls._variable_v1_call(*args, **kwargs)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py&quot;,
   line 146, in _variable_v1_call
       aggregation=aggregation)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py&quot;,
   line 125, in &lt;lambda&gt;
       previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py&quot;,
   line 2437, in default_variable_creator
       import_scope=import_scope)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py&quot;,
   line 187, in __call__
       return super(VariableMetaclass, cls).__call__(*args, **kwargs)   File
   &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py&quot;,
   line 297, in __init__
       constraint=constraint)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py&quot;,
   line 409, in _init_from_args
       initial_value() if init_from_fn else initial_value,   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py&quot;,
   line 1959, in &lt;lambda&gt;
       shape, dtype=dtype, partition_info=partition_info)   File &quot;/home/user_name/miniforge3/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py&quot;,
   line 473, in __call__
       scale /= max(1., (fan_in + fan_out) / 2.) TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'
  </code></pre>