def __init__( self, vectorizer: Union("Vectorizer", Count, TfIdf), decomposer: Union("Decomposer", NoDec, SVD), classifier: Union("Classifier", LR, SVM, DT), ): self.vectorizer = vectorizer self.decomposer = decomposer self.classifier = classifier super().__init__( [("vect", vectorizer), ("decomp", decomposer), ("class", classifier),] )
def generate_cfg(cls, grammar, head): symbol = head or Symbol(cls.__name__) compatible = [] for _, other_cls in grammar.namespace.items(): if cls.is_compatible(other_cls): compatible.append(other_cls) if not compatible: raise InterfaceIncompatibleError(cls) return Union(symbol.name, *compatible).generate_cfg(grammar, symbol)
def generate_cfg(cls, grammar, head): symbol = head or Symbol(cls.__name__) compatible = [] for _, other_cls in grammar.namespace.items(): if cls.is_compatible(other_cls): compatible.append(other_cls) if not compatible: raise ValueError( f"Cannot find any suitable implementation of algorithms with inputs: {inputs} and output: {output}" ) return Union(symbol.name, *compatible).generate_cfg(grammar, symbol)
def generate_cfg(cls, grammar, head): symbol = head or Symbol(cls.__name__) compatible = [] for _, other_cls in grammar.namespace.items(): if cls.is_compatible(other_cls): compatible.append(other_cls) if not compatible: raise ValueError( "Cannot find compatible implementations for interface %r" % cls ) return Union(symbol.name, *compatible).generate_cfg(grammar, symbol)
return left + right @nice_repr class Mult(Operator): def operate(self, left, right): return left * right @nice_repr class Concat(Operator): def operate(self, left, right): return int(str(left) + str(right)) Expr = Union("Expr", Number, Add, Mult, Concat) grammar = generate_cfg(Expr) print(grammar) # Our grammar is composed of addition, multiplication and concatenation operators. # Here are some possible examples: for i in range(100): try: solution = grammar.sample() print(solution) except ValueError: continue
def __init__(self, criterion: Categorical("gini", "entropy")): super().__init__(criterion=criterion) class NB(GaussianNB): def __init__(self, var_smoothing: Continuous(1e-10, 0.1)): super().__init__(var_smoothing=var_smoothing) # Next, we use AutoGOAL to construct a grammar for the union of the possible instances # of each of these clases. from autogoal.grammar import Union from autogoal.grammar import generate_cfg grammar = generate_cfg(Union("Classifier", LR, SVM, NB, DT)) # !!! note # The method [`generate_cfg`](/api/grammar/#generate_cfg) works not only with annotated classes # but also with plain methods, or anything that has a `__call__` and suitable annotations. # This grammar defines all possible ways to obtain a `Classifier`, which is basically # by instantiating one of the classes we gave it with a suitable value for each parameter. # We can test it by generating a few of them. print(grammar) # ```bash # <Classifier> := <LR> | <SVM> | <NB> | <DT> # <LR> := LR (penalty=<LR_penalty>, C=<LR_C>) # <LR_penalty> := categorical (options=['l1', 'l2'])
@nice_repr class A: pass @nice_repr class B: pass @nice_repr class C: pass D = Union("D", B, C) @nice_repr class Main: def __init__(self, xs: Subset("xs", A, D)): self.xs = xs grammar = generate_cfg(Main) print(grammar) print(grammar.sample())