Java Code Examples for org.apache.commons.math3.util.FastMath#tanh()
The following examples show how to use
org.apache.commons.math3.util.FastMath#tanh() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DerivativeTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testTanhDerivative() { //Derivative of sigmoid: ds(x)/dx = s(x)*(1-s(x)) //s(x) = 1 / (exp(-x) + 1) INDArray z = Nd4j.zeros(100); double[] expOut = new double[100]; for (int i = 0; i < 100; i++) { double x = 0.1 * (i - 50); z.putScalar(i, x); double tanh = FastMath.tanh(x); expOut[i] = 1.0 - tanh * tanh; } INDArray zPrime = Nd4j.getExecutioner().exec(new TanhDerivative(z)); for (int i = 0; i < 100; i++) { double relError = Math.abs(expOut[i] - zPrime.getDouble(i)) / (Math.abs(expOut[i]) + Math.abs(zPrime.getDouble(i))); assertTrue(relError < REL_ERROR_TOLERANCE); } }
Example 2
Source File: Tanh.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public UnivariateFunction derivative() { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { final double tanhX = FastMath.tanh(x); return 1 - tanhX * tanhX; } }; }
Example 3
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 4 votes |
/** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array <em>cannot</em> be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); }
Example 4
Source File: Builtin.java From systemds with Apache License 2.0 | 4 votes |
@Override public double execute (double in) { switch(bFunc) { case SIN: return FASTMATH ? FastMath.sin(in) : Math.sin(in); case COS: return FASTMATH ? FastMath.cos(in) : Math.cos(in); case TAN: return FASTMATH ? FastMath.tan(in) : Math.tan(in); case ASIN: return FASTMATH ? FastMath.asin(in) : Math.asin(in); case ACOS: return FASTMATH ? FastMath.acos(in) : Math.acos(in); case ATAN: return Math.atan(in); //faster in Math // FastMath.*h is faster 98% of time than Math.*h in initial micro-benchmarks case SINH: return FASTMATH ? FastMath.sinh(in) : Math.sinh(in); case COSH: return FASTMATH ? FastMath.cosh(in) : Math.cosh(in); case TANH: return FASTMATH ? FastMath.tanh(in) : Math.tanh(in); case CEIL: return FASTMATH ? FastMath.ceil(in) : Math.ceil(in); case FLOOR: return FASTMATH ? FastMath.floor(in) : Math.floor(in); case LOG: return Math.log(in); //faster in Math case LOG_NZ: return (in==0) ? 0 : Math.log(in); //faster in Math case ABS: return Math.abs(in); //no need for FastMath case SIGN: return FASTMATH ? FastMath.signum(in) : Math.signum(in); case SQRT: return Math.sqrt(in); //faster in Math case EXP: return FASTMATH ? FastMath.exp(in) : Math.exp(in); case ROUND: return Math.round(in); //no need for FastMath case PLOGP: if (in == 0.0) return 0.0; else if (in < 0) return Double.NaN; else //faster in Math return in * Math.log(in); case SPROP: //sample proportion: P*(1-P) return in * (1 - in); case SIGMOID: //sigmoid: 1/(1+exp(-x)) return FASTMATH ? 1 / (1 + FastMath.exp(-in)) : 1 / (1 + Math.exp(-in)); case ISNA: return Double.isNaN(in) ? 1 : 0; case ISNAN: return Double.isNaN(in) ? 1 : 0; case ISINF: return Double.isInfinite(in) ? 1 : 0; default: throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc); } }
Example 5
Source File: Tanh.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); }
Example 6
Source File: Tanh.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); }
Example 7
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 4 votes |
/** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array <em>cannot</em> be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); }
Example 8
Source File: SparseGradient.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public SparseGradient tanh() { final double t = FastMath.tanh(value); return new SparseGradient(t, 1 - t * t, derivatives); }
Example 9
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 4 votes |
/** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array <em>cannot</em> be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); }
Example 10
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static double[] vectTanhWrite(double[] a, int[] aix, int ai, int alen, int len) { double[] c = allocVector(len, true); for( int j = ai; j < ai+alen; j++ ) c[aix[j]] = FastMath.tanh(a[j]); return c; }
Example 11
Source File: Tanh.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); }
Example 12
Source File: DSCompiler.java From astor with GNU General Public License v2.0 | 4 votes |
/** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array <em>cannot</em> be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); }
Example 13
Source File: Builtin.java From systemds with Apache License 2.0 | 4 votes |
@Override public double execute (double in) { switch(bFunc) { case SIN: return FASTMATH ? FastMath.sin(in) : Math.sin(in); case COS: return FASTMATH ? FastMath.cos(in) : Math.cos(in); case TAN: return FASTMATH ? FastMath.tan(in) : Math.tan(in); case ASIN: return FASTMATH ? FastMath.asin(in) : Math.asin(in); case ACOS: return FASTMATH ? FastMath.acos(in) : Math.acos(in); case ATAN: return Math.atan(in); //faster in Math // FastMath.*h is faster 98% of time than Math.*h in initial micro-benchmarks case SINH: return FASTMATH ? FastMath.sinh(in) : Math.sinh(in); case COSH: return FASTMATH ? FastMath.cosh(in) : Math.cosh(in); case TANH: return FASTMATH ? FastMath.tanh(in) : Math.tanh(in); case CEIL: return FASTMATH ? FastMath.ceil(in) : Math.ceil(in); case FLOOR: return FASTMATH ? FastMath.floor(in) : Math.floor(in); case LOG: return Math.log(in); //faster in Math case LOG_NZ: return (in==0) ? 0 : Math.log(in); //faster in Math case ABS: return Math.abs(in); //no need for FastMath case SIGN: return FASTMATH ? FastMath.signum(in) : Math.signum(in); case SQRT: return Math.sqrt(in); //faster in Math case EXP: return FASTMATH ? FastMath.exp(in) : Math.exp(in); case ROUND: return Math.round(in); //no need for FastMath case PLOGP: if (in == 0.0) return 0.0; else if (in < 0) return Double.NaN; else //faster in Math return in * Math.log(in); case SPROP: //sample proportion: P*(1-P) return in * (1 - in); case SIGMOID: //sigmoid: 1/(1+exp(-x)) return FASTMATH ? 1 / (1 + FastMath.exp(-in)) : 1 / (1 + Math.exp(-in)); case ISNA: return Double.isNaN(in) ? 1 : 0; case ISNAN: return Double.isNaN(in) ? 1 : 0; case ISINF: return Double.isInfinite(in) ? 1 : 0; default: throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc); } }
Example 14
Source File: HyperbolicTangentKernel.java From clust4j with Apache License 2.0 | 4 votes |
@Override public double getSimilarity(double[] a, double[] b) { return FastMath.tanh(getAlpha() * VecUtils.innerProduct(a, b) + getConstant()); }
Example 15
Source File: Math_10_DSCompiler_s.java From coming with MIT License | 4 votes |
/** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array <em>cannot</em> be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); }
Example 16
Source File: Tanh.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); }
Example 17
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static double[] vectTanhWrite(double[] a, int[] aix, int ai, int alen, int len) { double[] c = allocVector(len, true); for( int j = ai; j < ai+alen; j++ ) c[aix[j]] = FastMath.tanh(a[j]); return c; }
Example 18
Source File: SparseGradient.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public SparseGradient tanh() { final double t = FastMath.tanh(value); return new SparseGradient(t, 1 - t * t, derivatives); }
Example 19
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static void vectTanhAdd(double[] a, double[] c, int[] aix, int ai, int ci, int alen, int len) { for( int j = ai; j < ai+alen; j++ ) c[ci + aix[j]] += FastMath.tanh(a[j]); }
Example 20
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static void vectTanhAdd(double[] a, double[] c, int ai, int ci, int len) { for( int j = ai; j < ai+len; j++, ci++) c[ci] += FastMath.tanh(a[j]); }