Example #1
0
def test_count_exprs():
    assert ast.count_exprs(
        ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.ADD)
    ) == 3

    assert ast.count_exprs(
        ast.ExpExpr(ast.NumVal(2))
    ) == 2

    assert ast.count_exprs(
        ast.VectorVal([
            ast.NumVal(2),
            ast.TanhExpr(ast.NumVal(3))
        ])
    ) == 4

    assert ast.count_exprs(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(2), ast.NumVal(0), ast.CompOpType.GT),
            ast.NumVal(3),
            ast.NumVal(4),
        )
    ) == 6

    assert ast.count_exprs(ast.NumVal(1)) == 1
Example #2
0
def test_count_all_exprs_types():
    expr = ast.BinVectorNumExpr(
        ast.BinVectorExpr(
            ast.VectorVal([
                ast.ExpExpr(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.VectorVal([
                ast.NumVal(1),
                ast.NumVal(2),
                ast.NumVal(3),
                ast.NumVal(4),
                ast.FeatureRef(1)
            ]),
            ast.BinNumOpType.SUB),
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(2), ast.NumVal(0), ast.CompOpType.GT),
            ast.NumVal(3),
            ast.NumVal(4),
        ),
        ast.BinNumOpType.MUL)

    assert ast.count_exprs(expr) == 27
Example #3
0
 def kernel_ast(sup_vec_value):
     return ast.TanhExpr(
         ast.BinNumExpr(
             ast.BinNumExpr(
                 ast.NumVal(estimator.gamma),
                 ast.BinNumExpr(ast.NumVal(sup_vec_value),
                                ast.FeatureRef(0), ast.BinNumOpType.MUL),
                 ast.BinNumOpType.MUL), ast.NumVal(0.0),
             ast.BinNumOpType.ADD))
Example #4
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
let score (input : double list) =
    tanh (2.0)
"""

    interpreter = FSharpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #5
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = JavascriptInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #6
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = RustInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #7
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = RInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #8
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
def score(input)
    Math.tanh(2.0)
end
"""

    interpreter = RubyInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #9
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = PythonInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #10
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    return np.tanh(2.0)"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #11
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = PowershellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #12
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = HaskellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #13
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = GoInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #14
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

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

    interpreter = PhpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #15
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = interpreters.CInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #16
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = JavaInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #17
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
Module Model
Function Tanh(ByVal number As Double) As Double
    ' Implementation is taken from
    ' https://github.com/golang/go/blob/master/src/math/tanh.go
    Dim z As Double
    z = Math.Abs(number)
    If z > 0.440148459655565271479942397125e+2 Then
        If number < 0 Then
            Tanh = -1.0
            Exit Function
        End If
        Tanh = 1.0
        Exit Function
    End If
    If z >= 0.625 Then
        z = 1 - 2 / (Math.Exp(2 * z) + 1)
        If number < 0 Then
            z = -z
        End If
        Tanh = z
        Exit Function
    End If
    If number = 0 Then
        Tanh = 0.0
        Exit Function
    End If
    Dim s As Double
    s = number * number
    z = number + number * s _
        * ((-0.964399179425052238628e+0 * s + -0.992877231001918586564e+2) _
           * s + -0.161468768441708447952e+4) _
        / (((s + 0.112811678491632931402e+3) _
            * s + 0.223548839060100448583e+4) _
           * s + 0.484406305325125486048e+4)
    Tanh = z
End Function
Function Score(ByRef inputVector() As Double) As Double
    Score = Tanh(2.0)
End Function
End Module
"""

    interpreter = VisualBasicInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #18
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
using static System.Math;
namespace ML {
    public static class Model {
        public static double Score(double[] input) {
            return Tanh(2.0);
        }
    }
}
"""

    interpreter = CSharpInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #19
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
import 'dart:math';
double score(List<double> input) {
    return tanh(2.0);
}
double tanh(double x) {
    if (x > 22.0)
        return 1.0;
    if (x < -22.0)
        return -1.0;
    return ((exp(2*x) - 1)/(exp(2*x) + 1));
}
"""
    interpreter = DartInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #20
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
import 'dart:math';
double score(List<double> input) {
    return tanh(2.0);
}
double tanh(double x) {
    // Implementation is taken from
    // https://github.com/golang/go/blob/master/src/math/tanh.go
    double z;
    z = x.abs();
    if (z > 0.440148459655565271479942397125e+2) {
        if (x < 0) {
            return -1.0;
        }
        return 1.0;
    }
    if (z >= 0.625) {
        z = 1 - 2 / (exp(2 * z) + 1);
        if (x < 0) {
            z = -z;
        }
        return z;
    }
    if (x == 0) {
        return 0.0;
    }
    double s;
    s = x * x;
    z = x + x * s
        * ((-0.964399179425052238628e+0 * s + -0.992877231001918586564e+2)
        * s + -0.161468768441708447952e+4) / (((s + 0.112811678491632931402e+3)
        * s + 0.223548839060100448583e+4) * s + 0.484406305325125486048e+4);
    return z;
}
"""

    interpreter = DartInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
def test_tanh_fallback_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = PythonInterpreter()
    interpreter.tanh_function_name = NotImplemented

    expected_code = """
import math
def score(input):
    var1 = 2.0
    if (var1) > (44.0):
        var0 = 1.0
    else:
        if (var1) < (-44.0):
            var0 = -1.0
        else:
            var0 = (1.0) - ((2.0) / ((math.exp((2.0) * (var1))) + (1.0)))
    return var0
"""

    assert_code_equal(interpreter.interpret(expr), expected_code)
Example #22
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    expected_code = """
Module Model
Function Tanh(ByVal number As Double) As Double
    If number > 44.0 Then  ' exp(2*x) <= 2^127
        Tanh = 1.0
        Exit Function
    End If
    If number < -44.0 Then
        Tanh = -1.0
        Exit Function
    End If
    Tanh = (Math.Exp(2 * number) - 1) / (Math.Exp(2 * number) + 1)
End Function
Function score(ByRef input_vector() As Double) As Double
    score = Tanh(2.0)
End Function
End Module
"""

    interpreter = VisualBasicInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Example #23
0
 def _sigmoid_kernel(self, support_vector):
     kernel = self._linear_kernel_with_gama_and_coef(support_vector)
     return ast.TanhExpr(kernel)
Example #24
0
        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),
                ast.NumVal(5),
                ast.NumVal(6),
                ast.NumVal(7),
                ast.NumVal(8),