Hesham Eraqi
Hesham Eraqi

Reputation: 2542

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)))

Upvotes: 2

Views: 718

Answers (1)

Kh40tiK
Kh40tiK

Reputation: 2336

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.

Upvotes: 1

Related Questions