How can I create my new type of python loss layer with parameters on python?

3,853 views
Skip to first unread message

Peerajak Witoonchart

unread,
Aug 26, 2015, 5:09:21 AM8/26/15
to Caffe Users
I am creating new type of loss function with my own weights and parameters for this loss function.


I would like to do

1. Forward a batch of training samples on GPU
2. get the output blobs and then calculate my own loss value, and gradient of the loss on CPU
( I have my own way of loss, and gradient calculation )
3. back propagate the gradient given by 2 to the rest of the network on GPU
4. Update all layer parameters according to gradient in 3.
5. Once a while save the learned network parameters.


Can I do this on python?  I read this https://github.com/BVLC/caffe/issues/1855  but not yet see how my wish can be implemented

many thanx

MahAbb

unread,
Sep 29, 2015, 12:13:58 PM9/29/15
to Caffe Users
I would like to know about defining a new loss function for the loss layer in Deep Net. Any help appreciate!
Thank you

Evan Shelhamer

unread,
Sep 29, 2015, 6:19:16 PM9/29/15
to Peerajak Witoonchart, Caffe Users
A PythonLayer can be a loss layer. PythonLayers are derived from the same Layer interface as the rest of the Caffe layers so `setup()`, `forward()`, `backward()`, and `reshape()` are all exposed for any purpose.

See this example PythonLayer for computing the Euclidean loss and its example prototxt. Note the use of `loss_weight` to signify it is a loss.

Evan Shelhamer



--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/caffe-users/8c905b5b-e2f6-4af4-aed6-bb19b5443a4e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mahdieh Abbasi

unread,
Sep 30, 2015, 10:50:56 AM9/30/15
to Evan Shelhamer, Peerajak Witoonchart, Caffe Users
Hi Evan,

Thank you for your answer. In fact, I just need to update the loss function by adding a new regularization term, not totally change the loss function. Like  https://github.com/BVLC/caffe/issues/1855 , I would like to compute the gradient of the new loss function (which has the regularization term) with respect to the last fully connected layer, then pass the computed gradient to net.backward(gradient) to update the parameters with regard the gradient. But I got error. If I tries to change diff of the last layer with my computed gradient, the backward applies my gradient through the network?

Thank you a lot for answering my questions


Sincerely,
 
Mahdieh Abbasi
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "Caffe Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/caffe-users/ELHfe3KbD0M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to caffe-users...@googlegroups.com.

To post to this group, send email to caffe...@googlegroups.com.

Evan Shelhamer

unread,
Oct 1, 2015, 7:12:59 PM10/1/15
to Mahdieh Abbasi, Peerajak Witoonchart, Caffe Users
If your regularization is summed with the existing loss then define your network with two losses that both have the last fully connected layer as their bottom. Let the first loss be the original desired loss and let the second layer be your regularization implemented as a PythonLayer. Each will compute their bottom diff, these diffs will be summed for the top diff of the fully connected layer, and then backprop will continue as usual.

Evan Shelhamer


Message has been deleted

勇翔王

unread,
Mar 13, 2016, 11:39:10 AM3/13/16
to Caffe Users


HI I am trying to implement this paper with caffe : http://ieeexplore.ieee.org/xpl/articleDetails.jsp?reload=true&arnumber=7298880
Which regularized the weight of two classifier while training multitask learning because they want two classifier capture similar feature of fully connected layer.
I am stuck on that I have no idea how to extract the weight of two layer in this multitask learning architecture.  
Would you please give some hint ?  Thank you 

Amir Abdi

unread,
Sep 22, 2016, 7:33:12 PM9/22/16
to Caffe Users, peer...@gmail.com
Can python layers use GPU as well?

Amal Feriani

unread,
May 31, 2017, 10:30:31 AM5/31/17
to Caffe Users
Hello,

I would like to do the same thing as you.

Did you find an answer to your question ?

Thank you 

Zhaoting Ye

unread,
Nov 24, 2017, 12:24:36 PM11/24/17
to Caffe Users
Hi Amal, 
Do you get any solution to this problem? I am also working on it. 
Thanks
Reply all
Reply to author
Forward
0 new messages