ActivationFunctions { public static final float tanh(final float x) { final float e2x = (float)Math.exp(2*x); return (e2x-1) / (e2x+1); } private  ActivationFunctions(); static final float calc(final ActivationType type, final float x); static final float prime(ActivationType type, float y); static final float sigmoid(final float x); static final float sigmoidPrime(final float y); static final float tanh(final float x); static final float tanhPrime(final float y); static final float relu(final float x); static final float reluPrime(final float y); static final float linear(final float x); static final float linearPrime(final float y);  }
@Test public void testTanh() { float[] x = {-7.0f, -6.9f, -6.8f, -6.7f, -6.6f, -6.5f, -6.4f, -6.3f, -6.2f, -6.1f, -6.0f, -5.9f, -5.8f, -5.7f, -5.6f, -5.5f, -5.4f, -5.3f, -5.2f, -5.1f, -5.0f, -4.9f, -4.8f, -4.7f, -4.6f, -4.5f, -4.4f, -4.3f, -4.2f, -4.1f, -4.0f, -3.9f, -3.8f, -3.7f, -3.6f, -3.5f, -3.4f, -3.3f, -3.2f, -3.1f, -3.0f, -2.9f, -2.8f, -2.7f, -2.6f, -2.5f, -2.4f, -2.3f, -2.2f, -2.1f, -2.0f, -1.9f, -1.8f, -1.7f, -1.6f, -1.5f, -1.4f, -1.3f, -1.2f, -1.1f, -1.0f, -0.9f, -0.8f, -0.7f, -0.6f, -0.5f, -0.4f, -0.3f, -0.2f, -0.1f, 0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f, 1.9f, 2.0f, 2.1f, 2.2f, 2.3f, 2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f, 3.0f, 3.1f, 3.2f, 3.3f, 3.4f, 3.5f, 3.6f, 3.7f, 3.8f, 3.9f, 4.0f, 4.1f, 4.2f, 4.3f, 4.4f, 4.5f, 4.6f, 4.7f, 4.8f, 4.9f, 5.0f, 5.1f, 5.2f, 5.3f, 5.4f, 5.5f, 5.6f, 5.7f, 5.8f, 5.9f, 6.0f, 6.1f, 6.2f, 6.3f, 6.4f, 6.5f, 6.6f, 6.7f, 6.8f, 6.9f, 7.0f}; float[] y = {-0.999998f, -0.999998f, -0.999997f, -0.999997f, -0.999996f, -0.999995f, -0.999994f, -0.999993f, -0.999992f, -0.99999f, -0.999988f, -0.999985f, -0.999982f, -0.999978f, -0.999973f, -0.999967f, -0.999959f, -0.99995f, -0.999939f, -0.999926f, -0.999909f, -0.999889f, -0.999865f, -0.999835f, -0.999798f, -0.999753f, -0.999699f, -0.999632f, -0.99955f, -0.999451f, -0.999329f, -0.999181f, -0.999f, -0.998778f, -0.998508f, -0.998178f, -0.997775f, -0.997283f, -0.996682f, -0.995949f, -0.995055f, -0.993963f, -0.992631f, -0.991007f, -0.989027f, -0.986614f, -0.983675f, -0.980096f, -0.975743f, -0.970452f, -0.964028f, -0.956237f, -0.946806f, -0.935409f, -0.921669f, -0.905148f, -0.885352f, -0.861723f, -0.833655f, -0.800499f, -0.761594f, -0.716298f, -0.664037f, -0.604368f, -0.53705f, -0.462117f, -0.379949f, -0.291313f, -0.197375f, -0.099668f, -2.4869e-14f, 0.099668f, 0.197375f, 0.291313f, 0.379949f, 0.462117f, 0.53705f, 0.604368f, 0.664037f, 0.716298f, 0.761594f, 0.800499f, 0.833655f, 0.861723f, 0.885352f, 0.905148f, 0.921669f, 0.935409f, 0.946806f, 0.956237f, 0.964028f, 0.970452f, 0.975743f, 0.980096f, 0.983675f, 0.986614f, 0.989027f, 0.991007f, 0.992631f, 0.993963f, 0.995055f, 0.995949f, 0.996682f, 0.997283f, 0.997775f, 0.998178f, 0.998508f, 0.998778f, 0.999f, 0.999181f, 0.999329f, 0.999451f, 0.99955f, 0.999632f, 0.999699f, 0.999753f, 0.999798f, 0.999835f, 0.999865f, 0.999889f, 0.999909f, 0.999926f, 0.999939f, 0.99995f, 0.999959f, 0.999967f, 0.999973f, 0.999978f, 0.999982f, 0.999985f, 0.999988f, 0.99999f, 0.999992f, 0.999993f, 0.999994f, 0.999995f, 0.999996f, 0.999997f, 0.999997f, 0.999998f, 0.999998f}; for (int i = 0; i < x.length; i++) { float expResult = y[i]; float result = ActivationFunctions.tanh(x[i]); assertEquals(expResult, result, 1e-6); } }