Esempio n. 1
0
def _document_matrix(structured_phrases, phrases_to_score, status_callback=None):
    indices = {}
    phrases = {}

    for i, phrase in enumerate(phrases_to_score):
        indices[phrase] = i
        phrases[i] = phrase

    # M = np.zeros((len(phrases_to_score), len(structured_phrases)))
    # sparsesvd requires csc format of sparse matrix
    M = lil_matrix((len(phrases_to_score), len(structured_phrases)))

    count = 0
    length = len(structured_phrases)
    increment = length / 100

    def do_callback():
        pct_format = lambda percent: 'building document matrix: %.0f%% done' % (percent * 100)
        if increment > 0 and status_callback and count % increment == 0:
            status_callback(pct_format(float(count) / length))

    for doc_index, doc in enumerate(structured_phrases):
        do_callback()
        count = doc_index
        for phrase in doc:
            h = hashable(phrase)
            if h in indices:
                M[indices[hashable(phrase)], doc_index] += 1
    return M.todense(), indices, phrases
Esempio n. 2
0
def _document_matrix(structured_phrases,
                     phrases_to_score,
                     status_callback=None):
    indices = {}
    phrases = {}

    for i, phrase in enumerate(phrases_to_score):
        indices[phrase] = i
        phrases[i] = phrase

    # M = np.zeros((len(phrases_to_score), len(structured_phrases)))
    # sparsesvd requires csc format of sparse matrix
    M = lil_matrix((len(phrases_to_score), len(structured_phrases)))

    count = 0
    length = len(structured_phrases)
    increment = length / 100

    def do_callback():
        pct_format = lambda percent: 'building document matrix: %.0f%% done' % (
            percent * 100)
        if increment > 0 and status_callback and count % increment == 0:
            status_callback(pct_format(float(count) / length))

    for doc_index, doc in enumerate(structured_phrases):
        do_callback()
        count = doc_index
        for phrase in doc:
            h = hashable(phrase)
            if h in indices:
                M[indices[hashable(phrase)], doc_index] += 1
    return M.todense(), indices, phrases
Esempio n. 3
0
    def value(self, game, playouts=100, steps=5):

        # play random playouts starting from that game value
        with Pool() as p:
            scores = p.map(self.playout,
                           [game.copy() for i in range(0, playouts)])

        return self.differential[hashable(
            game.state())] * 1.0 / self.visits[hashable(game.state())]
Esempio n. 4
0
def calculate_heatmap_values(heatmap_terms, graph_terms, model=None):
    """returns a dictionary of term -> intensity values for the
    terms in documents in heatmap_terms (which should be an iterable
    of term tuples) that are also in the set
    graph_terms"""
    term_counts = Counter()
    for term in heatmap_terms:
        if (not graph_terms) or (hashable(term) in graph_terms):
            term_counts[hashable(term)] += 1
    # term_counts = Counter(term for term in heatmap_terms if hashable(term) in graph_terms)
    return term_counts
Esempio n. 5
0
def calculate_heatmap_values(heatmap_terms, graph_terms, model=None):
    """returns a dictionary of term -> intensity values for the
    terms in documents in heatmap_terms (which should be an iterable
    of term tuples) that are also in the set
    graph_terms"""
    term_counts = Counter()
    for term in heatmap_terms:
        if (not graph_terms) or (hashable(term) in graph_terms):
            term_counts[hashable(term)] += 1
    # term_counts = Counter(term for term in heatmap_terms if hashable(term) in graph_terms)
    return term_counts
Esempio n. 6
0
    def value(self, game=chess.Board(), playouts=100, steps=5):

        # play random playouts starting from that game value
        with Pool() as p:
            p.map(self.playout,
                  [board(state(game)) for i in range(0, playouts)])

        V = self.differential[hashable(
            state(game))] * 1.0 / self.visits[hashable(state(game))]
        dataset = [{'input': state(game), 'target': V}]
        for i in range(0, steps):
            self.model.fit(dataset, batch_size=1, verbose=False)

        return V
Esempio n. 7
0
    def playout(self, game=chess.Board(), C=1.5, expand=80):

        if expand == 0 or game.is_game_over():
            score = 1 if game.is_checkmate() else 0
            self.record(game, score)
            print(score, file=sys.stderr)
            return score

        action_mapping = {}

        for action in game.generate_legal_moves():
            game.push(action)

            N = self.visits.get("total", 1)
            Ni = self.visits.get(hashable(state(game)), 1e-5)
            #V = self.differential.get(hashable(state(game)), 0)*1.0/Ni
            V = self.network_value(game)
            U = V + C * (np.log(N) / Ni)
            U = min(U, 100000) + 1
            action_mapping[action] = (U +
                                      random.random() * 1e-5)**2  #tiebreaker

            game.pop()

        chosen_action = sample(action_mapping)
        game.push(chosen_action)
        score = -self.playout(game, C=C, expand=expand - 1)  #play branch
        game.pop()
        self.record(game, score)

        return score
Esempio n. 8
0
    def get_direct_parent_interfaces(self):
        """
        return a set of all direct (local) implemented interfaces
        """

        if self.data[0] == sidlir.interface:
            parents = sidlir.interface_extends(self.data)
        else:
            parents = sidlir.class_implements(self.data)
        return set([hashable(impl) for _, impl in parents])
Esempio n. 9
0
def test_hashable():
    assert utils.hashable(1) is True
    assert utils.hashable('str') is True
    assert utils.hashable(u'unicode') is True
    assert utils.hashable([]) is False
    assert utils.hashable(set()) is False
    assert utils.hashable({}) is False

    assert _utils.hashable(1) is True
    assert _utils.hashable('str') is True
    assert _utils.hashable(u'unicode') is True
    assert _utils.hashable([]) is False
    assert _utils.hashable(set()) is False
    assert _utils.hashable({}) is False
Esempio n. 10
0
 def heuristic_value(self, game):
     N = self.visits.get("total", 1)
     Ni = self.visits.get(hashable(game.state()), 1e-9)
     V = self.network_value(game)
     return V + self.C * (np.log(N) / Ni)
Esempio n. 11
0
 def arg_hash((arg, attr, mode, typ, name)):
     if typ[0] == sidlir.scoped_id:
         return mode, hashable(typ)
     if typ[0] == sidlir.array:
         return hashable((mode, typ[0], sidlir.hashable_type_id(typ), typ[2], typ[3]))
     return mode, typ
Esempio n. 12
0
 def heuristic_value(self, game):
     N = self.visits.get("total", 1)
     Ni = self.visits.get(hashable(game.state()), 1e-9)
     V = self.differential.get(hashable(game.state()), 0) * 1.0 / Ni
     return V + self.C * (np.log(N) / Ni)
Esempio n. 13
0
 def record(self, game, score):
     self.visits["total"] = self.visits.get("total", 1) + 1
     self.visits[hashable(
         game.state())] = self.visits.get(hashable(game.state()), 0) + 1
     self.differential[hashable(game.state())] = self.differential.get(
         hashable(game.state()), 0) + score