Ejemplo n.º 1
0
Archivo: core.py Proyecto: jdmcbr/logpy
def bind(stream, goal):
    """ Bind a goal to a stream

    inputs:
        stream - sequence of substitutions (dicts)
        goal   - function :: substitution -> stream

    """
    return unique_dict(interleave(it.imap(goaleval(goal), stream)))
Ejemplo n.º 2
0
 def anygoal(s):
     reifiedgoals = (reify(goal, s) for goal in goals)
     def f(goals):
         for goal in goals:
             try:
                 yield goaleval(goal)(s)
             except EarlyGoalError:
                 pass
     return unique(interleave(f(reifiedgoals), [EarlyGoalError]),
                   key=dicthash)
Ejemplo n.º 3
0
    def anygoal(s):
        reifiedgoals = (reify(goal, s) for goal in goals)

        def f(goals):
            for goal in goals:
                try:
                    yield goaleval(goal)(s)
                except EarlyGoalError:
                    pass

        return unique(interleave(f(reifiedgoals), [EarlyGoalError]),
                      key=dicthash)
Ejemplo n.º 4
0
    def forward(self, input_a, input_b=None, l=None, mix=False):
        input_a_emb = self.embedding(input_a)
        if mix:
            batch_size = input_a.size(0)
            input_b_emb = self.embedding(input_b)
            # 13、14行合成一步了
            mixed_input_emb = l * input_a_emb + (1 - l) * input_b_emb

            # interleave labeled and unlabed samples between batches to get correct batchnorm calculation
            mixed_input = list(torch.split(mixed_input_emb, batch_size//3))
            mixed_input = interleave(mixed_input, batch_size)

            logits = [self._forward(mixed_input[0])]
            for input in mixed_input[1:]:
                logits.append(self._forward(input))

            # put interleaved samples back
            logits = interleave(logits, batch_size)
            logits_x = logits[0]
            logits_u = torch.cat(logits[1:], dim=0)
            return logits_x, logits_u
        else:
            return self._forward(input_a_emb)
Ejemplo n.º 5
0
    def solve(self, inputs, targets):
        digits = list(int(n) for n in inputs)
        winner = None
        c = 0
        possibleOps = list(product(*repeat('-+*', len(digits) - 1)))
        for n in range(self.cycles):
            shuffle(digits)
            ops = choice(possibleOps)

            attempt = Attempt('Sampling', interleave(list(digits), list(ops)),
                              targets)
            result = attempt.run()
            if winner is None or result.score() > winner.score():
                winner = result
            if winner.solved():
                break
            c = c + 1
        return (winner, c)
Ejemplo n.º 6
0
def _c2_get_offset_from_name(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return

    try:
        # Get the current indexes within prefix runs (3037)
        cur_idx0 = _prefix_offsets[frags[0]][0] + _get_suffixes(
            frags[0]).index(frags[1])
        cur_idx1 = _prefix_offsets[frags[2]][0] + _get_suffixes(
            frags[2]).index(frags[3])
    except:
        # Either the prefix or suffix lookup failed, likely a dodgy name
        log.warning(
            "Failed to look up prefixes/suffixes in _c2_get_offset_from_name; bad sector name?"
        )
        return None

    # Interleave the individual offsets into one big offset
    return util.interleave(cur_idx0, cur_idx1,
                           32)  # Again, length is anyone's guess
Ejemplo n.º 7
0
    def solve(self, inputs, targets, maxCycles=10000):
        digits = list(int(n) for n in inputs)
        perms = set(permutations(digits, len(digits)))
        ops = set(product(*repeat('-+*', len(digits) - 1)))

        c = 0
        winner = None
        for p in perms:
            for op in ops:
                attempt = Attempt('Exhaustive',
                                  interleave(list(p), list(op)),
                                  targets)
                result = attempt.run()
                if winner is None or winner.score() < result.score():
                    winner = result
                if winner.solved() is True:
                    break
                c += 1
                if c >= maxCycles:
                    break
            if c >= maxCycles or winner is None or winner.solved() is True:
                break
        return (winner, c)
Ejemplo n.º 8
0
 def allgoal(s):
     g = goaleval(reify(goals[0], s))
     return unique(interleave(
         goaleval(reify((lall, ) + tuple(goals[1:]), ss))(ss)
         for ss in g(s)),
                   key=dicthash)
Ejemplo n.º 9
0
Archivo: core.py Proyecto: jdmcbr/logpy
 def goal_conde(s):
     return unique_dict(interleave(bindstar((s,), *goals)
                                  for goals in goalseqs))
Ejemplo n.º 10
0
 def allgoal(s):
     g = goaleval(reify(goals[0], s))
     return unique(interleave(
                     goaleval(reify((lall,) + tuple(goals[1:]), ss))(ss)
                     for ss in g(s)),
                   key=dicthash)
Ejemplo n.º 11
0
 def anygoal(s):
     reifiedgoals = (reify(goal, s) for goal in goals)
     return interleave((goaleval(goal)(s) for goal in reifiedgoals
                                 if earlysafe(goal)), [EarlyGoalError])