It is necessary to specify piecewise linear interpolation of the hyperbolic tangent. And substitute this function as a layer of a neural network. I tried a lot of things, please suggest your options.) Here is what I tried:
sampledata[sd_] :=
RandomVariate[MultinormalDistribution[{0, 0}, sd*IdentityMatrix[2]],
500];
clusters = Map[sampledata, {3, 1.5, 0.2}];
ListPlot[clusters,
PlotStyle ->
Map[Directive[#, PointSize[0.015]] &, {RGBColor[1, 0.21,
0.35000000000000003`], RGBColor[0.3, 0.78, 0.38],
RGBColor[0.46, 0.5700000000000001, 1]}], Axes -> None,
AspectRatio -> 1]
trainingData =
Join[Thread[clusters[[1]] -> Red], Thread[clusters[[2]] -> Green],
Thread[clusters[[3]] -> Blue]];
RandomSample[trainingData, 8]
ifun = Interpolation[Table[{x, Tanh[x]}, {x, -100, 100, 0.4}],
InterpolationOrder -> 1];
InterpolationToPiecewise[if_, x_] :=
Module[{main, default, grid}, grid = if["Grid"];
Piecewise[{if@"GetPolynomial"[#, x - #], x < First@#} & /@
grid[[2 ;; -2]], if@"GetPolynomial"[#, x - #] &@grid[[-1]]]] /;
if["InterpolationMethod"] == "Hermite";
pwfun[x_] = InterpolationToPiecewise[ifun, x];
net = NetChain[{30, ElementwiseLayer[pwfun], 20,
ElementwiseLayer[pwfun], 3, SoftmaxLayer[]}, "Input" -> {2},
"Output" -> NetDecoder[{"Class", {Red, Green, Blue}}]]
trained = NetTrain[net, trainingData]
But there are errors: Consult Internal`$LastInternalFailure InvalidJSON Non-JSON value:{{KeyAbsent,$Failed},{KeyAbsent,$Failed}} Maybe there is another way to do this?
pwfun? If that is what you seek, then what have you tried so far? – MarcoB May 16 '20 at 17:30pwfun. They might not even follow a link. Include the code for them. – Michael E2 May 16 '20 at 18:30