Hesham Eraqi Hesham Eraqi - 1 month ago 10
Python Question

Lambda output layer

I've a sequential model as follows with a linear activation function (Keras default) for the single output neuron:

model = Sequential()
model.add( ...
...
model.add(Dense(100, activation='relu'))
model.add(Dense(1))


I need the final number to be bounded by 100, so I modified the last line of code above to be:

model.add(Lambda(lambda x: x%100, output_shape=(1)))



  • Is it correct? Does x here means the net as I expect ?

  • I get an error: "In Lambda,
    output_shape
    must be a list, a tuple, or a function".


Answer

output_shape=(1) should be output_shape=(1,).

BTW, I consider following alternatives being better:

  • Clip output to [0.0, 100.0].

    #...
    model.add(Dense(1)) #-2nd line from code in question
    model.add(Lambda(lambda x: max(0., min(x,100.)), output_shape=(1,)))
    

This is a continuous function as opposed to mod 100.

  • Use scaled sigmoid output layer.

    #...
    model.add(Dense(1), activation='sigmoid')
    model.add(Lambda(lambda x:x*100., output_shape=(1,)))
    

This is differentiable, being friendly to SGD.