Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model a shared layer in keras?

I want to train a model with a shared layer in the following form:

x --> F(x)
          ==> G(F(x),F(y))
y --> F(y) 

x and y are two separate input layers and F is a shared layer. G is the last layer after concatenating F(x) and F(y).

Is it possible to model this in Keras? How?

like image 738
Amirhessam Avatar asked Aug 24 '18 22:08

Amirhessam


People also ask

What is shared layer Keras?

Shared layers are an advanced deep learning concept, and are only possible with the Keras functional API. They allow you to define an operation and then apply the exact same operation (with the exact same weights) on different inputs. In this model, we will share team rating for both inputs.

What is shared layer?

Unlike simply duplicating a layer, sharing a layer lets you make changes to multiple copies by changing only a single linked layer. If you want to duplicate finished layers as shared layers in a new channel, you can select Share Layers As Channel from the context menu or Layers menu.

What is the meaning of model Sequentil () in Keras?

The Sequential model API is a way of creating deep learning models where an instance of the Sequential class is created and model layers are created and added to it.

What is Input_shape in Keras?

The input shape In Keras, the input layer itself is not a layer, but a tensor. It's the starting tensor you send to the first hidden layer. This tensor must have the same shape as your training data. Example: if you have 30 images of 50x50 pixels in RGB (3 channels), the shape of your input data is (30,50,50,3) .


1 Answers

You can use Keras functional API for this purpose:

from keras.layers import Input, concatenate

x = Input(shape=...)
y = Input(shape=...)

shared_layer = MySharedLayer(...)
out_x = shared_layer(x)
out_y = shared_layer(y)

concat = concatenate([out_x, out_y])

# pass concat to other layers ...

Note that x and y could be the output tensors of any layer and not necessarily input layers.

like image 188
today Avatar answered Sep 27 '22 01:09

today