I’m a beginner in Keras. I meet a simple problem, but I have searched for a long time and cannot find how to deal with it.
In briefly, I have 2 vectors: [a1, a2, a3]
& [b1, b2, b3]
, and I want to combine them by giving their weights, using the following formula to get the new vector: [y1, y2, y3]
. Then how to implement this with Keras?
My situation is that: I build 2 models separately, and each one has its prediction output (i.e. 3 values representing the predicted value of 3 categories). The output of the first model is [a1, a2, a3]
, and for the second model is [b1, b2, b3]
.
Now I want to merge these 2 outputs to get new prediction results [y1, y2, y3]
, so y1
is the combination of a1
& b1
, and the model should learn the weight by itself. It means that y1 = w1*a1 + w4*b1
, and w1
and w4
are the weights to be trained. Similarly, y2 = w2*a2 + w5*b2
, y3 = w3*a3 + w6*b3
, so I have 6 weights to be trained. It is like element-wise multiplication, but the numbers being multiplication are the weights to be trained. I have tried the following codes, but I find that it’s not what I want.
# output_a & output_b are size (3,1)
merge = concatenate([output_a, output_b], axis=2)
# merge is (3,2)
output_y = Dense(1, use_bias=False)(merge)
# output_y is (3,1)
I find that in the Dense layer, the number of training parameter is 2. I think it means that it just has 2 weights (i.e. w1=w2=w3
, and w4=w5=w6
). I’m not sure how to fix it. Thank you all for helping me!
Edit: Someone told me that maybe I should define the layer I want by myself. So isn't there any function I can use to achieve that?
Edit: I add a figure below and I think it can describe my question.