コード例 #1
0
ファイル: deduper.py プロジェクト: AnnuSachan/tweetmotif
def choose_multi_label(labels, lang_model):
  longest = util.argmax(labels, scorer=lambda ngram: len(ngram))
  if len(longest) > 3:
    
    best = util.argmax(bigrams.trigrams(longest), lambda ng: lang_model.lidstone(ng))
    best = (best,)
  elif len(longest) == 3:
    best = longest
    best = (best,)
  elif len(longest) <= 2:
    # this is kinda shitty set of them .. would rather want all possible skip n-grams (O(N^2) of them?)
    z = [(tuple(x),) for x in labels] + bigrams.bigrams(labels) + bigrams.trigrams(labels)
    assert z
    z = [x for x in z if len(util.flatten(x)) <= 3]
    # sum is too weird
    # lexicographic ordering of the top-ranked sublabels in the multilabel
    def scorer(ngrams):
      scores = [lang_model.lidstone(ng) for ng in ngrams]
      if len(scores) < 3:
        scores += [0]*(3 - len(scores))
      scores.sort(reverse=True)
      # print "SCORE %-30s %s" % (scores, ngrams)
      return scores
    z.sort(key= scorer, reverse=True)
    # print "RANKING",z
    best = z[0]
  else:
    assert False
  return best
コード例 #2
0
def choose_multi_label(labels, lang_model):
    longest = util.argmax(labels, scorer=lambda ngram: len(ngram))
    if len(longest) > 3:

        best = util.argmax(bigrams.trigrams(longest),
                           lambda ng: lang_model.lidstone(ng))
        best = (best, )
    elif len(longest) == 3:
        best = longest
        best = (best, )
    elif len(longest) <= 2:
        # this is kinda shitty set of them .. would rather want all possible skip n-grams (O(N^2) of them?)
        z = [(tuple(x), ) for x in labels
             ] + bigrams.bigrams(labels) + bigrams.trigrams(labels)
        assert z
        z = [x for x in z if len(util.flatten(x)) <= 3]

        # sum is too weird
        # lexicographic ordering of the top-ranked sublabels in the multilabel
        def scorer(ngrams):
            scores = [lang_model.lidstone(ng) for ng in ngrams]
            if len(scores) < 3:
                scores += [0] * (3 - len(scores))
            scores.sort(reverse=True)
            # print "SCORE %-30s %s" % (scores, ngrams)
            return scores

        z.sort(key=scorer, reverse=True)
        # print "RANKING",z
        best = z[0]
    else:
        assert False
    return best
コード例 #3
0
ファイル: hmm.py プロジェクト: anthonysutardja/mrca
    def viterbi(self):
        """Returns the viterbi decoded path."""
        # TODO(kevintee): Return the viterbi decoded path (list of states)
        viterbi = []
        forward_table = {}
        backpointers = {}

        states = self.theta.m.keys()
        for t in range(len(self.x)):
            for k in states:
                if t == 0:
                    forward_table[k] = [log(self.theta.e[k][self.x[t]]) + \
                        log(self.theta.m[k])]
                    backpointers[k] = [0]
                else:
                    offset = max([forward_table[i][t - 1] for i in states])
                    forward_table[k].append(log(self.theta.e[k][self.x[t]]) + \
                        offset + log(max([e**(forward_table[i][t - 1] - offset) * \
                        self.theta.a[i][k] for i in states])))
                    backpointers[k].append(argmax([e**(forward_table[i][t - 1] - \
                            offset) * self.theta.a[i][k] for i in states]))

        pointer = None
        for t in reversed(range(len(self.x))):
            if t == len(self.x) - 1:
                values = [forward_table[i][t] for i in states]
                viterbi.append(states[argmax(values)])
                pointer = backpointers[states[argmax(values)]][t]
            elif t > 0:
                viterbi = [states[pointer]] + viterbi
                pointer = backpointers[states[pointer]][t]
            elif t == 0:
                viterbi = [states[pointer]] + viterbi

        return viterbi
コード例 #4
0
    def _viterbi_decode(self, feats):
        """
        viterbi 维特比解码
        :param feats:
        :return:
        """

        backpointers = []

        # Initialize the viterbi variables in log space
        init_vvars = torch.full((1, self.tagset_size), -10000.)
        init_vvars[0][self.tag_to_ix[config.START_TAG]] = 0

        # forward_var at step i holds the viterbi variables for step i-1
        forward_var = init_vvars
        for feat in feats:
            bptrs_t = []  # holds the backpointers for this step
            viterbivars_t = []  # holds the viterbi variables for this step

            for next_tag in range(self.tagset_size):
                # next_tag_var[i] holds the viterbi variable for tag i at the
                # previous step, plus the score of transitioning
                # from tag i to next_tag.
                # We don't include the emission scores here because the max
                # does not depend on them (we add them in below)
                next_tag_var = forward_var + self.transitions[next_tag]
                best_tag_id = argmax(next_tag_var)
                bptrs_t.append(best_tag_id)
                viterbivars_t.append(next_tag_var[0][best_tag_id].view(1))
            # Now add in the emission scores, and assign forward_var to the set
            # of viterbi variables we just computed
            forward_var = (torch.cat(viterbivars_t) + feat).view(1, -1)
            backpointers.append(bptrs_t)

        # Transition to STOP_TAG
        terminal_var = forward_var + self.transitions[self.tag_to_ix[
            config.STOP_TAG]]
        best_tag_id = argmax(terminal_var)
        path_score = terminal_var[0][best_tag_id]

        # Follow the back pointers to decode the best path.
        best_path = [best_tag_id]
        for bptrs_t in reversed(backpointers):
            best_tag_id = bptrs_t[best_tag_id]
            best_path.append(best_tag_id)
        # Pop off the start tag (we dont want to return that to the caller)
        start = best_path.pop()
        assert start == self.tag_to_ix[config.START_TAG]  # Sanity check
        best_path.reverse()
        return path_score, best_path
コード例 #5
0
def knapsack(capacity: int, weights: List[int],
             values: List[int]) -> List[bool]:
    if len(weights) == 0:
        raise ValueError("Weights array must not be empty")
    if len(values) == 0:
        raise ValueError("Values array must not be empty")
    if len(weights) != len(values):
        raise ValueError(
            "Both weights and values arrays must have equal length")
    n = len(weights)
    table: List[List[int]] = [[0] * (capacity + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, capacity + 1):
            item = i - 1
            remainder = j - weights[item]
            add_item = 0
            if remainder >= 0:
                add_item = table[i - 1][remainder] + values[item]
            # decide whether to add item or not
            table[i][j] = max(table[i - 1][j], add_item)
    res: List[bool] = [False] * n
    i = n  # start from the last item (last row)
    j = capacity
    while i > 0 and j > 0:
        col = []
        for k in range(i + 1):  # take the column up to the ith item
            col.append(table[k][j])
        i = argmax(col)
        if table[i][
                j] != 0:  # pick this item because it increased the total value
            item = i - 1
            res[item] = True
            j -= weights[item]
            i -= 1
    return res
コード例 #6
0
def test(args, builder):
    print('testing...')
    total_fine_correct = 0.0
    total_coarse_correct = 0
    for test_no in range(args.test_data_size):
        fine_correct = 0
        # Generate a new test example
        length = random.randint(*args.test_length_range)
        sequence = random_sequence(length, args.source_alphabet_size)
        # Start building the computation graph for this sequence
        dynet.renew_cg()
        state = builder.initial_state(1)
        # Feed everything up to and including the separator symbol into the
        # model
        for symbol in sequence.input_sequence():
            index = input_symbol_to_index(symbol)
            state = state.next([index], StackLSTMBuilder.INPUT_MODE)
        # Feed the rest of the sequence into the model and stop at the first
        # error
        for correct_symbol in sequence.output_sequence():
            predicted_index = argmax(state.output().value())
            predicted_symbol = output_index_to_symbol(predicted_index)
            if predicted_symbol == correct_symbol:
                fine_correct += 1
                state = state.next([predicted_index],
                                   StackLSTMBuilder.OUTPUT_MODE)
            else:
                break
        fine_total = sequence.output_sequence_length()
        total_fine_correct += (fine_correct / fine_total)
        total_coarse_correct += (fine_correct == fine_total)
    fine_accuracy = total_fine_correct / args.test_data_size
    coarse_accuracy = total_coarse_correct / args.test_data_size
    print('fine accuracy:   %0.2f' % fine_accuracy)
    print('coarse accuracy: %0.2f' % coarse_accuracy)
コード例 #7
0
ファイル: CRF.py プロジェクト: zxz53000/SA-Sent
    def _viterbi_decode(self, feats):
        batch_size, sent_len, _ = feats.size()        
        feats = feats.transpose(0,1).transpose(1,2).contiguous()

        # it should finally with the size: sent_len * label_size * batch_size
        pointers = []
        backpointers = []

        # Initialize the viterbi variables in log space
        init_vvars = torch.Tensor(sent_len + 1, batch_size, self.label_size).fill_(0)
        if feats.is_cuda:  init_vvars = init_vvars.cuda() 
        forward_var = Variable(init_vvars)

        # pdb.set_trace()
        pointers = []
        for i in range(sent_len):
            # label_size * batch_size
            viterbivars_t = []
            bptr_s = []

            for next_tag in range(self.label_size):
                #next_tag_var = Variable(torch.Tensor(1, self.label_size).fill_(0))
                next_tag_var = None
                feat = feats[i, next_tag]
                emit_score = feat.view(batch_size, 1).expand(batch_size, self.label_size)
                if i == 0: # the first node don't have the transition score
                    next_tag_var = forward_var[i] + emit_score
                else:
                    trans_score = self.transitions[next_tag].view(1, self.label_size).expand(batch_size, self.label_size)
                    next_tag_var = forward_var[i] + trans_score + emit_score
                # pdb.set_trace()
                best_ids, best_value = util.argmax_m(next_tag_var)
                bptr_s.append(best_ids)
                best_value = best_value.view(-1, 1)
                viterbivars_t.append(best_value)
            forward_var[i + 1] = torch.cat(viterbivars_t, 1).view(batch_size, self.label_size)
            pointers.append(bptr_s)
    

        # pdb.set_trace()
        # decode the pointers
        assert len(pointers) == sent_len
        assert len(pointers[0]) == self.label_size
        # should be batch_size * sent_len
        pointers = np.array(pointers)
        ret_label = []
        for batch_id in range(batch_size):
            final_label = util.argmax(forward_var[-1, batch_id])
            sent_labels = []
            # the first state should always be zero
            seqs = pointers[1:,:, batch_id]
            f_ = final_label
            sent_labels.append(f_)
            for tok_ind in reversed(range(sent_len - 1)):
                f_ = seqs[tok_ind][f_]
                sent_labels.append(f_)
            # remember to reverse the labels
            ret_label.append(list(reversed(sent_labels)))
        
        return ret_label
コード例 #8
0
ファイル: mdp.py プロジェクト: danielrcardenas/pyhchem
def best_policy(mdp, U):
    """Given an MDP and a utility function U, determine the best policy,
    as a mapping from state to action. (Equation 17.4)"""
    pi = {}
    for s in mdp.states:
        pi[s] = argmax(mdp.actions(s), key=lambda a: expected_utility(a, s, U, mdp))
    return pi
コード例 #9
0
ファイル: subset_sum.py プロジェクト: seahrh/coding-interview
def subset_sum(capacity: int, weights: Set[int]) -> Set[int]:
    n = len(weights)
    w_list = list(weights)
    # table[i][j] is True if subset with sum j can be obtained with items [1..i]
    table: List[List[bool]] = [[False] * (capacity + 1) for _ in range(n + 1)]
    for i in range(n + 1):
        table[i][0] = True
    for i in range(1, n + 1):
        for j in range(1, capacity + 1):
            item = i - 1
            remainder = j - w_list[item]
            add_item = remainder >= 0 and table[i - 1][remainder]
            table[i][j] = table[i - 1][j] or add_item
    res: Set[int] = set()
    i = n  # start from the last item (last row)
    j = capacity
    while i > 0 and j > 0:
        col = []
        for k in range(i + 1):  # take the column up to the ith item
            col.append(table[k][j])
        i = argmax(col)
        if table[i][j]:  # pick this item
            w = w_list[i - 1]
            res.add(w)
            j -= w
            i -= 1
    return res
コード例 #10
0
    def _viterbi_decode(self, feats):

        backpointers = []
        # analogous to forward
        init_vvars = torch.Tensor(1, self.tagset_size).fill_(-10000.)
        init_vvars[0][self.tag_to_ix['<start>']] = 0
        forward_var = init_vvars
        for feat in feats:
            next_tag_var = forward_var.view(1, -1).expand(
                self.tagset_size, self.tagset_size) + self.transitions
            _, bptrs_t = torch.max(next_tag_var, dim=1)
            bptrs_t = bptrs_t.squeeze().data.cpu().numpy()
            next_tag_var = next_tag_var.data.cpu().numpy()
            viterbivars_t = next_tag_var[range(len(bptrs_t)), bptrs_t]
            viterbivars_t = torch.FloatTensor(viterbivars_t)
            forward_var = viterbivars_t + feat
            backpointers.append(bptrs_t)

        terminal_var = forward_var + self.transitions[self.tag_to_ix['<stop>']]
        terminal_var.data[self.tag_to_ix['<stop>']] = -10000.
        terminal_var.data[self.tag_to_ix['<start>']] = -10000.
        best_tag_id = argmax(terminal_var.unsqueeze(0))
        path_score = terminal_var[best_tag_id]
        best_path = [best_tag_id]
        for bptrs_t in reversed(backpointers):
            best_tag_id = bptrs_t[best_tag_id]
            best_path.append(best_tag_id)
        start = best_path.pop()
        assert start == self.tag_to_ix['<start>']
        best_path.reverse()
        return path_score, best_path
コード例 #11
0
ファイル: smtutil.py プロジェクト: ERATOMMSD/mind_the_gap
def get_interpolants_bv(
        bvfmls: List[Tree], m: int, opt: SMTUtilOption,
        stat: statistics.Statistics) -> Tuple[List[str], List[str], List[Any]]:
    st = time.time()
    liafmls = [convert_bv2lia(fml, m) for fml in bvfmls]
    # print("checking")
    # for b, l in zip(bvfmls, liafmls):
    #     assert_equal_bv(b, m, opt)
    # print("checked")
    stat.time_to_lia += time.time() - st
    withconstraints = [["and", p,
                        get_range_constraints(get_variables(p), m)]
                       for p in liafmls]
    # withconstraints = liafmls
    # lia_interpols = get_interpolants_lia(withconstraints)
    while True:
        try:
            lia_interpols = get_interpolants_lia(withconstraints, opt, stat)
            break
        except UnexpectedInterpolationError:
            logging.warning("Failed at interpolation")
            if not opt.expand_floor_in_inteprolation_error:
                raise UnexpectedInterpolationError()
            modsmods = [
                find_subtrees_by_tags(["mod"], liafml) for liafml in liafmls
            ]
            mods = functools.reduce(list.__add__, modsmods)
            if not mods:
                raise UnexpectedInterpolationError()

            def get_divisor(x):
                assert x[0][0] == "mod"
                return int(x[0][2])

            maxmod, _ = util.argmax(mods, get_divisor)
            logging.warning(
                f"{debug_print_list(maxmod[0])} is being replaced.")
            maxmod[1][maxmod[2]] = mod_to_floor(maxmod[0])

            def fv(t):
                return {v: (0, m - 1) for v in get_variables(t)}

            def ft(t):
                reduced_t, replacing_floor = reduce_float_from_tree(
                    t, fv(t), m, opt, "strategy1")
                processed_replacing_floor = substitute_tree(
                    reduced_t, replacing_floor)
                return processed_replacing_floor

            withconstraints = [ft(t) for t in withconstraints]
            print("hoge")
            stat.num_interp_failure += 1
    st = time.time()
    converteds = [convert_lia2bv(fml, m, opt, stat) for fml in lia_interpols]
    stat.time_from_lia += time.time() - st
    assert_valid_interpolant_bv(bvfmls, converteds, lia_interpols, liafmls, m,
                                opt)
    return converteds, lia_interpols, withconstraints
コード例 #12
0
ファイル: learner.py プロジェクト: niklasnolte/wizard_ml
 def get_next_action(self, state, eps):
     if random.uniform(0, 1) < eps:
         return random.choice([
             a for a, i in zip(self.env.action_space, self.action_mask) if i
         ])
     state = torch.tensor(state, dtype=torch.float32, device=self.device)
     state = state.reshape(1, -1)
     values = self.action_decider(state)
     out = argmax(values[0], self.action_mask)
     return out
コード例 #13
0
ファイル: mdp10.py プロジェクト: wesenu/MIT6.036
def greedy(q, s):
    """ Return pi*(s) based on a greedy strategy.

    >>> q = TabularQ([0,1,2,3],['b','c'])
    >>> q.set(0, 'b', 5)
    >>> q.set(0, 'c', 10)
    >>> q.set(1, 'b', 2)
    >>> greedy(q, 0)
    'c'
    >>> greedy(q, 1)
    'b'
    """
    # Your code here
    return argmax(q.actions, lambda a: q.get(s, a))
コード例 #14
0
ファイル: mdp.py プロジェクト: danielrcardenas/pyhchem
def policy_iteration(mdp):
    """Solve an MDP by policy iteration [Figure 17.7]"""
    U = {s: 0 for s in mdp.states}
    pi = {s: random.choice(mdp.actions(s)) for s in mdp.states}
    while True:
        U = policy_evaluation(pi, U, mdp)
        unchanged = True
        for s in mdp.states:
            a = argmax(mdp.actions(s), key=lambda a: expected_utility(a, s, U, mdp))
            if a != pi[s]:
                pi[s] = a
                unchanged = False
        if unchanged:
            return pi
コード例 #15
0
    def __call__(self, percept):
        s1, r1 = self.update_state(percept)
        Q, Nsa, s, a, r = self.Q, self.Nsa, self.s, self.a, self.r
        alpha, gamma, terminals = self.alpha, self.gamma, self.terminals,
        actions_in_state = self.actions_in_state

        if s in terminals:
            Q[s, None] = r1
        if s is not None:
            Nsa[s, a] += 1
            Q[s, a] += alpha(Nsa[s, a]) * (
                r + gamma * max(Q[s1, a1]
                                for a1 in actions_in_state(s1)) - Q[s, a])
        if s in terminals:
            self.s = self.a = self.r = None
        else:
            self.s, self.r = s1, r1
            self.a = argmax(actions_in_state(s1),
                            key=lambda a1: self.f(Q[s1, a1], Nsa[s1, a1]))
        return self.a
コード例 #16
0
    def __init__(self, moments, origin):
        self.origin = origin

        # Model the ellipse on the component with the largest area
        moments = list(moments)
        m = moments[util.argmax(m.m00 for m in moments)]

        self.centre = (m.m10 / m.m00, m.m01 / m.m00)
        self.angle = 0.5 * math.atan2(2.0 * m.mu11, m.mu20 - m.mu02)

        def variance_at_angle(angle):
            s, c = math.sin(2.0 * angle), math.cos(2.0 * angle)
            nu11, nu02, nu20 = (x / m.m00 for x in (m.mu11, m.mu02, m.mu20))
            return 0.5 * (nu20 + nu02 + c * (nu20 - nu02) + 2.0 * s * nu11)

        # Find the axis lengths up to a constant of proportionality.
        self.axes = tuple(variance_at_angle(x) ** 0.5 for x in (self.angle, self.angle + 0.5 * math.pi))

        # Scale the axes so that the area of the resulting ellipse matches the measured area.
        scale_factor = math.sqrt(m.m00 / (math.pi * self.axes[0] * self.axes[1]))
        self.axes = tuple(scale_factor * x for x in self.axes)
コード例 #17
0
 def dominantPole(self):
     """
     :returns: the pole with the largest magnitude
     """
     return util.argmax(self.poles(), abs)
コード例 #18
0
def greedy(q, s):  # Your code here
    return argmax(q.actions, lambda a: q.get(s, a))
コード例 #19
0
 def get_best_action(self, state):
     possible_actions = self.get_possible_actions(state)
     get_q_value = lambda action: self.q_by_state_action.get((state, action), 0)
     return util.argmax(possible_actions, get_q_value)
コード例 #20
0
 def test_argmax(self):
     x = ["hoge", "hogetarou", "a", "hogeh"]
     res, resval = util.argmax(x, len)
     self.assertEqual((res, resval), ("hogetarou", 9))
コード例 #21
0
def greedy(q, s):
    return argmax(q.actions, lambda a: q.get(s, a))
コード例 #22
0
# transProbs[h1][h2] = p_trans(h2 | h1)
# Estimate this from raw text (fully spuervised)
transCounts = [[0 for h2 in range(K)] for h1 in range(K)]
for i in range(1, len(rawText)):
    h1, h2 = rawText[i - 1], rawText[i]
    transCounts[h1][h2] += 1
transProbs = [util.normalize(counts) for counts in transCounts]
print(transProbs[util.toInt('t')][util.toInt('e')])
print(transProbs[util.toInt('t')][util.toInt('g')])

# emissionProbs[h][e] = p_emit(e | h)
emissionProbs = [[1.0 / K for e in range(K)] for h in range(K)]

### Run EM to learn the emission probabilities

for t in range(200):
    # E-step
    # q[i][h] = P(H_i = h | E = observations)
    q = util.forwardBackward(observations, startProbs, transProbs,
                             emissionProbs)

    print(t)
    print(util.toStrSeq([util.argmax(q_i) for q_i in q]))

    # M-step
    emissionCounts = [[0 for e in range(K)] for h in range(K)]
    for i in range(len(observations)):
        for h in range(K):
            emissionCounts[h][observations[i]] += q[i][h]
    emissionProbs = [util.normalize(counts) for counts in emissionCounts]
コード例 #23
0
rawText = util.toIntSeq(util.readText('lm.train'))
for i in range(len(rawText) - 1):
    h1 = rawText[i]
    h2 = rawText[i + 1]
    transitionCounts[h1][h2] += 1
transitionProbs = [util.normalize(transitionCounts[h1]) for h1 in range(K)]

# emissionProbs[h][e] = p_emit(e | h)
emissionProbs = [[1.0 / K for e in range(K)] for h in range(K)]

### Run EM

observations = util.toIntSeq(util.readText('ciphertext'))

for t in range(200):
    # E-step
    # q[i][h]: q_i(h)
    q = util.forwardBackward(observations, startProbs, transitionProbs,
                             emissionProbs)

    # Printing the best guess
    print(util.toStrSeq([util.argmax(q[i]) for i in range(len(observations))]))
    print('')

    # M-step
    emissionCounts = [[0 for e in range(K)] for h in range(K)]
    for i in range(len(observations)):
        for h in range(K):
            emissionCounts[h][observations[i]] += q[i][h]
    emissionProbs = [util.normalize(emissionCounts[h]) for h in range(K)]
コード例 #24
0
    def CalculateLossForDaf(daf, fValidation=False, fRunning=False):
        dy.renew_cg()
        tagged_daf = {"words":[],"file":daf["file"]}
        daf = daf["words"]

        # add a bos before and after
        seq = ['*BOS*'] + list(' '.join([word for word, _, _, _ in daf])) + ['*BOS*']

        # get all the char encodings for the daf
        char_embeds = [let_enc(let) for let in seq]

        # run it through the bilstm
        char_bilstm_outputs = bilstm(char_embeds)

        # now iterate and get all the separate word representations by concatenating the bilstm output
        # before and after the word
        word_bilstm_outputs = []
        iLet_start = 0
        for iLet, char in enumerate(seq):
            # if it is a bos, check if it's at the end of the sequence
            if char == '*BOS*':
                if iLet + 1 == len(seq):
                    char = ' '
                else:
                    continue
            # if we are at a space, take this bilstm output and the one at the letter start
            if char == ' ':
                cur_word_bilstm_output = dy.concatenate([char_bilstm_outputs[iLet_start], char_bilstm_outputs[iLet]])
                # add it in
                word_bilstm_outputs.append(cur_word_bilstm_output)

                # set the iLet_start ocunter to here
                iLet_start = iLet

        # safe-check, make sure word bilstm outputs length is the same as the daf
        if len(word_bilstm_outputs) != len(daf):
            log_message('Size mismatch!! word_bilstm_outputs: ' + str(len(word_bilstm_outputs)) + ', daf: ' + str(len(daf)))

        s_0 = prev_pos_lstm.initial_state()

        beam = [(['*BOS*'],1.0,s_0,[],0.0,0.0,0.0,0.0,0.0,[])] # seq, prob, lstm_state, losses, class_prec, class_items, pos_prec, rough_pos_prec, pos_items, confidences
        i = 0
        for (word, gold_word_class, gold_word_pos, gold_word_lang), bilstm_output in zip(daf, word_bilstm_outputs):
            should_backprop = gold_word_class == 1
            new_hypos = []
            for hypo in beam:
                seq, hyp_prob, hyp_state, losses, class_prec, class_items, pos_prec, rough_pos_prec, pos_items, confidences = hypo
                new_seq = seq[:]
                new_losses = losses[:]
                new_confidences = confidences[:]

                last_pos = seq[-1]

                next_hyp_state = hyp_state.add_input(pos_enc(last_pos))
                # create the mlp input, a concatenate of the bilstm output and of the prev pos output
                mlp_input = dy.concatenate([bilstm_output, next_hyp_state.output()])

                # run through the class mlp
                class_mlp_output = class_mlp(mlp_input)

                predicted_word_class = np.argmax(class_mlp_output.npvalue())

                new_confidences.append(np.max(class_mlp_output.npvalue()) / np.sum(class_mlp_output.npvalue()))


                # prec
                if should_backprop:
                    class_prec += 1 if predicted_word_class == gold_word_class else 0
                    class_items += 1

                # if we aren't doing validation, calculate the loss
                if not fValidation and not fRunning:
                    if should_backprop: new_losses.append(-dy.log(dy.pick(class_mlp_output, gold_word_class)))
                    word_class_ans = gold_word_class
                # otherwise, set the answer to be the argmax
                else:
                    word_class_ans = predicted_word_class

                # if the word_class answer is 1, do the pos!
                # alternatively, if validating and it's aramic, do the pos!
                if word_class_ans or (fValidation and gold_word_lang) or (fRunning and gold_word_lang):
                    # run the pos mlp output
                    pos_mlp_output = pos_mlp(mlp_input)
                    try:
                        temp_pos_array = pos_mlp_output.npvalue()
                        possible_pos_array = np.zeros(temp_pos_array.shape)
                        pos_list = pos_hashtable[word]
                        # pos_list.add('') #concat 'unknown' as possible pos
                        possible_pos_indices = [pos_vocab[temp_pos] for temp_pos in pos_list]
                        possible_pos_array[possible_pos_indices] = temp_pos_array[possible_pos_indices]
                    except KeyError:
                        possible_pos_array = pos_mlp_output.npvalue()
                        # if fValidation:
                        #    possible_pos_array[pos_vocab['']] = 0.0 # don't allow validation to guess UNK b/c it never trained against that TODO this makes sense, right?

                    poss_pos_sum = np.sum(possible_pos_array)


                    for iprob, prob in enumerate(possible_pos_array):
                        new_seq = seq[:]
                        temp_picked_pos = pos_vocab.getItem(iprob)
                        temp_confidence = possible_pos_array[iprob] / poss_pos_sum
                        new_confidences[-1] = temp_confidence # overwrite class confidence
                        new_pos_prec = pos_prec
                        new_pos_items = pos_items
                        new_rough_pos_prec = rough_pos_prec
                        if should_backprop:
                            new_pos_prec += 1 if temp_picked_pos == gold_word_pos else 0
                            new_rough_pos_prec += 1 if len(temp_picked_pos) > 0 and temp_picked_pos[0] == gold_word_pos[
                                0] else 0  # you got at least the rough pos right
                            new_pos_items += 1

                        if not fValidation and not fRunning:
                            if should_backprop: new_losses.append(
                                -dy.log(dy.pick(pos_mlp_output, pos_vocab[gold_word_pos])))
                        new_seq += [temp_picked_pos]
                        new_prob = hyp_prob + math.log(prob) if prob != 0 else hyp_prob + math.log(1E-10)  # which is log(0.00000001) or something like that
                        new_hypos += [(new_seq, new_prob, next_hyp_state, new_losses, class_prec, class_items, new_pos_prec,
                                      new_rough_pos_prec, new_pos_items, new_confidences)]
                else:
                    # assume prob is 1. It's really good at predicting hebrew / aramaic
                    new_seq = seq[:]
                    new_seq += ['']
                    new_prob = hyp_prob
                    new_hypos += [(new_seq, new_prob, next_hyp_state, new_losses, class_prec, class_items, pos_prec,
                                  rough_pos_prec, pos_items, new_confidences)]

            # pick the best hypos
            new_probs = [p for (s, p, r, l, cp, ci, pp, rpp, pi, c) in new_hypos]
            argmax_indices = util.argmax(new_probs, n=beam_width)
            if type(argmax_indices) == int:
                argmax_indices = [argmax_indices]
            beam = [new_hypos[l] for l in argmax_indices]

            i += 1


            correct_answer_in_beam = False
            for max_ind in argmax_indices:
                if new_hypos[max_ind][0][-1] == gold_word_pos:
                    correct_answer_in_beam = True
                    break
            if not correct_answer_in_beam and not fValidation and not fRunning and with_early_stop:
                # early stop
                break



        final_probs = [p for (s, p, r, l, cp, ci, pp, rpp, pi, c) in beam]
        argmax_index = util.argmax(final_probs)
        final_seq, prob, lstm_state, all_losses, class_prec, class_items, pos_prec, rough_pos_prec, pos_items, confidences = beam[argmax_index]
        for (word, gold_word_class, gold_word_pos, gold_word_lang), pred, conf in zip(daf, final_seq[1:], confidences): # VERY IMPORTANT. final_seq is off-by-one b/c we inited it with BOS
            tagged_daf['words'].append({"word":word,"gold_pos":gold_word_pos,"gold_class":gold_word_class,"predicted":pred,"confidence":conf,"lang":gold_word_lang})
            should_backprop = gold_word_class == 1
            if should_backprop: pos_conf_matrix(pos_vocab[pred], pos_vocab[gold_word_pos])




        if fRunning:
            return tagged_daf

        pos_prec = pos_prec / pos_items if pos_items > 0 else None
        rough_pos_prec = rough_pos_prec / pos_items if pos_items > 0 else None
        class_prec = class_prec / class_items if class_items > 0 else None

        if fValidation:
            return class_prec, pos_prec,tagged_daf, rough_pos_prec

        total_loss = dy.esum(all_losses) if len(all_losses) > 0 else None
        return total_loss, class_prec, pos_prec, rough_pos_prec
コード例 #25
0
ファイル: dist.py プロジェクト: randolphwong/libdw
 def maxProbElt(self):
     """
     :returns: The element in this domain with maximum probability
     """
     return util.argmax(self.support(), self.prob)
コード例 #26
0
ファイル: sf.py プロジェクト: EdgeBotix/SOAR-Source
 def dominantPole(self):
     """
     @returns: the pole with the largest magnitude
     """
     return util.argmax(self.poles(), abs)
コード例 #27
0
ファイル: dist.py プロジェクト: engcarlosrosa/SOAR-Source
 def maxProbElt(self):
     """
     @returns: The element in this domain with maximum probability
     """
     return util.argmax(self.support(), self.prob)
コード例 #28
0
ファイル: pomcp.py プロジェクト: fmgc/mdp
import random
コード例 #29
0
ファイル: evaluate.py プロジェクト: MatthewChang/video-dqn
def ours_evaluate(config, env, ep, house, epind, model, visualize,
                  model_config):
    hn, floor, class_label, goal_dist, pos, rot = ep

    if config.SCORE == 'detector' or config.COMBINE_DETECTOR:
        predictor = get_predictor()
        predictor_class_index = predictor.metadata.thing_classes.index(
            class_label)

    rng = random.Random()
    rng.seed(config.SEED)

    if goal_dist == float('inf'):
        return np.array([]) if config.STOP else 0

    class_index = class_labels.index(class_label)

    def model_score(ims):
        torch_ims = util.torch.to_imgnet(torch.tensor(ims).to('cuda'))
        with torch.no_grad():
            return model(torch_ims.unsqueeze(0))[0,
                                                 class_index, :].max().item()

    # compute the frame score combined with detector
    def score(ims):
        sc = model_score(ims['rgb'])
        if config.COMBINE_DETECTOR:
            size = ims['rgb'].shape[1]
            left_lim, right_lim = int(size / 3), int(size * 2 / 3)
            im = ims['rgb'][0] if len(ims['rgb']) == 4 else ims['rgb']
            boxes, scores = get_scores(predictor, im, predictor_class_index)
            if len(scores) > 0 and scores.max() > config.CONFIDENCE_THRESHOLD:
                box = boxes[scores.argmax()]
                if box[0] <= right_lim or box[2] >= left_lim:
                    if len(ims['rgb']) == 4:
                        ims['rgb'][0] = draw_box(ims['rgb'][0], box,
                                                 scores.max().item())
                    else:
                        ims['rgb'] = draw_box(ims['rgb'], box,
                                              scores.max().item())
                    sc += (scores.max().item() + 1)
        return sc

    def output():
        print(f"SPL: {spl}: {goal_dist}/{dist_traveled}")
        if config.SLAM and visualize:
            planner.write_combined(
                f'%04d_{class_label}-%dm-spl%.2f-steps%d' %
                (epind, int(goal_dist), spl, agent_steps_taken))
        # np.save(f'data_dump/good_trajectory{epind}',np.array(log))
        return np.array(log) if config.STOP else spl

    locs = house.object_locations_for_habitat_dest
    all_goals = [locs[k] for k in sorted(locs.keys())]

    from habitat.utils.visualizations import maps
    top_down_map = maps.get_topdown_map(env.env.sim,
                                        map_resolution=(map_resolution,
                                                        map_resolution))
    rrange, crange = util.habitat.crop_range(top_down_map)
    point_min = util.habitat.from_grid([rrange[0], crange[0]], map_resolution,
                                       0)
    point_max = util.habitat.from_grid([rrange[1], crange[1]], map_resolution,
                                       0)
    max_dim = np.abs(point_max - point_min).max()

    out_dir = f'{config.VIDEO_LOCATION}/{name_from_config(config)}'
    util.ensure_folders(out_dir, True)
    planner = DepthMapperAndPlanner(dt=30,
                                    out_dir=out_dir,
                                    map_size_cm=max_dim * 230,
                                    mark_locs=True,
                                    close_small_openings=True,
                                    log_visualization=visualize)
    polygons = relevant_objects(env.pos, house.objects[class_label])
    planner._reset(goal_dist,
                   global_goals=polygons,
                   start_pos=env.pos,
                   start_ang=env.angle)

    openlist = []
    visited = []
    dist_traveled = 0
    log = []
    spl = 0
    agent_steps_taken = 0
    full_log = []
    episode_ims = [env.get_observation()]
    no_move = False

    def semantic_reasoning():
        # log for visualizations
        planner.log_reasoning()

        images = []
        display_values = []
        for _ in range(NUM_ROTATIONS):
            ims, _, _, _ = env.step(1)
            loc = [*planner.pos_to_loc(env.pos), env.angle]
            planner.add_observation(ims['depth'] * 1000, loc)
            dest = check_movement(config,
                                  env,
                                  env.angle,
                                  planner=planner,
                                  rng=rng)
            sc = score(ims)
            # for visualizations
            images.append(ims)
            display_values.append(sc)
            if dest is not None:
                openlist.append((sc, dest))

        if visualize and config.SLAM:
            ims_to_render = [
                e['rgb'][0] if len(e['rgb'].shape) == 4 else e['rgb']
                for e in images
            ]
            current_pan = join_images(
                ims_to_render,
                -np.array(display_values),
                bl_text='Predicted Values',
                br_text=f'Object Class: {class_label.title()}')
            planner.set_current_pan(current_pan)

    macro_steps = 50 if config.SLAM else 30
    print("goals ", env.goals)

    # initial steps to scan env and choose dest
    semantic_reasoning()
    agent_steps_taken += NUM_ROTATIONS

    for macro_step_num in range(macro_steps):
        print(agent_steps_taken)

        if config.BACKTRACK_REJECTION and len(visited) > 0:
            vis_stack = np.stack(visited)

            def reject(point):
                dists = np.linalg.norm((vis_stack - point)[:, [0, 2]], axis=1)
                return (dists < (success_distance - 0.1)).sum() > 0

            openlist = [e for e in openlist if not reject(e[1])]

        def maxfunc(x):
            s, d = x
            dist = np.linalg.norm(env.pos - d)
            return s + config.CONSISTENCY_WEIGHT * max(10 - dist, 0) / 10

        if len(openlist) == 0:
            print("openlist exhausted")
            if visualize: planner.write_combined()
            return output()

        ind, (sc, next_pos), _ = util.argmax(openlist, maxfunc)
        openlist.pop(ind)

        original_openlist = openlist.copy()

        # remove points which we cannot move toward, with an exception
        # for the first step due to the initilization process
        dist_est = planner.fmmDistance(next_pos)
        while not planner.action_toward(next_pos):
            if len(openlist) == 0:
                print("openlist exhausted fmm")
                if visualize: planner.write_combined()
                return output()
            ind, (sc, next_pos), _ = util.argmax(openlist, maxfunc)
            openlist.pop(ind)
            dist_est = planner.fmmDistance(next_pos)

        print('score of', sc)

        if visualize and config.SLAM:
            planner.set_current_open(openlist)

        obs = env.get_observation()
        planner.set_goal(next_pos)
        goal_reached = False

        # 6 for initial rotation, and 2x distance*4 to
        # account for 1 rotation per forward step on average
        step_estimate = math.ceil(2 * (dist_est / 0.25) + 6)
        cur_dist_est = dist_est
        for step in range(step_estimate):
            new_dist_est = planner.fmmDistance(next_pos)
            # replan if the estimated distance jumps up too much
            if new_dist_est > cur_dist_est + 0.1:
                print('replan')
                break
            cur_dist_est = new_dist_est
            action = planner.get_action_toward(next_pos)
            print('action: ', action)

            if action == 3:
                print('subgoal reached')
                break

            before_pos = env.pos
            obs, _, _, _ = env.step(action)

            if action == 0:
                dist_traveled += 0.25
            planner.pos_to_loc(env.pos)
            planner.log_act(obs, env.pos, env.angle, action)
            episode_ims.append(obs)
            visited.append(env.pos)
            log.append([
                env.pos, env.rot, dist_traveled,
                env.distance_to_goal(), step == 0
            ])
            agent_steps_taken += 1

            if env._dist_to_goal(
                    env.pos) < success_distance and not config.STOP:
                spl = min(goal_dist / (dist_traveled + 1e-5), 1)
                return output()
            if agent_steps_taken >= MAX_STEPS: return output()
        semantic_reasoning()
        agent_steps_taken += NUM_ROTATIONS
        if agent_steps_taken >= MAX_STEPS: return output()
    return output()