How to add a custom layer at the end of a trained Model?

22 views
Skip to first unread message

Phuong LE-HONG

unread,
Jan 26, 2023, 10:17:51 AM1/26/23
to BigDL User Group
Hi all,

If I have a trained sequential model, say bigdl (of type KerasNet), then I can cast it to Sequential and call the add() method to add a custom layer as follows:

val sequential = bigdl.asInstanceOf[Sequential[Float]]
sequential.add(MyLayer())

However, if my bigdl model is more complicated, which is a graph of type Model[Float]:

val model = bigdl.asInstanceOf[Model[Float]]

Then how can I add that custom layer to the end of the model? My bigdl has only a single output. MyLayer() is used at prediction stage only, which transforms the 3-d output of the model to 2-d output, similar to ArgMax (so as to be usable with a NNModel).

I looked into the code of KerasNet (more specifically, keras/Model.scala, but I did not find any way to do this. 

Thank you very much for your help.

Best,

Phuong

Jennie Wang

unread,
Jan 26, 2023, 8:06:59 PM1/26/23
to User Group for BigDL
Hi Phuong,

You may try this way to see if it is working:
val model = bigdl.asInstanceOf[Model[Float]]
val out1 = model.getOutputs()(0)
val out2 = MyLayer().inputs(out1)
val model2 = Model(model.getInputs().toArray, out2)

Thanks,
Jennie

Phuong LE-HONG

unread,
Jan 26, 2023, 10:54:07 PM1/26/23
to Jennie Wang, User Group for BigDL
Hi Jennie,

I've tried your method before but it is not working. The reason is getInputs() and getOutputs are not accessible. It raises error:

method getInputs in class Model cannot be accessed in com.intel.analytics.bigdl.dllib.keras.Model[Float]

Do you have any other way to solve this problem?

Thank you very much.

Phuong

 

--
You received this message because you are subscribed to the Google Groups "User Group for BigDL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bigdl-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bigdl-user-group/364950cf-10a0-43ed-a69e-e7cec3fc6ad7n%40googlegroups.com.

Phuong LE-HONG

unread,
Jan 27, 2023, 10:16:37 AM1/27/23
to Jennie Wang, User Group for BigDL
Hi Jennie and all,

I've found a way. Instead of trying to call bigdl-private methods getInputs() and getOutputs(), we can set names for the modules, and then use the public nodes(someNames) or node(someName) to get the inputs/output modules. 

Btw, I do not understand why getInputs() and getOutputs() methods are made private. Is there any security reason here?

Cheers,

Phuong

Jennie Wang

unread,
Jan 27, 2023, 3:37:50 PM1/27/23
to User Group for BigDL
Hi Phuong,

Yes, you can use  nodes(someNames) or node(someName) to do that. 
Our team would discuss whether we need to make getInputs() and getOutputs() public.

Thanks,
Jennie

Reply all
Reply to author
Forward
0 new messages