Skip to Content
Deep Learning with Keras
book

Deep Learning with Keras

by Antonio Gulli, Sujit Pal
April 2017
Intermediate to advanced
318 pages
7h 40m
English
Packt Publishing
Content preview from Deep Learning with Keras

Keras example — using the lambda layer

Keras provides a lambda layer; it can wrap a function of your choosing. For example, if you wanted to build a layer that squares its input tensor element-wise, you can say simply:

model.add(lambda(lambda x: x ** 2))

You can also wrap functions within a lambda layer. For example, if you want to build a custom layer that computes the element-wise euclidean distance between two input tensors, you would define the function to compute the value itself, as well as one that returns the output shape from this function, like so:

def euclidean_distance(vecs):    x, y = vecs    return K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))def euclidean_distance_output_shape(shapes):    shape1, shape2 = shapes return (shape1[0], ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Deep Learning with Keras

Advanced Deep Learning with Keras

Rowel Atienza, Neeraj Verma, Valerio Maggio
Deep Learning with TensorFlow 2 and Keras - Second Edition

Deep Learning with TensorFlow 2 and Keras - Second Edition

Antonio Gulli, Dr. Amita Kapoor, Sujit Pal

Publisher Resources

ISBN: 9781787128422Supplemental Content