# Interpolated Hyperbolic tangent as a layer of a neural network

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 =
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?

• What’s the definition of pwfun? If that is what you seek, then what have you tried so far? – MarcoB May 16 '20 at 17:30
• Related: (221899), (221931) – Michael E2 May 16 '20 at 18:02
• That they are what I tried before (221899) , (221931) – Глеб May 16 '20 at 18:07
• I was trying to help people who might try to help you with your problem, by providing relevant (and necessary!) context. You should really edit the question to contain all the information needed to solve the problem. The people who help here do so voluntarily, and in order to have a good chance at getting help, you have to make it easy for them to understand the complete problem. Don't expect anyone to go searching for the definition of pwfun. They might not even follow a link. Include the code for them. – Michael E2 May 16 '20 at 18:30