I am trying to create a custom function in a Neural Network. The output of the NN is a n x 2 matrix which represent coordinates of points in a plane. From this, the Euclidean pairwise distance matrix needs to be computed which is then compared with the target pairwise distance matrix. Here is what I have so far:
coords1 = N@ Round[RandomReal[{-10., 10}, {5, 2}], 10^-4];
distM = Table[Table[EuclideanDistance[pi, pj], {pj, coords1}], {pi, coords1}] // MatrixForm; (* Distance Matrix *)
(* Pure function that computes the distance matrix *)
func = (Partition[#, Sqrt@Length@#] &)@*
Map[EuclideanDistance[#[[1]], #[[2]]] &]@*
(Partition[Part[#, Join @@ Tuples[{Range@Length@#, Range@Length@#}]], 2] &
(* The last Linear Layer 5 x 5 should be replaced by a layer that uses the above custom function to compute the distance matrix *)
net = NetChain[{LinearLayer[{5, 2}], ElementwiseLayer[Ramp], LinearLayer[{5, 5}]}, "Input" -> {5, 2}, "Output" -> {5, 5} ]
Any ideas? I have reviewed the documentation and I could not find anything that I could use. I appreciate any help in this matter.
Thanks