# How to implement the same function use *Layer function?

Reading YOLO method to detect object in the image(paper),I want to train this tiny-VOLO v1 on Mathematica,but the special loss function is very difficult to write.

Total loss is: For brief,I use 1-dim vec to make a example.

This is ordinary MSE function,but it does't have Mask like the loss in the image.

MeanSquaredLossLayer[][<|"Input" -> {1, 2, 3, 4, 5, 6},
"Target" -> {3, 2, 1, 4, 6, 2}|>](*4.16667*)


So I define a function:

meanSquareLoss = Mean[#Mask*(#Input - #Target)^2] &;
N@meanSquareLoss[<|"Mask" -> {1, 1, 1, 1, 1, 1},
"Input" -> {1, 2, 3, 4, 5, 6},
"Target" -> {3, 2, 1, 4, 6, 2}|>](*4.16667*)

N@meanSquareLoss[<|"Mask" -> {1, 0, 0, 1, 0, 1},
"Input" -> {1, 2, 3, 4, 5, 6},
"Target" -> {3, 2, 1, 4, 6, 2}|>](*3.33333*)

net = NetGraph[{ThreadingLayer[(#1 - #2)^2 &],
ThreadingLayer[Times]}, {{NetPort["Input"], NetPort["Target"]} -> 1 -> 2,
Mean@net[<|"Input" -> {1, 2, 3, 4, 5, 6},
"Target" -> {3, 2, 1, 4, 6, 2},
"Mask" -> {1, 0, 0, 1, 0, 1}|>](*3.33333*)
(*Because mma don't have MeanLayer[]*)


This way to define a loss net it throws error:

NetGraph[{ThreadingLayer[(#1 - #2)^2 &],
NetPort["Target"]} -> 1 -> 2, NetPort["Mask"] -> 2 -> 3}]


NetGraph::tyinc: Value for "Input" port of layer 3 (a list of tensors) is inconsistent with value for "Output" port of layer 2 (a tensor).

But Using the TotalLayer[] is just want to calculate the sum of all the elements...

And I want to use ConstantArrayLayer.

MeanSquaredLossMaskLayer = NetGraph[{ThreadingLayer[(#1 - #2)^2 &],ThreadingLayer[Times],
ConstantArrayLayer[], MeanSquaredLossLayer[]},
{{NetPort["Input"], NetPort["Target"]} -> 1 -> 2 -> NetPort[4, "Input"],
3 -> NetPort[4, "Target"], NetPort["Mask"] -> 2}]


But it can not use NetInitialize@MeanSquaredLossMaskLayer...

So how to define a custom loss function like this using *Layer funtion? if use 2-dims or higer dimension? How to add Mask into it?

And I find it has no *Layer function can do Map[Max, #, {2}]& that not only reduce the dimension of input but also use the Max to spicific dimension.

I try the NetMapOperator[ElementwiseLayer[Max], "Input" -> {20, 7, 7}],but the dimension of output is also {20, 7, 7}.

By the way PartLayer is not support {All,1}...

So how to write the custom loss function more effectly? • NetChain[{AggregationLayer[Max]}, "Input" -> {20, 7, 7}] – Alexey Golyshev Jun 13 '17 at 10:25
• meanLayer = {ReplicateLayer, AggregationLayer[Mean], PartLayer} – Alexey Golyshev Jun 13 '17 at 10:25
• Replace TotalLayer with SummationLayer. – Alexey Golyshev Jun 13 '17 at 10:48