示例#1
0
 def init_from_str(self):
     """Initialize optimizer from the `param` string"""
     r = r"([a-zA-Z]*)=([^,)]*)"
     opt_str = self.param.lower()
     kwargs = {i: _eval(j) for i, j in re.findall(r, opt_str)}
     if "sgd" in opt_str:
         optimizer = SGD(**kwargs)
     elif "adagrad" in opt_str:
         optimizer = AdaGrad(**kwargs)
     elif "rmsprop" in opt_str:
         optimizer = RMSProp(**kwargs)
     elif "adam" in opt_str:
         optimizer = Adam(**kwargs)
     else:
         raise NotImplementedError("{}".format(opt_str))
     return optimizer
示例#2
0
    def init_from_str(self):
        """Initialize scheduler from the param string"""
        r = r"([a-zA-Z]*)=([^,)]*)"
        sch_str = self.param.lower()
        kwargs = {i: _eval(j) for i, j in re.findall(r, sch_str)}

        if "constant" in sch_str:
            scheduler = ConstantScheduler(**kwargs)
        elif "exponential" in sch_str:
            scheduler = ExponentialScheduler(**kwargs)
        elif "noam" in sch_str:
            scheduler = NoamScheduler(**kwargs)
        elif "king" in sch_str:
            scheduler = KingScheduler(**kwargs)
        else:
            raise NotImplementedError("{}".format(sch_str))
        return scheduler
示例#3
0
    def data(self, data):
        """"""
        attributes = data.get('attributes') or {}
        default_vertex_attributes = data.get('default_vertex_attributes') or {}
        default_edge_attributes = data.get('default_edge_attributes') or {}
        vertex = data.get('vertex') or {}
        cell = data.get('cell') or {}
        halfface = data.get('halfface') or {}
        plane = data.get('plane') or {}
        edge = data.get('edge') or {}
        max_int_key = data.get('max_int_key', -1)
        max_int_fkey = data.get('max_int_fkey', -1)
        max_int_ckey = data.get('max_int_ckey', -1)

        if not vertex or not edge or not plane or not halfface or not cell:
            return

        self.clear()

        self.attributes.update(attributes)
        self.default_vertex_attributes.update(default_vertex_attributes)
        self.default_edge_attributes.update(default_edge_attributes)

        for _k, attr in vertex.iteritems():
            k = _eval(_k)
            self.vertex[k] = self.default_vertex_attributes.copy()
            if attr:
                self.vertex[k].update(attr)
            self.plane[k] = {}
            self.edge[k] = {}

        for _u, nbrs in edge.iteritems():
            nbrs = nbrs or {}
            u = _eval(_u)
            for _v, attr in nbrs.iteritems():
                v = _eval(_v)
                self.edge[u][v] = self.default_edge_attributes.copy()
                if attr:
                    self.edge[u][v].update(attr)

        for _f in halfface:
            f = _eval(_f)
            self.halfface[f] = {}
            for _u, _v in halfface[_f].iteritems():
                u = _eval(_u)
                v = _eval(_v)
                self.halfface[f][u] = v

        for _u in plane:
            u = _eval(_u)
            for _v in plane[_u]:
                v = _eval(_v)
                if v not in self.plane[u]:
                    self.plane[u][v] = {}
                for _w, _c in plane[_u][_v].iteritems():
                    w = _eval(_w)
                    c = _eval(_c)
                    self.plane[u][v][w] = c

        for _c in cell:
            c = _eval(_c)
            self.cell[c] = {}
            for _u in cell[_c]:
                u = _eval(_u)
                if u not in self.cell[c]:
                    self.cell[c][u] = {}
                for _v, _f in cell[_c][_u].iteritems():
                    v = _eval(_v)
                    f = _eval(_f)
                    self.cell[c][u][v] = f

        self._max_int_key = max_int_key
        self._max_int_fkey = max_int_fkey
        self._max_int_ckey = max_int_ckey