Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ast.CompOpType.LTE),
ast.NumVal(1.0),
ast.NumVal(2.0))),
ast.NumVal(0.5),
ast.BinNumOpType.MUL),
ast.BinNumExpr(
ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.NumVal(2.0),
ast.NumVal(3.0))),
ast.NumVal(0.5),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
assert utils.cmp_exprs(actual, expected)
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.CInterpreter()
expected_code = """
#include
void add_vectors(double *v1, double *v2, int size, double *result) {
for(int i = 0; i < size; ++i)
result[i] = v1[i] + v2[i];
}
void mul_vector_number(double *v1, double num, int size, double *result) {
for(int i = 0; i < size; ++i)
result[i] = v1[i] * num;
}
void score(double * input, double * output) {
double var0[2];
mul_vector_number((double[]){1, 2}, 1, 2, var0);
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.GoInterpreter()
expected_code = """
func addVectors(v1, v2 []float64) []float64 {
result := make([]float64, len(v1))
for i := 0; i < len(v1); i++ {
result[i] = v1[i] + v2[i]
}
return result
}
func mulVectorNumber(v1 []float64, num float64) []float64 {
result := make([]float64, len(v1))
for i := 0; i < len(v1); i++ {
result[i] = v1[i] * num
}
ast.BinNumOpType.MUL),
ast.BinVectorNumExpr(
ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.VectorVal([
ast.NumVal(1.0),
ast.NumVal(0.0)]),
ast.VectorVal([
ast.NumVal(0.0),
ast.NumVal(1.0)]))),
ast.NumVal(0.5),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
assert utils.cmp_exprs(actual, expected)
def test_bin_num_expr():
expr = ast.BinNumExpr(
ast.BinNumExpr(
ast.FeatureRef(0), ast.NumVal(-2), ast.BinNumOpType.DIV),
ast.NumVal(2),
ast.BinNumOpType.MUL)
interpreter = interpreters.GoInterpreter()
expected_code = """
func score(input []float64) float64 {
return ((input[0]) / (-2)) * (2)
}"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def _create_expected_single_output_ast(svm_model, kernels_ast):
return ast.BinNumExpr(
ast.BinNumExpr(
ast.NumVal(svm_model.intercept_[0]),
ast.BinNumExpr(
kernels_ast[0],
ast.NumVal(svm_model.dual_coef_[0][0]),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD),
ast.BinNumExpr(
kernels_ast[1],
ast.NumVal(svm_model.dual_coef_[0][1]),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
from m2cgen.interpreters import mixins
from m2cgen.interpreters import utils
from m2cgen.interpreters.interpreter import ToCodeInterpreter
from m2cgen.interpreters.java.code_generator import JavaCodeGenerator
class JavaInterpreter(ToCodeInterpreter,
mixins.LinearAlgebraMixin,
mixins.SubroutinesAsFunctionsMixin):
supported_bin_vector_ops = {
ast.BinNumOpType.ADD: "addVectors",
}
supported_bin_vector_num_ops = {
ast.BinNumOpType.MUL: "mulVectorNumber",
}
exponent_function_name = "Math.exp"
power_function_name = "Math.pow"
tanh_function_name = "Math.tanh"
def __init__(self, package_name=None, class_name="Model", indent=4,
*args, **kwargs):
self.package_name = package_name
self.class_name = class_name
self.indent = indent
# We don't provide any code generator as for each subroutine we will
# create a new one and concatenate their results into top_cg created
# in .interpret() method.
super().__init__(None, *args, **kwargs)
def mul(l, r, to_reuse=False):
return ast.BinNumExpr(l, r, ast.BinNumOpType.MUL, to_reuse=to_reuse)
import os
from m2cgen import ast
from m2cgen.interpreters import mixins, utils
from m2cgen.interpreters.interpreter import ToCodeInterpreter
from m2cgen.interpreters.visual_basic.code_generator \
import VisualBasicCodeGenerator
class VisualBasicInterpreter(ToCodeInterpreter, mixins.LinearAlgebraMixin):
supported_bin_vector_ops = {
ast.BinNumOpType.ADD: "addVectors",
}
supported_bin_vector_num_ops = {
ast.BinNumOpType.MUL: "mulVectorNumber",
}
exponent_function_name = "Math.Exp"
tanh_function_name = "Tanh"
with_tanh_expr = False
def __init__(self, module_name="Model", indent=4, *args, **kwargs):
self.module_name = module_name
cg = VisualBasicCodeGenerator(indent=indent)
kwargs["feature_array_name"] = "input_vector"
super(VisualBasicInterpreter, self).__init__(cg, *args, **kwargs)
def interpret(self, expr):
self._cg.reset_state()
self._reset_reused_expr_cache()
from m2cgen import ast
from m2cgen.interpreters import utils, mixins
from m2cgen.interpreters.c.code_generator import CCodeGenerator
from m2cgen.interpreters.interpreter import ToCodeInterpreter
class CInterpreter(ToCodeInterpreter,
mixins.LinearAlgebraMixin):
supported_bin_vector_ops = {
ast.BinNumOpType.ADD: "add_vectors",
}
supported_bin_vector_num_ops = {
ast.BinNumOpType.MUL: "mul_vector_number",
}
exponent_function_name = "exp"
power_function_name = "pow"
tanh_function_name = "tanh"
def __init__(self, indent=4, *args, **kwargs):
cg = CCodeGenerator(indent=indent)
super(CInterpreter, self).__init__(cg, *args, **kwargs)
def interpret(self, expr):
self._cg.reset_state()
self._reset_reused_expr_cache()
args = [(True, self._feature_array_name)]