def test_eval2(): e = Eval() assert e.eval("""\ def f(x): return x**2 f(3) """\ ) == "9" assert e.eval("""\ def f(x): return x**2 f(3) a = 5 """\ ) == "" assert e.eval("""\ def f(x): return x**2 if f(3) == 9: a = 1 else: a = 0 a """\ ) == "1" assert e.eval("""\ def f(x): return x**2 + 1 if f(3) == 9: a = 1 else: a = 0 a """\ ) == "0"
def validation(val_dataLoader, criterion, args): print("******* begin validation *******") model.eval() all_loss = 0.0 EVAL = Eval(args.n_classes) EVAL.reset() with torch.no_grad(): with tqdm(total=len(val_dataLoader), unit='batch') as pbar: for batch_id, (data, target_mask) in enumerate(val_dataLoader): data, target_mask = data.to(device), target_mask.to(device) out = model(data) loss = criterion(out, target_mask) current_loss = loss.data.item() all_loss += current_loss out = out.data.cpu().numpy() target_mask = target_mask.data.cpu().numpy() EVAL.add_batch(target_mask, out.argmax(axis=1)) pbar.update(1) print('[ validation ] [average loss:{}]'.format(all_loss/len(val_dataLoader))) PA = EVAL.Pixel_Accuracy() MPA = EVAL.Mean_Pixel_Accuracy() MIoU = EVAL.Mean_Intersection_over_Union() FWIoU = EVAL.Frequency_Weighted_Intersection_over_Union() print('[ validation ] [PA1: {:.8f}], [MPA1: {:.8f}], [MIoU1: {:.8f}], [FWIoU1: {:.8f}]'.format(PA, MPA,MIoU, FWIoU)) log_message ='**validation** [average loss: {:.8f} ],[PA1: {:.8f}], [MPA1: {:.8f}], [MIoU1: {:.8f}], [FWIoU1: {:.8f}]'\ .format(all_loss/len(val_dataLoader),PA, MPA,MIoU, FWIoU) write_log(args.log_file_name,log_message) return FWIoU
def test_eval3(): e = Eval() assert e.eval("xxxx").startswith("Traceback") assert e.eval("""\ def f(x): return x**2 + 1 + y if f(3) == 9: a = 1 else: a = 0 a """\ ).startswith("Traceback")
def evaluate(model, train_mat, test_mat, config, logger, device): logger.info("Start evaluation") model.eval() device = torch.device(config.device) with torch.no_grad(): user_num, item_num = train_mat.shape # users = torch.from_numpy(np.random.choice(user_num, min(user_num, 5000), False)).to(device) users = np.random.choice(user_num, min(5000, user_num), False) evals = Eval() m = evals.evaluate_item(train_mat[users, :], test_mat[users, :], users, model, device, topk=50) return m
def test_zero(epoch, dataset, logdir, N=4): recall_rates = np.load(os.path.join(data_root, 'recall.npy')) aps = [] accs = [] topk = [1, 5, 10] for i, item in enumerate(tqdm(dataset)): probe, label, gallery_feat = item probe = probe.squeeze() gcn_data = gallery_gcn_det(probe, gallery_feat, num=N) gcn_data = gcn_data[:, 0, :].squeeze() sim = gcn_data.dot(probe[0, :].squeeze()).ravel() pred = sim ap, acc = Eval(label, pred, topk) aps.append(ap * recall_rates[i]) accs.append(acc) print(' mAP = {:.2%}'.format(np.mean(aps))) accs = np.mean(accs, axis=0) for i, k in enumerate(topk): print(' top-{:2d} = {:.2%}'.format(k, accs[i])) return 0
def test_eval1(): e = Eval() assert e.eval("1+1") == "2" assert e.eval("1+1\n") == "2" assert e.eval("a=1+1") == "" assert e.eval("a=1+1\n") == "" assert e.eval("a=1+1\na") == "2" assert e.eval("a=1+1\na\n") == "2" assert e.eval("a=1+1\na=3") == "" assert e.eval("a=1+1\na=3\n") == ""
def try_sympy(self, s): namespace = {} exec "from sympy.interactive import *" in {}, namespace a = Eval(namespace) # change to True to spare the user from exceptions: r = a.eval(s, use_none_for_exceptions=False) if r is not None: result = [ {"title": "Input", "input": s}, {"title": "SymPy", "input": s, "output": r}, ] code = """\ s = %s a = s.atoms(Symbol) if len(a) == 1: x = a.pop() result = %s else: result = None result """ line = "diff(%s, x)" % s r = a.eval(code % (s, line), use_none_for_exceptions=True) if r and r != "None": result.append( {"title": "Derivative", "input": line, "output": r}) line = "integrate(%s, x)" % s r = a.eval(code % (s, line), use_none_for_exceptions=True) if r and r != "None": result.append( {"title": "Indefinite integral", "input": line, "output": r}) line = "series(%s, x, 0, 10)" % s r = a.eval(code % (s, line), use_none_for_exceptions=True) if r and r != "None": result.append( {"title": "Series expansion around 0", "input": line, "output": r}) return result else: return None
def test_one_epoch(epoch, dataset, model, logdir, best, N=4): aps = [] accs = [] aps_p = [] accs_p = [] topk = [1, 5, 10] recall_rates = np.load(os.path.join(data_root, 'recall.npy')) for i, item in enumerate(tqdm(dataset)): probe, label, gallery_feat = item probe = probe.squeeze() gcn_data = gallery_gcn_det(probe, gallery_feat, num=N) gcn_data0 = gcn_data[:, 0, :].squeeze() sim0 = gcn_data0.dot(probe[0, :].squeeze()).ravel() gcn_data = torch.tensor(gcn_data).cuda() probe = torch.tensor(probe).cuda() prob, gal, sim = model(gcn_data, probe) pred = sim.cpu().data.numpy() pred_p = pred + sim0 ap_p, acc_p = Eval(label, pred_p, topk) aps_p.append(ap_p * recall_rates[i]) accs_p.append(acc_p) map = np.mean(aps_p) print(' mAP = {:.2%}'.format(map)) accs = np.mean(accs_p, axis=0) for i, k in enumerate(topk): print(' top-{:2d} = {:.2%}'.format(k, accs[i])) #print('epoch:{} acc:{:.4f} map:{:.4f}'.format(epoch, accs[0], map)) if accs[0] > best: best = accs[0] if not os.path.exists(logdir): os.mkdir(logdir) np.save(os.path.join(logdir, 'log_{}'.format(epoch)), accs) logfile = os.path.join(logdir, 'log_gcnnet{}.txt'.format(epoch)) writer = open(logfile, 'w') writer.write("company number: " + str(N)) writer.write('\n') writer.write('map:{:.4f}\n'.format(map)) ids = topk for i, topi in enumerate(ids): writer.write('top_{} acc:{:.4f}\n'.format(topi, accs[i])) writer.close() return best
def eval_input(self, s): namespace = {} exec PREEXEC in {}, namespace def plot(f=None, **kwargs): """Plot functions. Not the same as SymPy's plot. This plot function is specific to Gamma. It has the following syntax:: plot([x^2, x^3, ...]) or:: plot(y=x,y1=x^2,r=sin(theta),r1=cos(theta)) ``plot`` accepts either a list of single-variable expressions to plot or keyword arguments indicating expressions to plot. If keyword arguments are used, the plot will be polar if the keyword argument starts with ``r`` and will be an xy graph otherwise. Note that Gamma will cut off plot values above and below a certain value, and that it will **not** warn the user if so. """ pass namespace.update({ 'plot': plot, # prevent textplot from printing stuff 'help': lambda f: f }) evaluator = Eval(namespace) # change to True to spare the user from exceptions: if not len(s): return None transformations = [] transformations.append(synonyms) transformations.extend(standard_transformations) transformations.extend((convert_xor, custom_implicit_transformation)) parsed = stringify_expr(s, {}, namespace, transformations) try: evaluated = eval_expr(parsed, {}, namespace) except SyntaxError: raise except Exception as e: raise ValueError(str(e)) input_repr = repr(evaluated) namespace['input_evaluated'] = evaluated return parsed, arguments(parsed, evaluator), evaluator, evaluated
def try_sympy(self, s): namespace = {} exec PREEXEC in {}, namespace a = Eval(namespace) # change to True to spare the user from exceptions: if not len(s): return try: evaluated = sympify(s, convert_xor=True, locals={ 'integrate': sympy.Integral, 'plot': lambda func: func }) input_repr = repr(evaluated) namespace['input_evaluated'] = evaluated except sympy_parser.TokenError: return [{ "title": "Input", "input": s }, { "title": "Error", "input": s, "error": "Invalid input" }] except Exception as e: return self.handle_error(s, e) if input_repr is not None: result = [ { "title": "Input", "input": s }, { "title": "SymPy", "input": s, "output": input_repr }, ] if isinstance(evaluated, sympy.Basic): variables = evaluated.atoms(Symbol) if len(variables) == 1: var = variables.pop() else: var = None else: var = None convert_input, cards = find_result_set(evaluated) namespace['input_evaluated'], var = convert_input(evaluated, var) # Come up with a solution to use all variables if more than 1 # is entered. if var != None: # See a better way to do this. input_repr = repr(namespace['input_evaluated']) line = "simplify(input_evaluated)" simplified = a.eval(line, use_none_for_exceptions=True) r = sympify(a.eval(line, use_none_for_exceptions=True)) if simplified != "None" and simplified != input_repr: result.append({ "title": "Simplification", "input": simplified, "output": mathjax_latex(r) }) for card in cards: try: r = card.eval(a, var) if r != "None": formatted_input = card.format_input( input_repr, var) result.append( dict(title=card.title, input=formatted_input, pre_output=latex( card.pre_output_function( input_repr, var)), output=card.format_output( r, mathjax_latex))) except SyntaxError: pass return result else: return None
def evaluate(self, train, test): m, n = train.shape u, v = self.get_uv() users = np.random.choice(m, min(m, 50000), False) m = Eval.evaluate_item(train[users, :], test[users, :], u[users, :], v, topk=-1) return m
def try_sympy(self, s): namespace = {} exec PREEXEC in {}, namespace a = Eval(namespace) # change to True to spare the user from exceptions: if not len(s): return try: evaluated = sympify(s, convert_xor=True, locals={ 'integrate': sympy.Integral, 'plot': lambda func: func }) input_repr = repr(evaluated) namespace['input_evaluated'] = evaluated except sympy_parser.TokenError: return [ {"title": "Input", "input": s}, {"title": "Error", "input": s, "error": "Invalid input"} ] except Exception as e: return self.handle_error(s, e) if input_repr is not None: result = [ {"title": "Input", "input": s}, {"title": "SymPy", "input": s, "output": input_repr}, ] if isinstance(evaluated, sympy.Basic): variables = evaluated.atoms(Symbol) if len(variables) == 1: var = variables.pop() else: var = None else: var = None convert_input, cards = find_result_set(evaluated) namespace['input_evaluated'], var = convert_input(evaluated, var) # Come up with a solution to use all variables if more than 1 # is entered. if var != None: # See a better way to do this. input_repr = repr(namespace['input_evaluated']) line = "simplify(input_evaluated)" simplified = a.eval(line, use_none_for_exceptions=True) r = sympify(a.eval(line, use_none_for_exceptions=True)) if simplified != "None" and simplified != input_repr: result.append( {"title": "Simplification", "input": simplified, "output": mathjax_latex(r)}) for card in cards: try: r = card.eval(a, var) if r != "None": formatted_input = card.format_input(input_repr, var) result.append(dict( title=card.title, input=formatted_input, pre_output=latex( card.pre_output_function(input_repr, var)), output=card.format_output(r, mathjax_latex) )) except SyntaxError: pass return result else: return None