コード例 #1
0
def Train_dis_BCE(netD, netG, real_loader, epochs=1, out=None):
    best_loss = np.Inf
    for _ in range(epochs):
        for i, real in enumerate(real_loader):
            size = len(real)
            fake = netG.sample(size)
            data = util.Variable(T.cat([fake, util.cuda(real)], dim=0))
            label = util.Variable(T.cat([T.zeros(size, 1), T.ones(size, 1)]))
            netD.optim.zero_grad()
            loss = netD.BCELoss(data, label)
            loss.backward()
            netD.optim.step()
            if i % 10 == 0 and i != 0:
                for param_group in netD.optim.param_groups:
                    param_group['lr'] *= (1 - 0.03)
        if out and loss.data[0] < best_loss:
            T.save(netD.state_dict(), out + ".pkg")
            best_loss = loss.data[0]
    return loss.data[0]
コード例 #2
0
def Train_GAN(netG, netD, netR, sigma=SIGMA):
    seqs = []
    for _ in range(MC):
        seq = netG.sample(BATCH_SIZE)
        seqs.append(seq)
    seqs = T.cat(seqs, dim=0)
    ix = util.unique(seqs)
    seqs = seqs[ix]
    smiles, valids = util.check_smiles(seqs, netG.voc)
    preds = sigma * netR(smiles) + (1 - sigma) * netD(
        util.Variable(seqs)).data.cpu().numpy()[:, 0]
    preds[valids == False] = 0
    preds -= BL
    ds = TensorDataset(seqs, T.Tensor(preds.reshape(-1, 1)))
    loader = DataLoader(ds, batch_size=BATCH_SIZE)
    for seq, pred in loader:
        score, _ = netG.likelihood(seq)
        netG.optim.zero_grad()
        loss = netG.PGLoss(score, seq, pred)
        loss.backward()
        netG.optim.step()
コード例 #3
0
ファイル: location.py プロジェクト: brailcom/wachecker
    def _open(self):
        file_name = self.local_copy()
        charset = self._local_copy_charset()
        stream = open(file_name)
        if charset:
            try:
                input_codec = codecs.getreader(charset)
                stream = input_codec(stream)
            except LookupError:  # unknown charset
                pass
        # Check encoding
        p = document.Parser(location=self)
        p.feed(stream.read())
        charset = util.Variable(charset)

        def check_charset(node):
            http_equiv = node.attr('http-equiv')
            if http_equiv and http_equiv.lower() == 'content-type':
                content = node.attr('content')
                if content:
                    match = re.match('.*; +charset=([-a-z0-9_]+)', content,
                                     re.I)
                    if match:
                        charset.set(match.group(1))
                        raise Exception('charset found')

        try:
            p.document().for_tags(('meta', ), check_charset)
        except:
            pass
        # Open the stream again, with proper encoding
        stream = open(file_name)
        if charset:
            try:
                input_codec = codecs.getreader(charset.get())
                stream = input_codec(stream)
            except LookupError:  # unknown charset
                pass
        return stream
コード例 #4
0
    def assign_stylesheets(self):
        if self._stylesheets_assigned:
            errors = None
        else:
            errors = []
            stylesheet = util.Variable(css.Stylesheet(()))
            default_stylesheet_type = util.Variable(None)
            current_location = util.Variable(self._location)

            def add_stylesheet(s, location):
                if isinstance(s, css.Stylesheet):
                    stylesheet.set(stylesheet.get().merge(s))
                else:
                    errors.append(
                        Stylesheet_Parse_Error(
                            description="Parse error in stylesheet",
                            data=location))

            # Find default stylesheet type
            def let(type=current_location.get().header('Content-Style-Type')):
                if type:
                    default_stylesheet_type.set(type)

            let()
            if not default_stylesheet_type:

                def check_stylesheet_type(node):
                    if node.attr('http-equiv') and node.attr(
                            'http-equiv').lower() == 'content-style-type':
                        default_stylesheet_type.set(node.attr('content'))

                self.for_tags(('meta', ), check_stylesheet_type)
            if default_stylesheet_type:
                if default_stylesheet_type.get() != 'text/css':
                    errors.append(
                        Unknown_Stylesheet_Type_Error(
                            description=
                            "The specified page stylesheet is of unknown type",
                            data=default_stylesheet_type.get()))
            # Find base location of the document
            def set_base_location(node):
                import location
                if node.attr('href'):
                    current_location.set(location.Location(node.attr('href')))

            self.for_tags(('base', ), set_base_location)

            # Load stylesheets
            def load_external_stylesheet(node):
                if node.attr('rel') == 'stylesheet':
                    type = node.attr('type')
                    href = node.attr('href')
                    if type and type != 'text/css':
                        errors.append(
                            Unknown_Stylesheet_Type_Error(
                                node=node,
                                description="Unknown stylesheet type",
                                data=type))
                    elif href:
                        loc = current_location.get().make_location(href)
                        add_stylesheet(css.parse(loc), loc)

            self.for_tags(('link', ), load_external_stylesheet)

            def load_inline_stylesheet(node):
                type = node.attr('type')
                if type and type != 'text/css':
                    errors.append(
                        Unknown_Stylesheet_Type_Error(
                            node=node,
                            description="Unknown stylesheet type",
                            data=type))
                else:
                    stream = StringIO.StringIO(node.text())

                    def let(loc=current_location.get()):
                        add_stylesheet(css.parse_stream(stream, loc), loc)

                    let()

            self.for_tags(('style', ), load_inline_stylesheet)

            # Assign styles to nodes
            def assign_properties(node):
                properties = stylesheet.get().node_properties(node, self)
                style = node.attr('style')
                if style:
                    if default_stylesheet_type.get() == 'text/css':
                        stream = StringIO.StringIO(style)
                        declarations = css.parse_stream(
                            stream, grammar=css.properties_grammar)
                        if util.is_sequence(declarations):
                            for d in declarations:
                                properties[d.name] = d.value
                        else:
                            errors.append(
                                Stylesheet_Parse_Error(
                                    description="Parse error in stylesheet",
                                    data='inline'))
                    elif not default_stylesheet_type:
                        errors.append(
                            No_Stylesheet_Type_Error(
                                node=node,
                                description=
                                "Style without stylesheet type defined",
                                data=style))
                    else:
                        errors.append(
                            Unknown_Stylesheet_Type_Error(
                                node=node,
                                description="Unknown stylesheet type",
                                data=style))
                node.set_style(properties)

            self.for_all_nodes(assign_properties)
            self._stylesheets_assigned = True
        return errors
コード例 #5
0
    def _calculate(self, target, delta_t=0):
        t = self.current_step + delta_t
        # print("calculating", target, "at t =", t)  # TODO
        # given as input to the program
        if target in self.parameters["simulation"]["inputs"]:
            if target.is_indexed:
                return self.sim_data[target][int(target.index)]
            else:
                return self.input_data[target]
        # already calculated
        if self.sim_timeline and target.name in self.sim_data.dtype.names:
            idx = 0
            try:  # pythonic way? better ask for forgiveness than permission?
                try:
                    idx = target.delay + t
                except TypeError:  # has no delay field => absolute index
                    assert target.is_absolutely_indexed
                    idx = int(target.index)
                value = self.sim_data[target.name][idx]
                if not math.isnan(value):
                    # print("for", target, "found", value)  # TODO
                    return value
            except ValueError:  # is sliced!
                assert target.is_sliced
                stuff = [int(s) for s in target.index.split(':') if s]
                # splat operator (LOL)
                value = self.sim_data[target.name][slice(*stuff)]
                if not any(math.isnan(v) for v in value):
                    # print("for", target, "found", value)  # TODO
                    return value
                else:
                    ValueError("nan found for ", target, "in:", value)

        # if to be calculated
        if target in self.functions:
            # collect the inputs required
            required_variables = self.functions[target].inputs
            for v in required_variables:
                v.value = self._calculate(v, delta_t)
                if (v.is_indexed and not v.is_sliced
                        and v.name in self.sim_data.dtype.names):
                    try:
                        self.sim_data[v.name][t + v.delay] = v.value
                    except TypeError as err:
                        if err.args[0][:24] != "unsupported operand type":
                            raise err
                        assert v.is_absolutely_indexed
                        self.sim_data[v.name][t] = v.value
            # then calculate
            return self.functions[target].calculate()
        # if is relatively indexed but the current value is in the functions
        if target.is_relatively_indexed:
            actualized_target = target.actualize(t)
            if actualized_target in self.functions:
                return self._calculate(actualized_target, delta_t)
        # if we reach this point, there's an issue in the yaml
        # the issue might be: we want to calculate x[t+3] but the function
        # saved is for x[t] (and ofc we have everything to calculate x[t])
            s = 0
            for i in list(range(1, 10)):  # -1 1 -2 2 -3 3 -4 4 -5
                if i % 2 == 0:
                    s += i
                else:
                    s -= i
                delay = target.delay + s
                if delay > 0:
                    delay = 't+' + str(delay)
                elif delay == 0:
                    delay = 't'
                else:
                    delay = 't' + str(delay)
                alternative_target = ut.Variable(target.name + '[' + delay +
                                                 ']')
                # print("alternative_target:", alternative_target)  # TODO
                if alternative_target in self.functions:
                    if alternative_target.delay == 0:
                        absolute_target = ut.Variable(target.name + '[0]')
                        if absolute_target in self.functions:
                            return self._calculate(absolute_target, delta_t)
                    diff_t = target.delay - alternative_target.delay + delta_t
                    return self._calculate(alternative_target, diff_t)
        # it's the t
        if target == ut.Variable("t"):
            return t
        # eventually, give up
        raise ValueError("Variable", target, "is not evaluable.")
コード例 #6
0
    def __init__(self, params=None):
        if not params:
            raise ValueError("No parameters given to Model constructor")

        if params["sources"]:
            import yaml
            data_cache = []
            for src in params["sources"]:
                with open(src, 'r') as f:
                    data_cache.append(yaml.load(f))
            params["sources"] = data_cache

        self.parameters = dict()
        self.parameters["simulation"] = dict()
        self.parameters["simulation"]["target"] = list()
        self.parameters["simulation"]["inputs"] = list()

        self.variable_names = dict()  # maps deindexified name with indexed one
        self.functions = dict()
        for source in params["sources"]:
            # check for field existence and emptiness
            if "functions" in source and source["functions"]:
                for function in source["functions"]:
                    item = ut.Function(function)
                    self.functions.update({y: item for y in item.outputs})
                    self.variable_names.update(
                        {y: y.name
                         for y in item.inputs + item.outputs})

            # take care of simulation details
            if "simulation" in source and "target" in source["simulation"]:
                items = [
                    ut.Variable(item)
                    for item in source["simulation"]["target"]
                    if ut.Variable.is_it(item)
                ]
                self.parameters["simulation"]["target"] += \
                    [item for item in items if not item.is_relatively_indexed]

            if "simulation" in source and "inputs" in source["simulation"]:
                items = [
                    ut.Variable(item)
                    for item in source["simulation"]["inputs"]
                    if ut.Variable.is_it(item)
                ]
                assert not any(item.is_relatively_indexed for item in items)
                self.parameters["simulation"]["inputs"] += items
                self.input_data = {
                    item: 0
                    for item in items if not item.is_indexed
                }

            # check for any logging requirement
            if "logging" in source:
                # create space, if it's first loggin
                if "logging" not in self.parameters:
                    self.parameters["logging"] = dict()
                # then read each logfile that has to be produced
                [
                    self.parameters["logging"].update({
                        filename: [
                            ut.Variable(item)
                            for item in source["logging"][filename]
                        ]
                    }) for filename in source["logging"]
                    if source["logging"][filename]
                    and len(source["logging"][filename]) > 0
                ]

            # check for any external source
            if "external" in source:
                # create space, if it's first external source found
                if "external" not in self.parameters:
                    self.parameters["external"] = list()
                # then read each logfile to produce
                self.parameters["external"] += source["external"]

        # consistency check
        if not self.parameters["simulation"]["target"]:
            raise ut.YAMLError("No target found in given YAML files")

        # simulation function sequence and database, if needed
        self._build_timeline()
        if self.sim_timeline:  # if is an instance of a dynamic model
            self._build_simulation_helpers()
            helper = {h.name for h in self.sim_step_todo}
            if "logging" in self.parameters and self.parameters["logging"]:
                for log_file in self.parameters["logging"]:
                    helper |= {
                        h.name
                        for h in self.parameters["logging"][log_file]
                    }
            sim_types = {
                'names': list(helper),
                'formats': ['float'] * len(helper)
            }
            self.sim_data = numpy.full(len(self.sim_timeline),
                                       numpy.nan,
                                       dtype=sim_types)

        # load external source if any
        if "external" in self.parameters:
            for source in self.parameters["external"]:
                if not self._load_source(source):
                    self.parameters["external"].remove(source)

        # last but not least, initialize internal clock
        self.current_step = 0