Beispiel #1
0
    def generate_code(self, node, annotated, engine):
        node = deepcopy(node)
        node_tag = get_node_tag(node)

        children = core.get_children(node["name"], annotated)

        args = dict()
        for child in children:
            # FIXME:
            if "code" not in child:
                continue
            args[child["value"]] = {
                "key": child["value"],
                "value": child["code"]["return_symbol"],
                "ref": child["name"],
                "tag": child["code"]["tag"],
                "str": child["code"]["str"]
            }
        node = cache_fn(node=node,
                        fn=node["value"],
                        args=args,
                        return_symbol=_name_strs_to_names(node["name"]),
                        name="",
                        node_tag=node_tag)
        return node
Beispiel #2
0
    def generate_code(self, node, annotated, engine):
        node = deepcopy(node)
        node_tag = get_node_tag(node)
        args = dict()
        fn = "%s_%s_shape" % (node["ancestor"]["value"],
                              core.get_parent(node["name"], annotated,
                                              "value"))
        return_symbol = _name_strs_to_names(node["name"])

        children = core.get_children(node["name"], annotated)
        for child in children:
            _node_tag = child["code"]["tag"]
            if _node_tag == VALUE:
                continue
            elif _node_tag == IDENTIFIER:
                _value = child["code"]["str"]
            elif _node_tag == FUNCTION:
                _value = child["code"]["return_symbol"]
            args = {
                **args,
                **{
                    child["value"]: {
                        "key": child["value"],
                        "value": _value,
                        "ref": child["name"],
                        "tag": child["code"]["tag"],
                        "str": child["code"]["str"]
                    }
                }
            }
        node = cache_fn(node, fn, args, return_symbol, "", node_tag, True)
        return node
Beispiel #3
0
    def generate_code(self, node, annotated, engine):
        node = deepcopy(node)
        node_tag = get_node_tag(node)

        args = dict()

        param_name = core.get_ancestor_param_node(node,
                                                  annotated,
                                                  field="value")
        shape_name = _name_strs_to_names(
            "%s_%s_shape" % (node["ancestor"]["name"], param_name))
        _name = node["name"]
        return_symbol = _name_strs_to_names(_name)

        args["shape"] = {
            "key": "shape",
            "value": shape_name,
            "ref": None,
            "tag": IDENTIFIER,
            "str": "%s" % shape_name
        }

        args[const.ALEX_ARG_NAME] = {
            "key": const.ALEX_ARG_NAME,
            "value": _name_strs_to_names(_name),
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (_name)
        }

        children = core.get_children(node["name"], annotated)
        for child in children:
            _node_tag = child["code"]["tag"]
            if _node_tag == VALUE:
                continue
            elif _node_tag == IDENTIFIER:
                _value = child["code"]["str"]
            elif _node_tag == FUNCTION:
                _value = child["code"]["return_symbol"]
            args = {
                **args,
                **{
                    child["value"]: {
                        "key": child["value"],
                        "value": _value,
                        "ref": child["name"],
                        "tag": child["code"]["tag"],
                        "str": child["code"]["str"]
                    }
                }
            }
        fn = node["value"]
        node = cache_fn(node, fn, args, return_symbol, _name, node_tag)

        return node
Beispiel #4
0
 def test_get_children(self):
     conv_tree = core.json_to_tree(self.conv, "conv")
     conv_key = list(conv_tree.keys())[-1]
     expected_children = ["dilation", "filters", "padding", "strides"]
     children = core.get_children(conv_key, conv_tree, "label")
     self.assertListEqual(children, expected_children)
Beispiel #5
0
    def generate_code(self, node, annotated, engine):
        node = deepcopy(node)
        node_tag = get_node_tag(node)
        args = dict()
        _name = "%s/%s" % (core.get_parent(node["name"], annotated,
                                           "name"), node["value"])
        return_symbol = _name_strs_to_names(_name)
        node_tag = FUNCTION

        ingredient = core.get_parent(node["name"], annotated)
        if ingredient["meta"]["trainable"] is not None:
            is_trainable = ingredient["meta"]["trainable"]
        else:
            is_trainable = registry.PARAMS[ingredient["value"]][
                node["value"]]["derivative"]
        args["is_trainable"] = {
            "key": "is_trainable",
            "value": is_trainable,
            "ref": None,
            "tag": IDENTIFIER,
            "str": "%s" % str(is_trainable)
        }
        fn = "%s_%s" % (core.get_parent(node["name"], annotated,
                                        "value"), node["value"])

        args[const.ALEX_ARG_NAME] = {
            "key": const.ALEX_ARG_NAME,
            "value": _name_strs_to_names(_name),
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (_name)
        }
        children = core.get_children(node["name"], annotated)
        for child in children:
            if "code" not in child:
                continue
            _node_tag = child["code"]["tag"]
            if _node_tag == VALUE:
                continue
            elif _node_tag == IDENTIFIER:
                _value = child["code"]["str"]
            elif _node_tag == FUNCTION:
                _value = child["code"]["return_symbol"]
            args = {
                **args,
                **{
                    child["value"]: {
                        "key": child["value"],
                        "value": _value,
                        "ref": child["name"],
                        "tag": child["code"]["tag"],
                        "str": child["code"]["str"]
                    }
                }
            }
        node = cache_fn(
            node=node,
            fn=fn,
            args=args,
            return_symbol=return_symbol,
            name=_name,
            node_tag=node_tag,
            inline=False,
            extra="%s['%s'] = %s\n" %
            (const.ALEX_ARG_TRAINABLE_PARAMS, _name, return_symbol))
        return node
Beispiel #6
0
    def generate_code(self, node, annotated, engine):
        children = core.get_children(node["name"], annotated)
        args = dict()
        args[const.ALEX_ARG_INPUTS] = {
            "key":
            const.ALEX_ARG_INPUTS,
            "value":
            node["meta"]["position"]["inputs"],
            "ref":
            None,
            "tag":
            IDENTIFIER,
            "str":
            value_to_kwarg_str("",
                               node["meta"]["position"]["inputs"],
                               literal=True)
        }
        args[const.ALEX_ARG_NAME] = {
            "key": const.ALEX_ARG_NAME,
            "value": _name_strs_to_names(node["name"]),
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (node["name"])
        }
        args["dtype"] = {
            "key": "dtype",
            "value": node["dtype"],
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (node["dtype"])
        }
        return_symbol = _name_strs_to_names(node["name"])

        for child in children:
            _arg = child["code"]["args"]
            for _key in _arg:
                if _arg[_key]["tag"] == IDENTIFIER:
                    args = {**args, **{_key: _arg[_key]}}
            if child["code"]["tag"] != VALUE:
                if child["value"] in registry.ALL_PARAMS:
                    _name = "%s/%s" % (core.get_parent(
                        child["name"], annotated, "name"), child["value"])

                    args = {
                        **args,
                        **{
                            child["value"]: {
                                "key": child["value"],
                                "value": "trainable_params['%s']" % _name,
                                "ref": child["name"],
                                "tag": child["code"]["tag"],
                                "str": child["code"]["str"]
                            }
                        }
                    }
                else:
                    args = {
                        **args,
                        **{
                            child["value"]: {
                                "key": child["value"],
                                "value": child["code"]["return_symbol"],
                                "ref": child["name"],
                                "tag": child["code"]["tag"],
                                "str": child["code"]["str"]
                            }
                        }
                    }
        node = cache_fn(node, node["value"], args, return_symbol, node["name"],
                        FUNCTION)
        return node
Beispiel #7
0
    def generate_code(self, node, annotated, engine):
        children = core.get_children(node["name"], annotated)
        args = dict()
        if node["input_nodes"] is not None:
            inputs = _name_strs_to_names(node["input_nodes"])
            if len(inputs) == 1:
                inputs = inputs[0]
        else:
            inputs = None

        args[const.ALEX_ARG_INPUTS] = {
            "key": const.ALEX_ARG_INPUTS,
            "value": inputs,
            "ref": inputs,
            "tag": IDENTIFIER,
            "str": value_to_kwarg_str("", inputs, literal=False)
        }
        args[const.ALEX_ARG_NAME] = {
            "key": const.ALEX_ARG_NAME,
            "value": _name_strs_to_names(node["name"]),
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (node["name"])
        }
        if node["input_nodes"] is not None:
            input_nodes = annotated[node["input_nodes"][0]]
            args["shape"] = {
                "key": "shape",
                "value": "%s" % str(input_nodes["shape"]),  # input_nodes,
                "ref": None,
                "tag": IDENTIFIER,
                "str": "%s" % str(input_nodes["shape"])
            }
        args["dtype"] = {
            "key": "dtype",
            "value": node["dtype"],
            "ref": None,
            "tag": IDENTIFIER,
            "str": "'%s'" % (node["dtype"])
        }
        return_symbol = _name_strs_to_names(node["name"])

        for child in children:
            if child["value"] in args:
                continue
            # _arg = child["code"]["args"]
            # for _key in _arg:
            #     if _arg[_key]["tag"] == IDENTIFIER:
            #         args = {**args, **{_key: _arg[_key]}}
            if child["code"]["tag"] != VALUE:
                if child["value"] in registry.ALL_PARAMS:
                    _name = "%s/%s" % (core.get_parent(
                        child["name"], annotated, "name"), child["value"])
                    args = {
                        **args,
                        **{
                            child["value"]: {
                                "key": child["value"],
                                "value": "trainable_params['%s']" % _name,
                                "ref": child["name"],
                                "tag": child["code"]["tag"],
                                "str": child["code"]["str"]
                            }
                        }
                    }
                else:
                    _child = {
                        "key": child["value"],
                        "value": child["code"]["return_symbol"],
                        "ref": child["name"],
                        "tag": child["code"]["tag"],
                        "str": child["code"]["str"]
                    }
                    if child["value"] == "decay":
                        if engine == "tf":
                            _child["key"] = "learning_rate"
                            args["learning_rate"] = _child
                    else:
                        args = {**args, **{child["value"]: _child}}
        node = cache_fn(node, node["value"], args, return_symbol, node["name"],
                        FUNCTION)
        return node
Beispiel #8
0
 def get_trainable_params_count(self, node, annotated):
     name = node["name"]
     return sum(
         list(map(lambda x: x["count"], core.get_children(name,
                                                          annotated))))