Why this custom layer keeps throwing errors ffs?

class MyDenseLayer(tf.keras.layers.Layer):

  def __init__(self):

    super(MyDenseLayer, self).__init__()

  def build(self, input_shape):

    self.omegam = tfd.Normal(loc=tf.Variable(0.3, name='omegam'), scale=0.01)

    self.omegaw0 = tfd.Normal(loc=tf.Variable(-1., name='omegaw0'), scale=0.01)

    self.omegaw1 = tfd.Normal(loc=tf.Variable(0., name='omegaw1'), scale=0.01)

  def call(self, inputs):

    inputs=inputs

    uffa= self.omegam.sample()

    uffa2= self.omegaw1.sample()

    uffa3=self.omegaw0.sample()

    uff= 1/tf.math.sqrt(uffa*(1+inputs)**3+ (1-uffa)*tf.math.exp(uffa2*inputs+(1+uffa3 -uffa2)*tf.math.log(1+inputs)))  

 

    return  uff

i just want to apply a function to the input variable but it never works ffs why doesn’t it work?

when i try

mydense = MyDenseLayer()

mydense(5)

i get

     14     uffa2= self.omegaw1.sample()
     15     uffa3=self.omegaw0.sample()
---> 16     uff= 1/tf.math.sqrt(uffa*(1+inputs)**3+ (1-uffa)*tf.math.exp(uffa2*inputs+(1+uffa3 -uffa2)*tf.math.log(1+inputs)))
     17 
     18 

InvalidArgumentError: Exception encountered when calling layer "my_dense_layer_34" (type MyDenseLayer).

cannot compute Mul as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Mul]

Call arguments received by layer "my_dense_layer_34" (type MyDenseLayer):
  • inputs=tf.Tensor(shape=(), dtype=int32)

Hi @P11, As mentioned in the error by passing the float value instead of int the code works fine. You can refer to this gist for working code example.Thank You.

1 Like

hi, will this inability to take integers as input affects the neural network if i place the layer ahead of a mlp?

Hi @P11, I don’t think there will be any problem. Thank You.

1 Like