keras layer that computes logarithms?

Viewed 2988

I'd like to set up a Keras layer in which each node simply computes the logarithm of the corresponding node in the preceding layer. I see from the Keras documentation that there is a "log" function in its backend module. But somehow I'm not understanding how to use this.

Thanks in advance for any hints you can offer!

1 Answers

You can use any backend function inside a Lambda layer:

from keras.layers import Lambda
import keras.backend as K

Define just any function taking the input tensor:

def logFunc(x):
   return K.log(x)

And create a lambda layer with it:

#add to the model the way you're used to:
model.add(Lambda(logFunc,output_shape=(necessaryWithTheano)))

And if the function is already defined, taking only one argument and returning a tensor, you don't need to create your own function, just Lambda(K.log), for instance.

Related