Logistic function

Percentage Accurate: 99.8% → 99.8%
Time: 10.7s
Alternatives: 14
Speedup: 1.0×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

Sampling outcomes in binary32 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 14 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + e^{\frac{-x}{s}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + exp((-x / s)));
end
\begin{array}{l}

\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}

Alternative 1: 99.8% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + \frac{1}{e^{\frac{x}{s}}}} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (/ 1.0 (exp (/ x s))))))
float code(float x, float s) {
	return 1.0f / (1.0f + (1.0f / expf((x / s))));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (1.0e0 + (1.0e0 / exp((x / s))))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) / exp(Float32(x / s)))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (single(1.0) + (single(1.0) / exp((x / s))));
end
\begin{array}{l}

\\
\frac{1}{1 + \frac{1}{e^{\frac{x}{s}}}}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-exp.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{e^{\frac{\mathsf{neg}\left(x\right)}{s}}}} \]
    2. lift-/.f32N/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\frac{\mathsf{neg}\left(x\right)}{s}}}} \]
    3. lift-neg.f32N/A

      \[\leadsto \frac{1}{1 + e^{\frac{\color{blue}{\mathsf{neg}\left(x\right)}}{s}}} \]
    4. distribute-frac-negN/A

      \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(\frac{x}{s}\right)}}} \]
    5. exp-negN/A

      \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
    6. lower-/.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
    7. lower-exp.f32N/A

      \[\leadsto \frac{1}{1 + \frac{1}{\color{blue}{e^{\frac{x}{s}}}}} \]
    8. lower-/.f3299.9

      \[\leadsto \frac{1}{1 + \frac{1}{e^{\color{blue}{\frac{x}{s}}}}} \]
  4. Applied rewrites99.9%

    \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
  5. Add Preprocessing

Alternative 2: 93.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;e^{\frac{x}{-s}} \leq 39999999311872:\\ \;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{\mathsf{fma}\left(x, \frac{x}{s} \cdot 0.5, x\right)}{s}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{x}{s \cdot s}, \mathsf{fma}\left(-0.16666666666666666, \frac{x}{s}, 0.5\right), \frac{-1}{s}\right), 2\right)}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= (exp (/ x (- s))) 39999999311872.0)
   (/ 1.0 (+ 1.0 (/ 1.0 (+ 1.0 (/ (fma x (* (/ x s) 0.5) x) s)))))
   (/
    1.0
    (fma
     x
     (fma (/ x (* s s)) (fma -0.16666666666666666 (/ x s) 0.5) (/ -1.0 s))
     2.0))))
float code(float x, float s) {
	float tmp;
	if (expf((x / -s)) <= 39999999311872.0f) {
		tmp = 1.0f / (1.0f + (1.0f / (1.0f + (fmaf(x, ((x / s) * 0.5f), x) / s))));
	} else {
		tmp = 1.0f / fmaf(x, fmaf((x / (s * s)), fmaf(-0.16666666666666666f, (x / s), 0.5f), (-1.0f / s)), 2.0f);
	}
	return tmp;
}
function code(x, s)
	tmp = Float32(0.0)
	if (exp(Float32(x / Float32(-s))) <= Float32(39999999311872.0))
		tmp = Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(fma(x, Float32(Float32(x / s) * Float32(0.5)), x) / s)))));
	else
		tmp = Float32(Float32(1.0) / fma(x, fma(Float32(x / Float32(s * s)), fma(Float32(-0.16666666666666666), Float32(x / s), Float32(0.5)), Float32(Float32(-1.0) / s)), Float32(2.0)));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;e^{\frac{x}{-s}} \leq 39999999311872:\\
\;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{\mathsf{fma}\left(x, \frac{x}{s} \cdot 0.5, x\right)}{s}}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{x}{s \cdot s}, \mathsf{fma}\left(-0.16666666666666666, \frac{x}{s}, 0.5\right), \frac{-1}{s}\right), 2\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f32 (/.f32 (neg.f32 x) s)) < 3.99999993e13

    1. Initial program 99.8%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-exp.f32N/A

        \[\leadsto \frac{1}{1 + \color{blue}{e^{\frac{\mathsf{neg}\left(x\right)}{s}}}} \]
      2. lift-/.f32N/A

        \[\leadsto \frac{1}{1 + e^{\color{blue}{\frac{\mathsf{neg}\left(x\right)}{s}}}} \]
      3. lift-neg.f32N/A

        \[\leadsto \frac{1}{1 + e^{\frac{\color{blue}{\mathsf{neg}\left(x\right)}}{s}}} \]
      4. distribute-frac-negN/A

        \[\leadsto \frac{1}{1 + e^{\color{blue}{\mathsf{neg}\left(\frac{x}{s}\right)}}} \]
      5. exp-negN/A

        \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
      6. lower-/.f32N/A

        \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
      7. lower-exp.f32N/A

        \[\leadsto \frac{1}{1 + \frac{1}{\color{blue}{e^{\frac{x}{s}}}}} \]
      8. lower-/.f3299.7

        \[\leadsto \frac{1}{1 + \frac{1}{e^{\color{blue}{\frac{x}{s}}}}} \]
    4. Applied rewrites99.7%

      \[\leadsto \frac{1}{1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}} \]
    5. Taylor expanded in s around -inf

      \[\leadsto \frac{1}{1 + \frac{1}{\color{blue}{1 + -1 \cdot \frac{-1 \cdot x + \frac{-1}{2} \cdot \frac{{x}^{2}}{s}}{s}}}} \]
    6. Step-by-step derivation
      1. lower-+.f32N/A

        \[\leadsto \frac{1}{1 + \frac{1}{\color{blue}{1 + -1 \cdot \frac{-1 \cdot x + \frac{-1}{2} \cdot \frac{{x}^{2}}{s}}{s}}}} \]
      2. associate-*r/N/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \color{blue}{\frac{-1 \cdot \left(-1 \cdot x + \frac{-1}{2} \cdot \frac{{x}^{2}}{s}\right)}{s}}}} \]
      3. mul-1-negN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\color{blue}{\mathsf{neg}\left(\left(-1 \cdot x + \frac{-1}{2} \cdot \frac{{x}^{2}}{s}\right)\right)}}{s}}} \]
      4. +-commutativeN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\mathsf{neg}\left(\color{blue}{\left(\frac{-1}{2} \cdot \frac{{x}^{2}}{s} + -1 \cdot x\right)}\right)}{s}}} \]
      5. distribute-neg-inN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{2} \cdot \frac{{x}^{2}}{s}\right)\right) + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}}{s}}} \]
      6. distribute-lft-neg-inN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{2}\right)\right) \cdot \frac{{x}^{2}}{s}} + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}{s}}} \]
      7. metadata-evalN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\color{blue}{\frac{1}{2}} \cdot \frac{{x}^{2}}{s} + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)}{s}}} \]
      8. sub-negN/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \frac{\color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{s} - -1 \cdot x}}{s}}} \]
      9. lower-/.f32N/A

        \[\leadsto \frac{1}{1 + \frac{1}{1 + \color{blue}{\frac{\frac{1}{2} \cdot \frac{{x}^{2}}{s} - -1 \cdot x}{s}}}} \]
    7. Applied rewrites96.0%

      \[\leadsto \frac{1}{1 + \frac{1}{\color{blue}{1 + \frac{\mathsf{fma}\left(x, \frac{x}{s} \cdot 0.5, x\right)}{s}}}} \]

    if 3.99999993e13 < (exp.f32 (/.f32 (neg.f32 x) s))

    1. Initial program 99.9%

      \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \frac{1}{\color{blue}{2 + x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right)}} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \frac{1}{\color{blue}{x \cdot \left(x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}\right) + 2}} \]
      2. lower-fma.f32N/A

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, x \cdot \left(\frac{-1}{6} \cdot \frac{x}{{s}^{3}} + \frac{1}{2} \cdot \frac{1}{{s}^{2}}\right) - \frac{1}{s}, 2\right)}} \]
    5. Applied rewrites88.5%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{x}{s \cdot s}, \mathsf{fma}\left(-0.16666666666666666, \frac{x}{s}, 0.5\right), \frac{-1}{s}\right), 2\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification93.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{\frac{x}{-s}} \leq 39999999311872:\\ \;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{\mathsf{fma}\left(x, \frac{x}{s} \cdot 0.5, x\right)}{s}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{x}{s \cdot s}, \mathsf{fma}\left(-0.16666666666666666, \frac{x}{s}, 0.5\right), \frac{-1}{s}\right), 2\right)}\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024228 
(FPCore (x s)
  :name "Logistic function"
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))