Пример #1
0
def log1p(expr):
    # Use trick to compute log1p for small values more accurate
    # https://www.johndcook.com/blog/2012/07/25/trick-for-computing-log1x/
    expr = ast.IdExpr(expr, to_reuse=True)
    expr1p = utils.add(ast.NumVal(1.0), expr, to_reuse=True)
    expr1pm1 = utils.sub(expr1p, ast.NumVal(1.0), to_reuse=True)
    return ast.IfExpr(
        utils.eq(expr1pm1, ast.NumVal(0.0)), expr,
        utils.div(utils.mul(expr, ast.LogExpr(expr1p)), expr1pm1))
Пример #2
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = FSharpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #3
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = RustInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #4
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = RInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #5
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

    expected_code = """
import 'dart:math';
double score(List<double> input) {
    return log(2.0);
}
"""
    interpreter = DartInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #6
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = PowershellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #7
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = JavascriptInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #8
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = RubyInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #9
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = PythonInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #10
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = HaskellInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #11
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

    interpreter = GoInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #12
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = PhpInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #13
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

    interpreter = interpreters.CInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #14
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

    interpreter = JavaInterpreter()

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

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #15
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

    expected_code = """
Module Model
Function Score(ByRef inputVector() As Double) As Double
    Score = Math.Log(2.0)
End Function
End Module
"""

    interpreter = VisualBasicInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #16
0
def test_log_expr():
    expr = ast.LogExpr(ast.NumVal(2.0))

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

    interpreter = CSharpInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Пример #17
0
        exclude_list={ast.BinExpr, ast.NumVal}
    ) == 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),