def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ import "math" func score(input []float64) []float64 { return softmax([]float64{2.0, 3.0}) } func softmax(x []float64) []float64 { size := len(x) result := make([]float64, size) max := x[0] for _, v := range x { if (v > max) { max = v } } sum := 0.0 for i := 0; i < size; i++ { result[i] = math.Exp(x[i] - max) sum += result[i] } for i := 0; i < size; i++ { result[i] /= sum } return result } """ interpreter = GoInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ using static System.Math; namespace ML { public static class Model { public static double[] Score(double[] input) { return Softmax(new double[2] {2.0, 3.0}); } private static double[] Softmax(double[] x) { int size = x.Length; double[] result = new double[size]; double max = x[0]; for (int i = 1; i < size; ++i) { if (x[i] > max) max = x[i]; } double sum = 0.0; for (int i = 0; i < size; ++i) { result[i] = Exp(x[i] - max); sum += result[i]; } for (int i = 0; i < size; ++i) result[i] /= sum; return result; } } } """ interpreter = CSharpInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ import 'dart:math'; List<double> score(List<double> input) { return softmax([2.0, 3.0]); } List<double> softmax(List<double> x) { int size = x.length; List<double> result = new List<double>.filled(size, 0.0); double maxElem = x.reduce(max); double sum = 0.0; for (int i = 0; i < size; ++i) { result[i] = exp(x[i] - maxElem); sum += result[i]; } for (int i = 0; i < size; ++i) result[i] /= sum; return result; } """ interpreter = DartInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ #include <string.h> void softmax(double *x, int size, double *result) { double max = x[0]; for (int i = 1; i < size; ++i) { if (x[i] > max) max = x[i]; } double sum = 0.0; for (int i = 0; i < size; ++i) { result[i] = exp(x[i] - max); sum += result[i]; } for (int i = 0; i < size; ++i) result[i] /= sum; } void score(double * input, double * output) { double var0[2]; softmax((double[]){2.0, 3.0}, 2, var0); memcpy(output, var0, 2 * sizeof(double)); } """ interpreter = CInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) interpreter = JavaInterpreter() expected_code = """ public class Model { public static double[] score(double[] input) { return softmax(new double[] {2.0, 3.0}); } private static double[] softmax(double[] x) { int size = x.length; double[] result = new double[size]; double max = x[0]; for (int i = 1; i < size; ++i) { if (x[i] > max) max = x[i]; } double sum = 0.0; for (int i = 0; i < size; ++i) { result[i] = Math.exp(x[i] - max); sum += result[i]; } for (int i = 0; i < size; ++i) result[i] /= sum; return result; } }""" utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) interpreter = JavascriptInterpreter() expected_code = """ function score(input) { return softmax([2.0, 3.0]); } function softmax(x) { let size = x.length; let result = new Array(size); let max = x[0]; for (let i = 1; i < size; ++i) { if (x[i] > max) max = x[i]; } let sum = 0.0; for (let i = 0; i < size; ++i) { result[i] = Math.exp(x[i] - max); sum += result[i]; } for (let i = 0; i < size; ++i) result[i] /= sum; return result; } """ utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ <?php function score(array $input) { return softmax(array(2.0, 3.0)); } function softmax(array $x) { $size = count($x); $result = array(); $m = max($x); $sum = 0.0; for ($i = 0; $i < $size; ++$i) { $result[$i] = exp($x[$i] - $m); $sum += $result[$i]; } for ($i = 0; $i < $size; ++$i) { $result[$i] /= $sum; } return $result; } """ interpreter = PhpInterpreter() utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ function Score([double[]] $InputVector) { return Softmax $(@($(2.0), $(3.0))) } function Softmax([double[]] $x) { [int] $size = $x.Length [double[]] $result = @(0) * $size [double] $max = $x[0] for ([int] $i = 1; $i -lt $size; ++$i) { if ($x[$i] -gt $max) { $max = $x[$i] } } [double] $sum = 0.0 for ([int] $i = 0; $i -lt $size; ++$i) { $result[$i] = [math]::Exp($x[$i] - $max) $sum += $result[$i] } for ([int] $i = 0; $i -lt $size; ++$i) { $result[$i] /= $sum; } return $result } """ interpreter = PowershellInterpreter() utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_multi_class(): estimator = lgb.LGBMClassifier(n_estimators=1, random_state=1, max_depth=1) estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3])) assembler = LightGBMModelAssembler(estimator) actual = assembler.assemble() num_expr = ast.NumVal(-1.0986122886681098) expected = ast.SoftmaxExpr([num_expr] * 3) assert utils.cmp_exprs(actual, expected)
def test_multi_class(): estimator = xgb.XGBClassifier(n_estimators=1, random_state=1, max_depth=1) estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3])) assembler = XGBoostModelAssemblerSelector(estimator) actual = assembler.assemble() num_expr = ast.BinNumExpr(ast.NumVal(0.5), ast.NumVal(-7.663454759665456e-09), ast.BinNumOpType.ADD) expected = ast.SoftmaxExpr([num_expr] * 3) assert utils.cmp_exprs(actual, expected)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ let private softmax x = let maxElem = List.reduce max x let exps = List.map (fun i -> exp (i - maxElem)) x let sumExps = List.sum exps List.map (fun i -> i / sumExps) exps let score (input : double list) = softmax ([2.0; 3.0]) """ interpreter = FSharpInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ softmax <- function (x) { m <- max(x) exps <- exp(x - m) s <- sum(exps) return(exps / s) } score <- function(input) { return(softmax(c(2.0, 3.0))) } """ interpreter = RInterpreter() utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ import math def softmax(x): m = max(x) exps = [math.exp(i - m) for i in x] s = sum(exps) for idx, _ in enumerate(exps): exps[idx] /= s return exps def score(input): return softmax([2.0, 3.0]) """ interpreter = PythonInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = r""" module Model where score :: [Double] -> [Double] score input = softmax ([2.0, 3.0]) softmax :: [Double] -> [Double] softmax x = let m = maximum x exps = map (\i -> exp (i - m)) x sumExps = sum exps in map (\i -> i / sumExps) exps """ interpreter = HaskellInterpreter() utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ def score(input) softmax([2.0, 3.0]) end def softmax(x) m = x.max exps = [] s = 0.0 x.each_with_index do |v, i| exps[i] = Math.exp(v - m) s += exps[i] end exps.map { |i| i / s } end """ interpreter = RubyInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ Module Model Function Score(ByRef inputVector() As Double) As Double() Dim var0(1) As Double var0(0) = 2.0 var0(1) = 3.0 Score = Softmax(var0) End Function Function Softmax(ByRef x() As Double) As Double() Dim size As Integer size = UBound(x) - LBound(x) Dim result() As Double ReDim result(size) Dim max As Double max = x(LBound(x)) Dim i As Integer For i = LBound(x) + 1 To UBound(x) If x(i) > max Then max = x(i) End If Next i Dim sum As Double sum = 0.0 For i = LBound(x) To UBound(x) result(i) = Math.Exp(x(i) - max) sum = sum + result(i) Next i For i = LBound(x) To UBound(x) result(i) = result(i) / sum Next i Softmax = result End Function End Module """ interpreter = VisualBasicInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_softmax_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) expected_code = """ fn score(input: Vec<f64>) -> Vec<f64> { softmax(vec![2.0_f64, 3.0_f64]) } fn softmax(x: Vec<f64>) -> Vec<f64> { let size: usize = x.len(); let m: f64 = x.iter().fold(std::f64::MIN, |a, b| a.max(*b)); let mut exps: Vec<f64> = vec![0.0_f64; size]; let mut s: f64 = 0.0_f64; for (i, &v) in x.iter().enumerate() { exps[i] = (v - m).exp(); s += exps[i]; } exps.iter().map(|&i| i / s).collect::<Vec<f64>>() } """ interpreter = RustInterpreter() assert_code_equal(interpreter.interpret(expr), expected_code)
def test_multi_class_best_ntree_limit(): base_score = 0.5 estimator = xgb.XGBClassifier(n_estimators=100, random_state=1, max_depth=1, base_score=base_score) utils.get_classification_model_trainer()(estimator) estimator.get_booster().best_ntree_limit = 1 assembler = XGBoostModelAssemblerSelector(estimator) actual = assembler.assemble() estimator_class1 = ast.BinNumExpr( ast.NumVal(0.5), ast.IfExpr( ast.CompExpr(ast.FeatureRef(2), ast.NumVal(2.450000047683716), ast.CompOpType.GTE), ast.NumVal(-0.21995015442371368), ast.NumVal(0.43024390935897827)), ast.BinNumOpType.ADD) estimator_class2 = ast.BinNumExpr( ast.NumVal(0.5), ast.IfExpr( ast.CompExpr(ast.FeatureRef(2), ast.NumVal(2.450000047683716), ast.CompOpType.GTE), ast.NumVal(0.10324188321828842), ast.NumVal(-0.21512198448181152)), ast.BinNumOpType.ADD) estimator_class3 = ast.BinNumExpr( ast.NumVal(0.5), ast.IfExpr( ast.CompExpr(ast.FeatureRef(3), ast.NumVal(1.6500000953674316), ast.CompOpType.GTE), ast.NumVal(0.4029850661754608), ast.NumVal(-0.19333337247371674)), ast.BinNumOpType.ADD) expected = ast.SoftmaxExpr( [estimator_class1, estimator_class2, estimator_class3]) assert utils.cmp_exprs(actual, expected)
def test_softmax_fallback_expr(): expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)]) class InterpreterWithoutSoftmax(PythonInterpreter): softmax_function_name = NotImplemented def interpret_softmax_expr(self, expr, **kwargs): return super(PythonInterpreter, self).interpret_softmax_expr(expr, **kwargs) interpreter = InterpreterWithoutSoftmax() expected_code = """ import math def score(input): var0 = math.exp(2.0) var1 = math.exp(3.0) var2 = (var0) + (var1) return [(var0) / (var2), (var1) / (var2)] """ assert_code_equal(interpreter.interpret(expr), expected_code)
def _multi_class_convert_output(self, exprs): return ast.SoftmaxExpr(exprs)
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), ast.NumVal(9), 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) def test_count_all_exprs_types():