Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_multi_class_rbf_kernel():
estimator = svm.SVC(kernel="rbf", random_state=1, gamma=2.0)
estimator.fit([[1], [2], [3]], [1, 2, 3])
assembler = assemblers.SVMModelAssembler(estimator)
actual = assembler.assemble()
kernels = [
_rbf_kernel_ast(estimator, float(i), to_reuse=True)
for i in range(1, 4)
]
expected = ast.VectorVal([
ast.BinNumExpr(
ast.BinNumExpr(
ast.NumVal(0.0),
ast.BinNumExpr(
kernels[1],
ast.NumVal(-1.0),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD),
ast.BinNumExpr(
kernels[0],
ast.NumVal(1.0),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD),
ast.BinNumExpr(
ast.BinNumExpr(
ast.NumVal(0.0),
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
expected_code = """
Module Model
Function addVectors(ByRef v1() As Double, ByRef v2() As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) + v2(i)
Next i
ast.BinNumOpType.ADD),
ast.SubroutineExpr(
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)
ast.SubroutineExpr(
ast.BinNumExpr(
ast.NumVal(0.0),
ast.NumVal(-1.0986122886681098),
ast.BinNumOpType.ADD)),
to_reuse=True)
exponent_sum = ast.BinNumExpr(
ast.BinNumExpr(exponent, exponent, ast.BinNumOpType.ADD),
exponent,
ast.BinNumOpType.ADD,
to_reuse=True)
softmax = ast.BinNumExpr(exponent, exponent_sum, ast.BinNumOpType.DIV)
expected = ast.VectorVal([softmax] * 3)
assert utils.cmp_exprs(actual, expected)
def test_multi_output():
expr = ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.NumVal(1),
ast.NumVal(1),
ast.CompOpType.EQ),
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])))
expected_code = """
def score(input):
if (1) == (1):
var0 = [1, 2]
else:
var0 = [3, 4]
return var0
"""
interpreter = interpreters.PythonInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_bin_vector_expr():
expr = ast.BinVectorExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)]),
ast.BinNumOpType.MUL)
interpreter = interpreters.PythonInterpreter()
expected_code = """
import numpy as np
def score(input):
return (np.asarray([1, 2])) * (np.asarray([3, 4]))
"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
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.JavaInterpreter()
expected_code = """
public class Model {
public static double[] score(double[] input) {
return mulVectorNumber(new double[] {1, 2}, 1);
}
public static double[] addVectors(double[] v1, double[] v2) {
double[] result = new double[v1.length];
for (int i = 0; i < v1.length; i++) {
result[i] = v1[i] + v2[i];
}
def test_bin_vector_expr():
expr = ast.BinVectorExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)]),
ast.BinNumOpType.ADD)
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];
def test_multi_output():
expr = ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.NumVal(1),
ast.NumVal(1),
ast.CompOpType.EQ),
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])))
expected_code = """
func score(input []float64) []float64 {
var var0 []float64
if (1) == (1) {
var0 = []float64{1, 2}
} else {
var0 = []float64{3, 4}
}
return var0
}"""
interpreter = interpreters.GoInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def _build_ast(self):
coef = utils.to_2d_array(self.model.coef_)
intercept = utils.to_1d_array(self.model.intercept_)
if coef.shape[0] == 1:
return _linear_to_ast(coef[0], intercept[0])
exprs = []
for idx in range(coef.shape[0]):
exprs.append(ast.SubroutineExpr(
_linear_to_ast(coef[idx], intercept[idx])))
return ast.VectorVal(exprs)