Keras: Lambda output layer

Created on 25 Oct 2016  路  8Comments  路  Source: keras-team/keras

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)))
1- Is it correct? Does x here means the net as I expect ?
2- I get an error: "In Lambda, output_shape must be a list, a tuple, or a function".

stale

Most helpful comment

(Notice the comma after 1)

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

You can also igonre the output_shape argument in this case.

model.add(Lambda(lambda x: x%100))

For bounding by 100, I think you should use K.clip.

All 8 comments

(Notice the comma after 1)

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

You can also igonre the output_shape argument in this case.

model.add(Lambda(lambda x: x%100))

For bounding by 100, I think you should use K.clip.

@farizrahman4u Thank you so much. But doesn't this mean that I will have 100 output neurons not a single one ?

No. Since your last layer, Dense(1) already reduces the dimensionality to 1, and it is conserved through the Lambda layer unless specified otherwise.

@kgrm I say "I modified the last line ...", so it's not there any more. So, I think it will produce 100 output neurons, and the workaround is to add a dense(1) layer before.
Can I do it with single layer directly ?

You can, if you use a function that takes 100 inputs and produces a single output.

@kgrm can you please refer me to an article or something that tells how to code this please? Thank you.

For an example of a function that takes an arbitrary-shape tensor and outputs a scalar, see K.sum().

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.

Was this page helpful?
0 / 5 - 0 ratings