Hesham Eraqi Hesham Eraqi - 1 year ago 140
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'))

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,
    must be a list, a tuple, or a function".

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download