コード例 #1
0
ファイル: test_lightgbm.py プロジェクト: klesouza/m2cgen
def test_log1p_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2, random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy_lambda")
    utils.get_bounded_regression_model_trainer()(estimator)

    assembler = assemblers.LightGBMModelAssembler(estimator)
    actual = assembler.assemble()

    expected = ast.Log1pExpr(
        ast.ExpExpr(
            ast.BinNumExpr(
                ast.IfExpr(
                    ast.CompExpr(
                        ast.FeatureRef(12),
                        ast.NumVal(19.23),
                        ast.CompOpType.GT),
                    ast.NumVal(0.6623502468),
                    ast.NumVal(0.6683497987)),
                ast.IfExpr(
                    ast.CompExpr(
                        ast.FeatureRef(12),
                        ast.NumVal(15.145),
                        ast.CompOpType.GT),
                    ast.NumVal(0.1405181490),
                    ast.NumVal(0.1453602134)),
                ast.BinNumOpType.ADD)))

    assert utils.cmp_exprs(actual, expected)
コード例 #2
0
def test_log1p_exp_output_transform():
    estimator = lgb.LGBMRegressor(n_estimators=2,
                                  random_state=1,
                                  max_depth=1,
                                  objective="cross_entropy_lambda")
    utils.get_bounded_regression_model_trainer()(estimator)

    assembler = LightGBMModelAssembler(estimator)
    actual = assembler.assemble()

    expected = ast.Log1pExpr(
        ast.ExpExpr(
            ast.BinNumExpr(
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(12), ast.NumVal(19.23),
                                 ast.CompOpType.GT),
                    ast.NumVal(0.6622623010380544),
                    ast.NumVal(0.6684065452877841)),
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(12), ast.NumVal(15.145),
                                 ast.CompOpType.GT),
                    ast.NumVal(0.1404975120475147),
                    ast.NumVal(0.14535916856709272)), ast.BinNumOpType.ADD)))

    assert utils.cmp_exprs(actual, expected)
コード例 #3
0
ファイル: test_powershell.py プロジェクト: kingland/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
function Log1p([double] $x) {
    if ($x -eq 0.0) { return 0.0 }
    if ($x -eq -1.0) { return [double]::NegativeInfinity }
    if ($x -lt -1.0) { return [double]::NaN }
    [double] $xAbs = [math]::Abs($x)
    if ($xAbs -lt 0.5 * [double]::Epsilon) { return $x }
    if ((($x -gt 0.0) -and ($x -lt 1e-8))
        -or (($x -gt -1e-9) -and ($x -lt 0.0))) {
        return $x * (1.0 - $x * 0.5)
    }
    if ($xAbs -lt 0.375) {
        [double[]] $coeffs = @(
             0.10378693562743769800686267719098e+1,
            -0.13364301504908918098766041553133e+0,
             0.19408249135520563357926199374750e-1,
            -0.30107551127535777690376537776592e-2,
             0.48694614797154850090456366509137e-3,
            -0.81054881893175356066809943008622e-4,
             0.13778847799559524782938251496059e-4,
            -0.23802210894358970251369992914935e-5,
             0.41640416213865183476391859901989e-6,
            -0.73595828378075994984266837031998e-7,
             0.13117611876241674949152294345011e-7,
            -0.23546709317742425136696092330175e-8,
             0.42522773276034997775638052962567e-9,
            -0.77190894134840796826108107493300e-10,
             0.14075746481359069909215356472191e-10,
            -0.25769072058024680627537078627584e-11,
             0.47342406666294421849154395005938e-12,
            -0.87249012674742641745301263292675e-13,
             0.16124614902740551465739833119115e-13,
            -0.29875652015665773006710792416815e-14,
             0.55480701209082887983041321697279e-15,
            -0.10324619158271569595141333961932e-15)
        return $x * (1.0 - $x * (Chebyshev-Broucke ($x / 0.375) $coeffs))
    }
    return [math]::Log(1.0 + $x)
}
function Chebyshev-Broucke([double] $x, [double[]] $coeffs) {
    [double] $b2 = [double] $b1 = [double] $b0 = 0.0
    [double] $x2 = $x * 2
    for ([int] $i = $coeffs.Length - 1; $i -ge 0; --$i) {
        $b2 = $b1
        $b1 = $b0
        $b0 = $x2 * $b1 - $b2 + $coeffs[$i]
    }
    return ($b0 - $b2) * 0.5
}
function Score([double[]] $InputVector) {
    return Log1p $(2.0)
}
"""

    interpreter = PowershellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #4
0
ファイル: test_r.py プロジェクト: akashmavle5/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
score <- function(input) {
    return(log1p(2.0))
}
"""

    interpreter = RInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #5
0
ファイル: test_rust.py プロジェクト: BayesWitnesses/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
fn score(input: Vec<f64>) -> f64 {
    f64::ln_1p(2.0_f64)
}
"""

    interpreter = RustInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #6
0
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
function score(input) {
    return Math.log1p(2.0);
}
"""

    interpreter = JavascriptInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #7
0
ファイル: test_python.py プロジェクト: BayesWitnesses/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
import math
def score(input):
    return math.log1p(2.0)
"""

    interpreter = PythonInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #8
0
ファイル: test_go.py プロジェクト: donlinglok/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    interpreter = GoInterpreter()

    expected_code = """
import "math"
func score(input []float64) float64 {
    return math.Log1p(2.0)
}"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #9
0
ファイル: test_php.py プロジェクト: kingland/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
<?php
function score(array $input) {
    return log1p(2.0);
}
"""

    interpreter = PhpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #10
0
ファイル: test_haskell.py プロジェクト: akashmavle5/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
module Model where
score :: [Double] -> Double
score input =
    log1p (2.0)
log1p :: Double -> Double
log1p x
    | x == 0               = 0
    | x == -1              = -1 / 0
    | x < -1               = 0 / 0
    | x' < m_epsilon * 0.5 = x
    | (x > 0 && x < 1e-8) || (x > -1e-9 && x < 0)
                           = x * (1 - x * 0.5)
    | x' < 0.375           = x * (1 - x * chebyshevBroucke (x / 0.375) coeffs)
    | otherwise            = log (1 + x)
  where
    m_epsilon = encodeFloat (signif + 1) expo - 1.0
        where (signif, expo) = decodeFloat (1.0::Double)
    x' = abs x
    coeffs = [
         0.10378693562743769800686267719098e+1,
        -0.13364301504908918098766041553133e+0,
         0.19408249135520563357926199374750e-1,
        -0.30107551127535777690376537776592e-2,
         0.48694614797154850090456366509137e-3,
        -0.81054881893175356066809943008622e-4,
         0.13778847799559524782938251496059e-4,
        -0.23802210894358970251369992914935e-5,
         0.41640416213865183476391859901989e-6,
        -0.73595828378075994984266837031998e-7,
         0.13117611876241674949152294345011e-7,
        -0.23546709317742425136696092330175e-8,
         0.42522773276034997775638052962567e-9,
        -0.77190894134840796826108107493300e-10,
         0.14075746481359069909215356472191e-10,
        -0.25769072058024680627537078627584e-11,
         0.47342406666294421849154395005938e-12,
        -0.87249012674742641745301263292675e-13,
         0.16124614902740551465739833119115e-13,
        -0.29875652015665773006710792416815e-14,
         0.55480701209082887983041321697279e-15,
        -0.10324619158271569595141333961932e-15]
    chebyshevBroucke i = fini . foldr step (0, 0, 0)
        where
            step k (b0, b1, _) = ((k + i * 2 * b0 - b1), b0, b1)
            fini (b0, _, b2) = (b0 - b2) * 0.5
"""

    interpreter = HaskellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #11
0
ファイル: test_c.py プロジェクト: rspadim/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    interpreter = interpreters.CInterpreter()

    expected_code = """
#include <math.h>
double score(double * input) {
    return log1p(2.0);
}"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #12
0
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    interpreter = JavaInterpreter()

    expected_code = """
public class Model {
    public static double score(double[] input) {
        return Math.log1p(2.0);
    }
}"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #13
0
ファイル: test_f_sharp.py プロジェクト: yarix/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
let private chebyshevBroucke i coeffs =
    let step k (b0, b1, _) = ((k + i * 2.0 * b0 - b1), b0, b1)
    let fini (b0, _, b2) = (b0 - b2) * 0.5
    fini (List.foldBack step coeffs (0.0, 0.0, 0.0))
let private log1p x =
    let x' = abs x
    let chebCoeffs = [
         0.10378693562743769800686267719098e+1;
        -0.13364301504908918098766041553133e+0;
         0.19408249135520563357926199374750e-1;
        -0.30107551127535777690376537776592e-2;
         0.48694614797154850090456366509137e-3;
        -0.81054881893175356066809943008622e-4;
         0.13778847799559524782938251496059e-4;
        -0.23802210894358970251369992914935e-5;
         0.41640416213865183476391859901989e-6;
        -0.73595828378075994984266837031998e-7;
         0.13117611876241674949152294345011e-7;
        -0.23546709317742425136696092330175e-8;
         0.42522773276034997775638052962567e-9;
        -0.77190894134840796826108107493300e-10;
         0.14075746481359069909215356472191e-10;
        -0.25769072058024680627537078627584e-11;
         0.47342406666294421849154395005938e-12;
        -0.87249012674742641745301263292675e-13;
         0.16124614902740551465739833119115e-13;
        -0.29875652015665773006710792416815e-14;
         0.55480701209082887983041321697279e-15;
        -0.10324619158271569595141333961932e-15]
    match x with
        | 0.0               -> 0.0
        | -1.0              -> -infinity
        | i when i < -1.0   -> nan
        | i when x' < System.Double.Epsilon * 0.5
                            -> x
        | i when (x > 0.0 && x < 1e-8) || (x > -1e-9 && x < 0.0)
                            -> x * (1.0 - x * 0.5)
        | i when x' < 0.375 ->
            x * (1.0 - x * chebyshevBroucke (x / 0.375) chebCoeffs)
        | _                 -> log (1.0 + x)
let score (input : double list) =
    log1p (2.0)
"""

    interpreter = FSharpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #14
0
def test_log1p_fallback_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    interpreter = PythonInterpreter()
    interpreter.log1p_function_name = NotImplemented

    expected_code = """
import math
def score(input):
    var1 = 2.0
    var2 = (1.0) + (var1)
    var3 = (var2) - (1.0)
    if (var3) == (0.0):
        var0 = var1
    else:
        var0 = ((var1) * (math.log(var2))) / (var3)
    return var0
"""

    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #15
0
ファイル: test_c_sharp.py プロジェクト: BayesWitnesses/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
using static System.Math;
namespace ML {
    public static class Model {
        public static double Score(double[] input) {
            return Log1p(2.0);
        }
        private static double Log1p(double x) {
            if (x == 0.0)
                return 0.0;
            if (x == -1.0)
                return double.NegativeInfinity;
            if (x < -1.0)
                return double.NaN;
            double xAbs = Abs(x);
            if (xAbs < 0.5 * double.Epsilon)
                return x;
            if ((x > 0.0 && x < 1e-8) || (x > -1e-9 && x < 0.0))
                return x * (1.0 - x * 0.5);
            if (xAbs < 0.375) {
                double[] coeffs = {
                     0.10378693562743769800686267719098e+1,
                    -0.13364301504908918098766041553133e+0,
                     0.19408249135520563357926199374750e-1,
                    -0.30107551127535777690376537776592e-2,
                     0.48694614797154850090456366509137e-3,
                    -0.81054881893175356066809943008622e-4,
                     0.13778847799559524782938251496059e-4,
                    -0.23802210894358970251369992914935e-5,
                     0.41640416213865183476391859901989e-6,
                    -0.73595828378075994984266837031998e-7,
                     0.13117611876241674949152294345011e-7,
                    -0.23546709317742425136696092330175e-8,
                     0.42522773276034997775638052962567e-9,
                    -0.77190894134840796826108107493300e-10,
                     0.14075746481359069909215356472191e-10,
                    -0.25769072058024680627537078627584e-11,
                     0.47342406666294421849154395005938e-12,
                    -0.87249012674742641745301263292675e-13,
                     0.16124614902740551465739833119115e-13,
                    -0.29875652015665773006710792416815e-14,
                     0.55480701209082887983041321697279e-15,
                    -0.10324619158271569595141333961932e-15};
                return x * (1.0 - x * ChebyshevBroucke(x / 0.375, coeffs));
            }
            return Log(1.0 + x);
        }
        private static double ChebyshevBroucke(double x, double[] coeffs) {
            double b0, b1, b2, x2;
            b2 = b1 = b0 = 0.0;
            x2 = x * 2;
            for (int i = coeffs.Length - 1; i >= 0; --i) {
                b2 = b1;
                b1 = b0;
                b0 = x2 * b1 - b2 + coeffs[i];
            }
            return (b0 - b2) * 0.5;
        }
    }
}
"""

    interpreter = CSharpInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #16
0
ファイル: boosting.py プロジェクト: klesouza/m2cgen
 def _log1p_exp_transform(self, expr):
     return ast.Log1pExpr(ast.ExpExpr(expr))
コード例 #17
0
ファイル: test_ruby.py プロジェクト: BayesWitnesses/m2cgen
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
def score(input)
    log1p(2.0)
end
def log1p(x)
    if x == 0.0
        return 0.0
    end
    if x == -1.0
        return -Float::INFINITY
    end
    if x < -1.0
        return Float::NAN
    end
    x_abs = x.abs
    if x_abs < 0.5 * Float::EPSILON
        return x
    end
    if (x > 0.0 && x < 1e-8) || (x > -1e-9 && x < 0.0)
        return x * (1.0 - x * 0.5)
    end
    if x_abs < 0.375
        coeffs = [
             0.10378693562743769800686267719098e+1,
            -0.13364301504908918098766041553133e+0,
             0.19408249135520563357926199374750e-1,
            -0.30107551127535777690376537776592e-2,
             0.48694614797154850090456366509137e-3,
            -0.81054881893175356066809943008622e-4,
             0.13778847799559524782938251496059e-4,
            -0.23802210894358970251369992914935e-5,
             0.41640416213865183476391859901989e-6,
            -0.73595828378075994984266837031998e-7,
             0.13117611876241674949152294345011e-7,
            -0.23546709317742425136696092330175e-8,
             0.42522773276034997775638052962567e-9,
            -0.77190894134840796826108107493300e-10,
             0.14075746481359069909215356472191e-10,
            -0.25769072058024680627537078627584e-11,
             0.47342406666294421849154395005938e-12,
            -0.87249012674742641745301263292675e-13,
             0.16124614902740551465739833119115e-13,
            -0.29875652015665773006710792416815e-14,
             0.55480701209082887983041321697279e-15,
            -0.10324619158271569595141333961932e-15]
        return x * (1.0 - x * chebyshev_broucke(x / 0.375, coeffs))
    end
    return Math.log(1.0 + x)
end
def chebyshev_broucke(x, coeffs)
    b2 = b1 = b0 = 0.0
    x2 = x * 2
    coeffs.reverse_each do |i|
        b2 = b1
        b1 = b0
        b0 = x2 * b1 - b2 + i
    end
    (b0 - b2) * 0.5
end
"""

    interpreter = RubyInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
コード例 #18
0
ファイル: test_ast.py プロジェクト: BayesWitnesses/m2cgen
    ) == 0

    assert ast.count_exprs(
        ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.ADD),
        exclude_list={ast.BinNumExpr}
    ) == 2


EXPR_WITH_ALL_EXPRS = ast.BinVectorNumExpr(
    ast.BinVectorExpr(
        ast.VectorVal([
            ast.AbsExpr(ast.NumVal(-2)),
            ast.AtanExpr(ast.NumVal(2)),
            ast.ExpExpr(ast.NumVal(2)),
            ast.LogExpr(ast.NumVal(2)),
            ast.Log1pExpr(ast.NumVal(2)),
            ast.SigmoidExpr(ast.NumVal(2)),
            ast.SqrtExpr(ast.NumVal(2)),
            ast.PowExpr(ast.NumVal(2), ast.NumVal(3)),
            ast.TanhExpr(ast.NumVal(1)),
            ast.BinNumExpr(
                ast.NumVal(0),
                ast.FeatureRef(0),
                ast.BinNumOpType.ADD)
        ]),
        ast.IdExpr(
            ast.SoftmaxExpr([
                ast.NumVal(1),
                ast.NumVal(2),
                ast.NumVal(3),
                ast.NumVal(4),
コード例 #19
0
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
Module Model
Function ChebyshevBroucke(ByVal x As Double, _
                          ByRef coeffs() As Double) As Double
    Dim b2 as Double
    Dim b1 as Double
    Dim b0 as Double
    Dim x2 as Double
    b2 = 0.0
    b1 = 0.0
    b0 = 0.0
    x2 = x * 2
    Dim i as Integer
    For i = UBound(coeffs) - 1 To 0 Step -1
        b2 = b1
        b1 = b0
        b0 = x2 * b1 - b2 + coeffs(i)
    Next i
    ChebyshevBroucke = (b0 - b2) * 0.5
End Function
Function Log1p(ByVal x As Double) As Double
    If x = 0.0 Then
        Log1p = 0.0
        Exit Function
    End If
    If x = -1.0 Then
        On Error Resume Next
        Log1p = -1.0 / 0.0
        Exit Function
    End If
    If x < -1.0 Then
        On Error Resume Next
        Log1p = 0.0 / 0.0
        Exit Function
    End If
    Dim xAbs As Double
    xAbs = Math.Abs(x)
    If xAbs < 0.5 * 4.94065645841247e-324 Then
        Log1p = x
        Exit Function
    End If
    If (x > 0.0 AND x < 1e-8) OR (x > -1e-9 AND x < 0.0) Then
        Log1p = x * (1.0 - x * 0.5)
        Exit Function
    End If
    If xAbs < 0.375 Then
        Dim coeffs(22) As Double
        coeffs(0)  =  0.10378693562743769800686267719098e+1
        coeffs(1)  = -0.13364301504908918098766041553133e+0
        coeffs(2)  =  0.19408249135520563357926199374750e-1
        coeffs(3)  = -0.30107551127535777690376537776592e-2
        coeffs(4)  =  0.48694614797154850090456366509137e-3
        coeffs(5)  = -0.81054881893175356066809943008622e-4
        coeffs(6)  =  0.13778847799559524782938251496059e-4
        coeffs(7)  = -0.23802210894358970251369992914935e-5
        coeffs(8)  =  0.41640416213865183476391859901989e-6
        coeffs(9)  = -0.73595828378075994984266837031998e-7
        coeffs(10) =  0.13117611876241674949152294345011e-7
        coeffs(11) = -0.23546709317742425136696092330175e-8
        coeffs(12) =  0.42522773276034997775638052962567e-9
        coeffs(13) = -0.77190894134840796826108107493300e-10
        coeffs(14) =  0.14075746481359069909215356472191e-10
        coeffs(15) = -0.25769072058024680627537078627584e-11
        coeffs(16) =  0.47342406666294421849154395005938e-12
        coeffs(17) = -0.87249012674742641745301263292675e-13
        coeffs(18) =  0.16124614902740551465739833119115e-13
        coeffs(19) = -0.29875652015665773006710792416815e-14
        coeffs(20) =  0.55480701209082887983041321697279e-15
        coeffs(21) = -0.10324619158271569595141333961932e-15
        Log1p = x * (1.0 - x * ChebyshevBroucke(x / 0.375, coeffs))
        Exit Function
    End If
    Log1p = Math.log(1.0 + x)
End Function
Function Score(ByRef inputVector() As Double) As Double
    Score = Log1p(2.0)
End Function
End Module
"""

    interpreter = VisualBasicInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)