Is there any loss function in Caffe optimizing dot product of 2 vectors?

427 views
Skip to first unread message

Wong Fungtion

unread,
Feb 23, 2017, 8:52:39 AM2/23/17
to Caffe Users
I have a vector A output from InnerProduct layer, I would like to compare it with groundTruth B(same shape with A), and make the dot product of A and B the bigger the better. I am wondering if there is any loss function in Caffe could make it.

Thanks

Przemek D

unread,
Mar 2, 2017, 2:06:12 AM3/2/17
to Caffe Users
I don't think there is a function that does this exactly. However, you can make any layer become a loss function simply by adding a weight_loss: 1 param to its definition (1 is default for normal loss layers), so you can design your own loss with the available layers - just add this param to the final layer you use. I don't know what exactly are you trying to do and what the numbers will look like, so I suppose you need to come up with some loss equation on paper first and see how it works for you. I'd experiment with Power layer and build something around L(x) = 1/x, but that requires some approach to negative numbers etc.

Alex Orloff

unread,
Mar 2, 2017, 2:25:17 AM3/2/17
to Caffe Users
its called SoftMaxWithLoss

Przemek D

unread,
Mar 2, 2017, 4:07:16 AM3/2/17
to Caffe Users
its called SoftMaxWithLoss
 I think this is wrong but could you elaborate on how would that work?

Alex Orloff

unread,
Mar 2, 2017, 8:41:07 AM3/2/17
to Caffe Users
you may think anything you want.
SoftMaxWithLoss is loss function to maximaze correct output, which is dot function of two vectors.

Przemek D

unread,
Mar 3, 2017, 3:19:56 AM3/3/17
to Caffe Users
No need to be unpleasant, I just don't see how one could use Softmax on a single number. We know little on what Wong actually wants to do, so  I took his words literally and assumed he wants to calculate AoB (probably with another InnerProduct layer), which is some scalar [a], and maximize this value.
Because if you put this through Softmax, you will always get 1 - recall the formula:
Softmax(x) = exp(x_i) / sum(exp(x_i))
And I don't know how would it work and what exactly would be the result if you fed A and B directly to softmax, so I asked you to elaborate.

Wong Fungtion

unread,
Mar 3, 2017, 3:58:08 AM3/3/17
to Caffe Users
I don't think it will work, SoftMaxWithLoss require A.shape=(n, c, w, h), and B.shape =(n, 1, 1, 1). In my application, A.shape=(1,n) and B.shape=(1,n).

在 2017年3月2日星期四 UTC+8下午9:41:07,Alex Orloff写道:

Wong Fungtion

unread,
Mar 3, 2017, 4:03:03 AM3/3/17
to Caffe Users
Specifically, given A = [a1, a2, ..., an] and B = [b1, b2, ... bn], B is the groudtruth, what I want to optimize is max\sum_{i=1}^n (a_i*b_i)

在 2017年3月3日星期五 UTC+8下午4:19:56,Przemek D写道:

Przemek D

unread,
Mar 3, 2017, 4:25:41 AM3/3/17
to Caffe Users
In this case I would make another InnerProduct layer, with A and B as bottoms and num_output: 1 (its output will be that dot product you want). Then you would need to introduce some notion of loss that would pull all possible outputs towards +infinity. The simplest way would be to propagate backwards a constant. Since you don't want the dot product to reach a specific value, but just be "as big as it can", pulling all outputs up by the same amount no matter the network output, seems like a good idea. Things would become complicated if you wanted to pull smaller outputs more than the larger ones, but before we go deeper in this... could you provide some background? What data are you working with, what exactly is A and B? What is the point of this kind of optimization?
Reply all
Reply to author
Forward
0 new messages