def draw_polyline_nice_corners(cr, points, arrow_degrees, arrow_len, arrow_start=False, arrow_end=False): ex, ey = points[-1] prev = points[0] if arrow_start: cr.move_to(prev[0], prev[1]) draw_arrow(cr, utils.make_vector(points[1], points[0]), arrow_degrees, arrow_len) cr.move_to(prev[0], prev[1]) for i in xrange(1, len(points) - 1): a = utils.make_vector(points[i - 1], points[i]) b = utils.make_vector(points[i], points[i + 1]) la = utils.vector_len(a) lb = utils.vector_len(b) if la < 0.01 or lb < 0.01: continue v = utils.vector_mul_scalar(utils.normalize_vector(a), min(la, 20.0)) w = utils.vector_mul_scalar(utils.normalize_vector(b), min(lb, 20.0)) t = utils.vector_diff(points[i], v) cr.line_to(t[0], t[1]) cr.rel_curve_to(v[0], v[1], v[0], v[1], v[0] + w[0], v[1] + w[1]) cr.line_to(ex, ey) cr.stroke() if arrow_end: cr.move_to(ex, ey) draw_arrow(cr, utils.make_vector(points[-2], points[-1]), arrow_degrees, arrow_len)
def draw_polyline_nice_corners(cr, points, arrow_degrees, arrow_len, arrow_start = False, arrow_end = False): ex, ey = points[-1] prev = points[0] if arrow_start: cr.move_to(prev[0],prev[1]) draw_arrow(cr, utils.make_vector(points[1], points[0]), arrow_degrees, arrow_len) cr.move_to(prev[0],prev[1]) for i in xrange(1, len(points) - 1): a = utils.make_vector(points[i-1], points[i]) b = utils.make_vector(points[i], points[i + 1]) la = utils.vector_len(a) lb = utils.vector_len(b) if la < 0.01 or lb < 0.01: continue v = utils.vector_mul_scalar(utils.normalize_vector(a), min(la, 20.0)) w = utils.vector_mul_scalar(utils.normalize_vector(b), min(lb, 20.0)) t = utils.vector_diff(points[i], v) cr.line_to(t[0], t[1]) cr.rel_curve_to(v[0], v[1], v[0], v[1], v[0] + w[0], v[1] + w[1]) cr.line_to(ex,ey) cr.stroke() if arrow_end: cr.move_to(ex, ey) draw_arrow(cr, utils.make_vector(points[-2], points[-1]), arrow_degrees, arrow_len)
def on_mouse_move(self, event, position): if self.scroll_point: change = utils.make_vector(position, self.scroll_point) self.canvas.set_viewport( utils.vector_add(self.canvas.get_viewport(), change)) return if self.mass_selection_origin: self.mass_selection_cursor = position self.mouseover_items = self.get_items_in_mass_selection() self.set_highlight() return if self.drag_items: change = utils.make_vector(self.drag_mouse_origin, position) for i, item in enumerate(self.drag_items): new_position = utils.vector_add(self.drag_items_origin[i], change) new_position = utils.snap_to_grid(new_position, self.grid_size) item.set_position(new_position) self.canvas.redraw() return item = self.get_item_at_position(position) if item: self.canvas.set_cursor(get_cursor(item.action)) if self.selection: self.mouseover_items = item.get_group() self.set_highlight() elif self.mouseover_items: self.canvas.set_cursor(None) self.mouseover_items = [] self.set_highlight() else: self.canvas.set_cursor(None)
def test(model, data): acc, total = 0, 0 for d in data: q = d[0] #print('q', ' '.join(q)) labels = d[1] cands = d[2] # preprare answer labels label_indices = [cands.index(l) for l in labels if l in cands] # build data q = make_vector([q], w2i, len(q)) cands = [label_to_ans_text[c] for c in cands] # id to text max_cand_len = min(args.max_sent_len, max([len(c) for c in cands])) cands = make_vector(cands, w2i, max_cand_len) # predict scores = [model(q, c.unsqueeze(0)).data[0] for c in cands] pred_idx = np.argmax(scores) if pred_idx in label_indices: #print('correct') acc += 1 #else: #print('wrong') total += 1 print('Test Acc:', 100 * acc / total, '%')
def on_mouse_move(self, event, position): if self.scroll_point: change = utils.make_vector(position, self.scroll_point) self.canvas.set_viewport( utils.vector_add(self.canvas.get_viewport(), change)) return if self.drag_items: change = utils.make_vector(self.drag_mouse_origin, position) for i, item in enumerate(self.drag_items): new_position = utils.vector_add(self.drag_items_origin[i], change) new_position = utils.snap_to_grid(new_position, self.grid_size) item.set_position(new_position) self.canvas.redraw() return item = self.get_item_at_position(position) if item: self.canvas.set_cursor(get_cursor(item.action)) if self.selection: self.mouseover_items = item.get_group() self.set_highlight() elif self.mouseover_items: self.canvas.set_cursor(None) self.mouseover_items = [] self.set_highlight() else: self.canvas.set_cursor(None)
def on_mouse_move(self, event, position): if self.resize_item: change = utils.make_vector(self.initial_mouse, position) change = utils.snap_to_grid(change, self.grid_size) new_size = utils.vector_add(self.initial_size, change) new_size = utils.vector_at_least(new_size, 0, 0) self.resize_item.size = new_size self.canvas.redraw() else: NetEditCanvasConfig.on_mouse_move(self, event, position)
def set_position(self, position): points = self.multiline.get_points() if position is None: self.point_index = (len(points) - 1) / 2 self.line_param = 0.5 self.offset = (0.0, 0.0) else: self.point_index, self.line_param = \ utils.nearest_point_of_multiline(points, position) self.offset = utils.make_vector( self.compute_point_on_multiline(points), position)
def set_position(self, position): points = self.multiline.get_points() if position is None: self.point_index = (len(points) - 1) / 2 self.line_param = 0.5 self.offset = (0.0, 0.0) else: self.point_index, self.line_param = \ utils.nearest_point_of_multiline(points, position) self.offset = utils.make_vector(self.compute_point_on_multiline(points), position)
def train(model, data, test_data, optimizer, n_epochs=4, batch_size=256): for epoch in range(n_epochs): model.train() print('epoch', epoch) random.shuffle(data) # TODO use idxies losses = [] for i, d in enumerate(tqdm(data)): q, pos, negs = d[0], d[1], d[2] vec_q = make_vector([q], w2i, len(q)) vec_pos = make_vector([pos], w2i, len(pos)) pos_sim = model(vec_q, vec_pos) for _ in range(50): neg = random.choice(negs) vec_neg = make_vector([neg], w2i, len(neg)) neg_sim = model(vec_q, vec_neg) loss = loss_fn(pos_sim, neg_sim) if loss.data[0] != 0: losses.append(loss) break if len(losses) == batch_size or i == len(data) - 1: loss = torch.mean(torch.stack(losses, 0).squeeze(), 0) #print(loss.data[0]) #print(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() losses = [] filename = '{}/Epoch-{}.model'.format('./checkpoints', epoch) save_checkpoint( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'loss': loss, }, filename=filename) test(model, test_data)
def get_size(self): return utils.make_vector(self.point1.get_position(), self.point2.get_position())