def addMAX_P(self):
        operator = Operator('MAX_P', 1)

        def func(node):
            node.maxPooling = True
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addDOUB(self):
        operator = Operator('DOUB', 1)

        def func(node):
            node.multiply_neuron_count(2)
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addSOFTPLUS(self):
        operator = Operator('SOFTPLUS', 1)

        def func(node):
            node.set_activation('softplus')
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addTANH(self):
        operator = Operator('TANH', 1)

        def func(node):
            node.set_activation('tanh')
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addHSIGMOID(self):
        operator = Operator('HSIGMOID', 1)

        def func(node):
            node.set_activation('hard_sigmoid')
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addSOFTSIGN(self):
        operator = Operator('SOFTSIGN', 1)

        def func(node):
            node.set_activation('softsign')
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addRELU(self):
        operator = Operator('RELU', 1)

        def func(node):
            node.set_activation('relu')
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addFILTER_INC(self):
        operator = Operator('FILTER_INC', 1)

        def func(node):
            node.filter_count *= 2
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addHALF(self):
        operator = Operator('HALF', 1)

        def func(node):
            node.divide_neuron_count(2)
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addPOOL_SIZE_INC(self):
        operator = Operator('POOL_SIZE_INC', 1)

        def func(node):
            if node.pool_size < constants.IMG_DIMENSION:  # prevent out of bounds
                node.pool_size += 1
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addPOOL_SIZE_DEC(self):
        operator = Operator('POOL_SIZE_DEC', 1)

        def func(node):
            if node.pool_size > constants.POOL_SIZE_MIN:  # make sure it always stays at least POOL_SIZE_MIN
                node.pool_size -= 1
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addKER_SIZE_DEC(self):
        operator = Operator('KER_SIZE_DEC', 1)

        def func(node):
            if node.kernel_size > constants.KERNEL_SIZE_MIN:  # make sure kernel stays at least KERNEL_SIZE_MIN
                node.kernel_size -= 2  # make sure it always stays uneven (e.g. 3, 5, 7 etc.) for convolution to work
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addFILTER_DEC(self):
        operator = Operator('FILTER_DEC', 1)

        def func(node):
            if node.filter_count > constants.FILTER_COUNT_MIN:  # make sure that it stays at least FILTER_COUNT_MIN
                node.filter_count = int(node.filter_count / 2)
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addDROP_INC(self):
        operator = Operator('DROP_INC', 1)

        def func(node):
            if node.dropout < 0.9:  # make sure that it never reaches 100%
                node.dropout += 0.1
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addDROP_DEC(self):
        operator = Operator('DROP_DEC', 1)

        def func(node):
            if node.dropout > 0.1:  # make sure that it never reaches 0%
                node.dropout -= 0.1
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addPAR(self):
        operator = Operator('PAR', 2)

        def func(node, index):
            next = Phenotype(index, node.neurons)
            next.copy_inputs(node)
            next.copy_outputs(node)
            return node, next  # return LEFT, RIGHT

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addKER_SIZE_INC(self):
        operator = Operator('KER_SIZE_INC', 1)

        def func(node):
            if node.kernel_size < constants.IMG_DIMENSION:
                node.kernel_size += 2  # make sure kernel always stays uneven (e.g. 3, 5, 7 etc.) for convolution to work
                if node.kernel_size > constants.IMG_DIMENSION:  # addition could lead to out of bounds
                    node.kernel_size -= 2
            return node

        operator.set_pheno_func(func)
        self.operators.append(operator)
    def addSEQ(self):
        operator = Operator('SEQ', 2)

        def func(node, index):
            next = Phenotype(index, node.neurons)
            next.add_input(node)
            next.copy_outputs(node)
            for n in node.outputs:
                n.inputs.remove(node)
            node.outputs = []
            node.add_output(next)
            return node, next  # return LEFT, RIGHT

        operator.set_pheno_func(func)
        self.operators.append(operator)
Example #19
0
def parse_rule(sentence, scope, tnorm, snorm, cnorm):
    """
    Parse a if-then string rule with variables and adjectives in scope dictionary
    :param sentence: if-then rule with (mamdani/sugeno/tsukamoto) format
    :param scope: scope dictionary use for parsing task
    :param tnorm: norm use for and operator
    :param snorm: norm use for or operator
    :param cnorm: norm use for not operator
    """
    assert isinstance(sentence, str), 'sentence must be a string'

    from operators.operator import Operator

    if ':' in sentence:
        system_type, sentence, *empty_list = sentence.split(':')
    else:
        system_type = None

    if_, then_ = sentence.split(THEN)
    index = if_.index(IF)
    if_ = if_[index + len(IF):]
    antecedent = Operator.build_tree(if_, scope, tnorm, snorm, cnorm)

    # or (EQUAL in then_ and not WITH in then_)
    if system_type == 's' or (not IS in then_):  # Sugeno Consequent
        from rules.srule import SRule
        index = then_.index(EQUAL)
        consequent = then_[index + 1:]
        if WITH in consequent:
            consequent, weight, *rest = consequent.split(WITH)
            weight = float(weight.strip())
        else:
            weight = 1
        consequent = consequent.strip()
        rule = SRule(antecedent, consequent, weight)
    else:  # Mamdani Consequent... predicate
        then_ = then_.replace(',', ' , ')
        then_ = then_.rstrip(';')
        consequent, weight = _conclusion(then_.split(), scope)
        if system_type == 't':
            from rules.trule import TRule
            return TRule(antecedent, consequent[0], weight)
        from rules.mrule import MRule
        rule = MRule(antecedent, consequent, weight)

    return rule
Example #20
0
def parse_rule(sentence, scope, tnorm, snorm, cnorm):
    """
    Parse a if-then string rule with variables and adjectives in scope dictionary
    :param sentence: if-then rule with (mamdani/sugeno/tsukamoto) format
    :param scope: scope dictionary use for parsing task
    :param tnorm: norm use for and operator
    :param snorm: norm use for or operator
    :param cnorm: norm use for not operator
    """
    assert isinstance(sentence, str), 'sentence must be a string'

    from operators.operator import Operator

    if ':' in sentence:
        system_type , sentence, *empty_list = sentence.split(':')
    else:
        system_type = None

    if_, then_ = sentence.split(THEN)
    index = if_.index(IF)
    if_ = if_[index+len(IF):]
    antecedent = Operator.build_tree(if_, scope, tnorm, snorm, cnorm)

    # or (EQUAL in then_ and not WITH in then_)
    if system_type == 's' or (not IS in then_): # Sugeno Consequent
        from rules.srule import SRule
        index = then_.index(EQUAL)
        consequent = then_[index+1:]
        if WITH in consequent:
            consequent, weight, *rest = consequent.split(WITH)
            weight = float(weight.strip())
        else:
            weight = 1
        consequent = consequent.strip()
        rule = SRule(antecedent, consequent, weight)
    else:  # Mamdani Consequent... predicate
        then_ = then_.replace(',', ' , ')
        then_ = then_.rstrip(';')
        consequent, weight = _conclusion(then_.split(), scope)
        if system_type == 't':
            from rules.trule import TRule
            return TRule(antecedent, consequent[0], weight)
        from rules.mrule import MRule
        rule = MRule(antecedent, consequent, weight)

    return rule
Example #21
0
    def render_translation(self, sentence):
        res = self.key

        assert len(self._transforms) == len(sentence._transforms)
        from operators.operator import Operator

        for t, t_map in zip(reversed(self._transforms),
                            reversed(sentence._transforms)):
            assert t['operator'] == t_map['operator'] and t[
                'parameters'] == t_map['parameters']

            if t['operator'] == 'raw':
                break

            op = Operator.from_name(t['operator'], t['parameters'])
            res = op.postprocess(res, t['mapping'], self.language,
                                 t_map['mapping'])

        return res
Example #22
0
 def __init__(self):
     self.operators = Operator.get_registered_operators()