def validate(model): start = time.time() validate_df = pd.read_csv(config.validatePath, header=0, encoding='utf-8') x_val = pickle.load(open('word_indices_val.pkl', 'rb')) x_val = pad_sequences(x_val, maxlen) y_val = validate_df.iloc[:, 4] + 2 pred = np.argmax(model.predict(x_val), axis=-1) scores = get_f1_score(y_val, pred) print(scores) print('validation time:{}'.format(time.time() - start)) return scores
def validate(model, val_loader): model.eval() start = time.time() scores = [] for x_val, y_val in val_loader: out = model(x_val) scores.append(get_f1_score(y_val, torch.argmax(out, dim=1))) scores = np.mean(scores, axis=0) print(scores) print('validation time: ', time.time()-start) model.train() return scores
batch_y = batch_y.to(device) optimizer.zero_grad() pred = net(batch_x) loss = loss_function(pred, batch_y) loss.backward() optimizer.step() # 计算准确率 this_test_acc[i] = get_acc(net.cpu(), test_x.cpu(), test_y.cpu()) this_train_acc[i] = get_acc(net.cpu(), train_x.cpu(), train_y.cpu()) # 计算f1 this_test_f1[i] = get_f1_score(net.cpu(), test_x.cpu(), test_y.cpu()) this_train_f1[i] = get_f1_score(net.cpu(), train_x.cpu(), train_y.cpu()) mean_test_acc_list.append(this_test_acc.mean()) mean_train_acc_list.append(this_train_acc.mean()) np.save(training_path + 'test_acc_for_epoch_{}'.format(k_fold_time), np.array(mean_test_acc_list)) np.save(training_path + 'train_acc_for_epoch_{}'.format(k_fold_time), np.array(mean_train_acc_list)) mean_test_f1_list.append(this_test_f1.mean()) mean_train_f1_list.append(this_train_f1.mean()) np.save(training_path + 'test_f1_for_epoch_{}'.format(k_fold_time), np.array(mean_test_f1_list)) np.save(training_path + 'train_f1_for_epoch_{}'.format(k_fold_time),
def post(self): parse = reqparse.RequestParser() parse.add_argument('hid', type=int, required=True, help='hid cannot be blank') parse.add_argument('file', type=FileStorage, location='files') args = parse.parse_args() hid = args.get('hid') file = args.get('file') uid = current_user.uid # 获取当前作业 try: homework = HomeworkTable.query.get(hid) except: response = make_response(jsonify(code=10, message='database error')) return response # 获取权重 id weight_id = homework.weightid try: weight = WeightTable.query.get(weight_id) except: response = make_response(jsonify(code=10, message='database error')) return response print(weight) ## 获取答案的路径 ans_path = get_answer_path(hid) print(ans_path) # 如果答案不存在 if ans_path == '': response = make_response(jsonify(code=10, message='database error')) return response ## 暂时保存 filename = file.filename if '\"' in filename: filename = filename[:-1] actual_filename = generate_temp_name(hid, uid, filename) res_path = os.path.join(os.path.expanduser(TEMP_FOLDER), actual_filename) file.save(res_path) ## 各个指标的权重值 micro_weight = weight.micro macro_weight = weight.macro f1_score_weight = weight.f1_score rmse_weight = weight.rmse r2_score_weight = weight.r2_score print('各个指标的权重') print(micro_weight) print(macro_weight) print(f1_score_weight) print(rmse_weight) print(r2_score_weight) ## 各个指标的得分初始化 micro_score = 0 macro_score = 0 f1_score_score = 0 rmse_score = 0 r2_score_score = 0 ## 总分 total = 0 ## 计算得分 if micro_weight > 0: total += micro_weight micro_score = get_micro_precision_score(ans_path, res_path) * micro_weight print('micro_score: ') print(micro_score) if macro_weight > 0: total += macro_weight macro_score = get_macro_precision_score(ans_path, res_path) * macro_weight print('macro_score: ') print(macro_score) if f1_score_weight > 0: total += f1_score_weight f1_score_score = get_f1_score(ans_path, res_path) * f1_score_weight print('f1_score_score: ') print(f1_score_score) if rmse_weight > 0: total += rmse_weight rmse_score = get_rmse(ans_path, res_path) * rmse_weight print('rmse_score: ') print(rmse_score) if r2_score_weight > 0: total += r2_score_weight r2_score_score = get_r2_score(ans_path, res_path) * r2_score_weight print('r2_score_score: ') print(r2_score_score) # 计算加权平均 score = round((micro_score + macro_score + f1_score_score + rmse_score + r2_score_score) / total, 1) print('score: ') print(score) try: print('1') user_homework = UserHomeworkTable.query.filter( and_(UserHomeworkTable.hid == hid, UserHomeworkTable.uid == uid)).first() print(user_homework) except: print('2') response = make_response(jsonify(code=10, message='database error')) return response if user_homework is None: print('3') response = make_response(jsonify(code=41, message='请先上传代码,再上传结果')) return response try: print('4') user_homework.score = score db.session.commit() except: response = make_response(jsonify(code=10, message='database error')) return response response = make_response( jsonify(code=0, message='OK', data={'user_homework': user_homework.to_json()})) return response
validate_df = pd.read_csv(config.validatePath, header=0, encoding='utf-8') x_val = pickle.load(open('word_indices_val.pkl', 'rb')) x_val = pad_sequences(x_val, maxlen) y_val = np.asarray(validate_df.iloc[:, 4] + 2) start = time.time() fast_pred = fast.predict(x_val) print('fast_time:{}'.format(time.time() - start)) merge_pred = copy.deepcopy(fast_pred) merge_pred = np.zeros(fast_pred.shape) start = time.time() cnn_pred = cnn.predict(x_val) print('cnn_time:{}'.format(time.time() - start)) merge_pred = np.add(merge_pred, cnn_pred) print(get_f1_score(y_val, np.argmax(merge_pred, axis=-1))) start = time.time() rnn_pred = rnn.predict(x_val) print('rnn_time:{}'.format(time.time() - start)) merge_pred = np.add(merge_pred, rnn_pred) print(get_f1_score(y_val, np.argmax(merge_pred, axis=-1))) start = time.time() rcnn_pred = rcnn.predict(x_val) print('rcnn_time:{}'.format(time.time() - start)) merge_pred = np.add(merge_pred, rcnn_pred) print(get_f1_score(y_val, np.argmax(merge_pred, axis=-1))) ''' pred = [] for i in range(len(y_val)):
writer.add_graph(model, dummy_input) model.train() for epoch in range(old_epoch, epochs+1): for data in tqdm.tqdm(train_loader): x_train, y_train = data optimizer.zero_grad() out = model(x_train) loss = criterion(out, y_train) loss.backward() optimizer.step() log = 'loss:{:.4f} epoch:{} step:{} num_data:{} ' \ 'f1_0:{:.4f} f1_1:{:.4f} f1_2:{:.4f} f1_3:{:.4f} f1_mean:{:.4f}' scores = get_f1_score(y_train, torch.argmax(out, dim=1)) logging.info(log.format(loss, epoch, step, batch_size*step, *scores)) writer.add_scalar('loss', loss, step) writer.add_scalar('f1_mean', scores[-1], step) writer.add_scalars('f1_single', {'f1_0':scores[0], 'f1_1':scores[1], 'f1_2':scores[2], 'f1_3':scores[3]}, step) if step % 20 == 0: torch.save({ 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), 'epoch': epoch, 'step': step }, 'cnn_ckpt.tar') print('model has been saved')
batch_x = batch_x.to(device) batch_y = batch_y.to(device) optimizer.zero_grad() pred = net(batch_x) loss = loss_function(pred, batch_y) loss.backward() optimizer.step() # 计算准确率 this_test_acc[i] = get_acc(net, test_x, test_y) this_train_acc[i] = get_acc(net, train_x, train_y) # 计算f1 this_test_f1[i] = get_f1_score(net, test_x, test_y) this_train_f1[i] = get_f1_score(net, train_x, train_y) mean_test_acc_list.append(this_test_acc.mean()) mean_train_acc_list.append(this_train_acc.mean()) np.save('test_acc_for_epoch_{}'.format(k_fold_time), np.array(mean_test_acc_list)) np.save('train_acc_for_epoch_{}'.format(k_fold_time), np.array(mean_train_acc_list)) mean_test_f1_list.append(this_test_f1.mean()) mean_train_f1_list.append(this_train_f1.mean()) np.save('test_f1_for_epoch_{}'.format(k_fold_time), np.array(mean_test_f1_list)) np.save('train_f1_for_epoch_{}'.format(k_fold_time), np.array(mean_train_f1_list))