Example #1
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
function Score([double[]] $InputVector) {
    return [math]::Exp(1.0)
}
"""

    interpreter = PowershellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #2
0
 def _negativebinomial_inversed(self, ast_to_transform):
     alpha = self._get_alpha()
     res = utils.sub(
         ast.NumVal(1.0),
         ast.ExpExpr(
             utils.sub(
                 ast.NumVal(0.0),
                 ast_to_transform)))
     return utils.div(
         ast.NumVal(-1.0),
         utils.mul(ast.NumVal(alpha), res) if alpha != 1.0 else res)
Example #3
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
function score(input) {
    return Math.exp(1.0);
}
"""

    interpreter = JavascriptInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #4
0
 def _negativebinomial_inversed(self, ast_to_transform):
     return utils.div(
         ast.NumVal(-1.0),
         utils.mul(
             ast.NumVal(self.model.model.family.link.alpha),
             utils.sub(
                 ast.NumVal(1.0),
                 ast.ExpExpr(
                     utils.sub(
                         ast.NumVal(0.0),
                         ast_to_transform)))))
Example #5
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
def score(input)
    Math.exp(1.0)
end
"""

    interpreter = RubyInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #6
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
score <- function(input) {
    return(exp(1.0))
}
"""

    interpreter = RInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #7
0
 def _rbf_kernel(self, support_vector):
     elem_wise = [
         ast.PowExpr(
             utils.sub(ast.NumVal(support_element), ast.FeatureRef(i)),
             ast.NumVal(2.0))
         for i, support_element in enumerate(support_vector)
     ]
     kernel = utils.apply_op_to_expressions(ast.BinNumOpType.ADD,
                                            *elem_wise)
     kernel = utils.mul(self._neg_gamma_expr, kernel)
     return ast.ExpExpr(kernel)
Example #8
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

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

    interpreter = RustInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #9
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
import math
def score(input):
    return math.exp(1.0)
"""

    interpreter = PythonInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #10
0
def _rbf_kernel_ast(estimator, sup_vec_value, to_reuse=False):
    return ast.ExpExpr(
        ast.BinNumExpr(
            ast.NumVal(-estimator.gamma),
            ast.PowExpr(
                ast.BinNumExpr(
                    ast.NumVal(sup_vec_value),
                    ast.FeatureRef(0),
                    ast.BinNumOpType.SUB),
                ast.NumVal(2)),
            ast.BinNumOpType.MUL),
        to_reuse=to_reuse)
Example #11
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
module Model where
score :: [Double] -> Double
score input =
    exp (1.0)
"""

    interpreter = HaskellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #12
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

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

    interpreter = PhpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #13
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    interpreter = interpreters.CInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #14
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    interpreter = GoInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #15
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
import 'dart:math';
double score(List<double> input) {
    return exp(1.0);
}
"""

    interpreter = DartInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #16
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
let score (input : double list) =
    let func0 =
        exp (1.0)
    (func0) / (func0)
"""

    interpreter = FSharpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #17
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
def score(input)
    var0 = Math.exp(1.0)
    (var0).fdiv(var0)
end
"""

    interpreter = RubyInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #18
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    var0 = np.exp(1.0)
    return (var0) / (var0)"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #19
0
def tanh(expr):
    expr = ast.IdExpr(expr, to_reuse=True)
    tanh_expr = utils.sub(
        ast.NumVal(1.0),
        utils.div(
            ast.NumVal(2.0),
            utils.add(ast.ExpExpr(utils.mul(ast.NumVal(2.0), expr)),
                      ast.NumVal(1.0))))
    return ast.IfExpr(
        utils.gt(expr, ast.NumVal(44.0)),  # exp(2*x) <= 2^127
        ast.NumVal(1.0),
        ast.IfExpr(utils.lt(expr, ast.NumVal(-44.0)), ast.NumVal(-1.0),
                   tanh_expr))
Example #20
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    interpreter = JavaInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #21
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
import math
def score(input):
    var0 = math.exp(1.0)
    return (var0) / (var0)
"""

    interpreter = PythonInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
def test_exp_fallback_expr():
    expr = ast.ExpExpr(ast.NumVal(2.0))

    interpreter = PythonInterpreter()
    interpreter.exponent_function_name = NotImplemented

    expected_code = """
import math
def score(input):
    return math.pow(2.718281828459045, 2.0)
"""

    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #23
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
score <- function(input) {
    var0 <- exp(1.0)
    return((var0) / (var0))
}
"""

    interpreter = RInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #24
0
def test_exp_expr():
    expr = ast.ExpExpr(ast.NumVal(1.0))

    expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
    score = Math.Exp(1.0)
End Function
End Module
"""

    interpreter = VisualBasicInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #25
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
function score(input) {
    var var0;
    var0 = Math.exp(1.0);
    return (var0) / (var0);
}
"""

    interpreter = JavascriptInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #26
0
def _rbf_kernel_ast(estimator, sup_vec_value, to_reuse=False):
    negative_gamma_ast = ast.BinNumExpr(ast.NumVal(0),
                                        ast.NumVal(estimator.gamma),
                                        ast.BinNumOpType.SUB,
                                        to_reuse=True)

    return ast.SubroutineExpr(ast.ExpExpr(
        ast.BinNumExpr(
            negative_gamma_ast,
            ast.PowExpr(
                ast.BinNumExpr(ast.NumVal(sup_vec_value),
                               ast.FeatureRef(0), ast.BinNumOpType.SUB),
                ast.NumVal(2)), ast.BinNumOpType.MUL)),
                              to_reuse=to_reuse)
Example #27
0
def test_leaves_cutoff_threshold():
    estimator = lightgbm.LGBMClassifier(n_estimators=2, random_state=1,
                                        max_depth=1)
    utils.train_model_classification_binary(estimator)

    assembler = assemblers.LightGBMModelAssembler(estimator,
                                                  leaves_cutoff_threshold=1)
    actual = assembler.assemble()

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.SubroutineExpr(
                        ast.BinNumExpr(
                            ast.BinNumExpr(
                                ast.NumVal(0),
                                ast.SubroutineExpr(
                                    ast.SubroutineExpr(
                                        ast.IfExpr(
                                            ast.CompExpr(
                                                ast.FeatureRef(23),
                                                ast.NumVal(868.2000000000002),
                                                ast.CompOpType.GT),
                                            ast.NumVal(0.25986931215073095),
                                            ast.NumVal(0.6237178414050242)))),
                                ast.BinNumOpType.ADD),
                            ast.SubroutineExpr(
                                ast.SubroutineExpr(
                                    ast.IfExpr(
                                        ast.CompExpr(
                                            ast.FeatureRef(7),
                                            ast.NumVal(0.05142),
                                            ast.CompOpType.GT),
                                        ast.NumVal(-0.1909605544006228),
                                        ast.NumVal(0.1293965108676673)))),
                            ast.BinNumOpType.ADD)),
                    ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB),
        sigmoid])

    assert utils.cmp_exprs(actual, expected)
Example #28
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
fn score(input: Vec<f64>) -> f64 {
    let var0: f64;
    var0 = f64::exp(1.0_f64);
    (var0) / (var0)
}
"""

    interpreter = RustInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #29
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    expected_code = """
function Score([double[]] $InputVector) {
    [double]$var0 = 0
    $var0 = [math]::Exp(1.0)
    return ($var0) / ($var0)
}
"""

    interpreter = PowershellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #30
0
def test_leaves_cutoff_threshold():
    estimator = xgboost.XGBClassifier(n_estimators=2,
                                      random_state=1,
                                      max_depth=1)
    utils.train_model_classification_binary(estimator)

    assembler = assemblers.XGBoostModelAssembler(estimator,
                                                 leaves_cutoff_threshold=1)
    actual = assembler.assemble()

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.SubroutineExpr(
                        ast.BinNumExpr(
                            ast.BinNumExpr(
                                ast.NumVal(-0.0),
                                ast.SubroutineExpr(
                                    ast.SubroutineExpr(
                                        ast.IfExpr(
                                            ast.CompExpr(
                                                ast.FeatureRef(20),
                                                ast.NumVal(16.7950001),
                                                ast.CompOpType.GTE),
                                            ast.NumVal(-0.173057005),
                                            ast.NumVal(0.163440868)))),
                                ast.BinNumOpType.ADD),
                            ast.SubroutineExpr(
                                ast.SubroutineExpr(
                                    ast.IfExpr(
                                        ast.CompExpr(ast.FeatureRef(27),
                                                     ast.NumVal(0.142349988),
                                                     ast.CompOpType.GTE),
                                        ast.NumVal(-0.161026895),
                                        ast.NumVal(0.149405137)))),
                            ast.BinNumOpType.ADD)), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB), sigmoid
    ])

    assert utils.cmp_exprs(actual, expected)