Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def kernel_ast(sup_vec_value):
return ast.SubroutineExpr(
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)))
def test_single_feature():
estimator = linear_model.LinearRegression()
estimator.coef_ = [1]
estimator.intercept_ = 3
assembler = assemblers.LinearModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.BinNumExpr(
ast.NumVal(3),
ast.BinNumExpr(
ast.FeatureRef(0),
ast.NumVal(1),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
assert utils.cmp_exprs(actual, expected)
ast.NumVal(16.7950001),
ast.CompOpType.GTE),
ast.NumVal(-0.17062147),
ast.NumVal(0.1638484)),
ast.BinNumOpType.ADD),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(27),
ast.NumVal(0.142349988),
ast.CompOpType.GTE),
ast.NumVal(-0.16087772),
ast.NumVal(0.149866998)),
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)
import os
from m2cgen import ast
from m2cgen.interpreters import mixins, utils
from m2cgen.interpreters.go.code_generator import GoCodeGenerator
from m2cgen.interpreters.interpreter import ToCodeInterpreter
class GoInterpreter(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"
power_function_name = "math.Pow"
tanh_function_name = "math.Tanh"
def __init__(self, indent=4, *args, **kwargs):
cg = GoCodeGenerator(indent=indent)
super(GoInterpreter, self).__init__(cg, *args, **kwargs)
def interpret(self, expr):
self._cg.reset_state()
self._reset_reused_expr_cache()
args = [(True, self._feature_array_name)]
def _linear_to_ast(coef, intercept):
feature_weight_mul_ops = []
for index, value in enumerate(coef):
feature_weight_mul_ops.append(
utils.mul(ast.FeatureRef(index), ast.NumVal(value)))
return utils.apply_op_to_expressions(
ast.BinNumOpType.ADD,
ast.NumVal(intercept),
*feature_weight_mul_ops)