Message Boards Message Boards

GROUPS:

Define a NN loss function that considers results on two inputs?

Posted 3 months ago
538 Views
|
2 Replies
|
1 Total Likes
|

Is it possible to define a loss function that considers the results of applying the net to two different inputs? To be clearer, suppose I have a NN net, and a function f of two tensors. I want to use training data of the kind

{in1,in2}->value

where the loss function would calculate something like

(f[net[in1],net[in2]] - value)^2.

Is that possible?

2 Replies

Dabrowski, I do not know if this is what you will expect, but for your information.

(* training data *)
in1 = Table[RandomReal[], {100}, {2}];
in2 = Table[RandomReal[], {100}, {3}];
val = Table[RandomInteger[{1, 10}], {100}];
traingdata = (Association[{"In1" -> #[[1]], "In2" -> #[[2]], 
       "Val" -> #[[3]]}] & /@ Transpose[{in1, in2, val}]);
(* define network *)
net = NetGraph[{LinearLayer[5, "Input" -> 2], 
    LinearLayer[5, "Input" -> 3]}, {NetPort["In1"] -> 
     1 -> NetPort["Out1"], NetPort["In2"] -> 2 -> NetPort["Out2"]}];
loss = NetGraph[{ThreadingLayer[(#1 + #2) &], SummationLayer[], 
    MeanSquaredLossLayer[]},
   {{NetPort["Out1"], NetPort["Out2"]} -> 
     1 -> 2, {2, NetPort["Val"]} -> 3}];
lossNet = 
  NetGraph[{"net" -> net, 
    "loss" -> loss}, {NetPort["net", "Out1"] -> 
     NetPort["loss", "Out1"], 
    NetPort["net", "Out2"] -> NetPort["loss", "Out2"]}];
(* training *)
results = NetTrain[lossNet, traingdata]

enter image description here

Thanks for your comment. That is not quite what I'm looking for though.

I want net to perform exactly the same calculation with exactly the same weights on both inputs and then compare the outputs from both.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract