How to implement a cascaded neural network in Keras where 1st NN's forward output is cloned twice to perform forward on the 2nd NN?

247 views
Skip to first unread message

Parvathy Prasad

unread,
Dec 8, 2020, 5:28:31 AM12/8/20
to Keras-users

Using Keras, I am trying to reproduce a few basic results from a published paper.

In this task, there are two neural networks - A & B, that are connected in a cascade formation, i.e. the output of model A feeds the input of model B. Please see the figure below.

Optimized-IMG_20201207_192551_2.jpg

Here are the specifics steps that need to be implemented:

  1. Perform one forward pass on model A with a batch size of 64, and 13155 rows.
  2. Clone the output to create a batch of 128 rows i.e. use each training sample twice. (There are auxiliary inputs which distinguish the duplicate rows, but I have omitted that info for the sake of brevity).
  3. Perform one forward pass on model B by feeding it with #2 above.
  4. Now backward the two networks, calculate a loss for each one, sum the loss & send it to the optimizer with the gradients of both networks.

As I am quite new to the world of neural networks & Keras, I'd really appreciate any help from the community to implement the above steps in Keras. In particular, I am quite confused about implementing step 2 (please see the attached figure).

The details of customization to my specific problem is fuzzy, for example how to implement the forward & backward logic separately etc.

Krishnakumar Gopalakrishnan

unread,
Dec 11, 2020, 1:09:04 AM12/11/20
to Keras-users
I am also interested in how to implement the tasks in this question using Keras.  In particular, I am intrigued by the part wherein the output of the 1st network is doubled/cloned and fed as input to the second network. Can someone from the community please point in a suitable direction?

Regards,
Krishnakumar

Lance Norskog

unread,
Dec 11, 2020, 9:01:55 PM12/11/20
to Krishnakumar Gopalakrishnan, Keras-users
In the Keras class hierarchy, a Model is a subclass of Layer.

You can create two inner Models, then use them as Layers in a Model that wraps the two and drives the training loop.
Each training batch will push the input sample through all Layers and compare with its output.

You can then save & load these inner Models however you see fit. 

Does this have anything to do with this problem?




--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/2408083a-c79c-424e-8a37-8d0ddcc90b67n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

Lance Norskog

unread,
Dec 11, 2020, 9:21:15 PM12/11/20
to Krishnakumar Gopalakrishnan, Keras-users
To implement a network that compares two inputs, you need to create two channels.
Here is an example of a 'Siamese' network, where both input channels use matching structure and weights.
You do not need to do this, the different channels can have different processing.

The 'feature_model' in this example is used twice in the 'similarity_model' in this example:


Reply all
Reply to author
Forward
0 new messages