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
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
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())]
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
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
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
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])
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
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)
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
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)
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