| 90 | |
| 91 | |
| 92 | def eval(sess, test_data, model, model_path): |
| 93 | |
| 94 | loss_sum = 0. |
| 95 | accuracy_sum = 0. |
| 96 | aux_loss_sum = 0. |
| 97 | nums = 0 |
| 98 | stored_arr = [] |
| 99 | for src, tgt in test_data: |
| 100 | nums += 1 |
| 101 | uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data( |
| 102 | src, tgt, return_neg=True) |
| 103 | prob, loss, acc, aux_loss = model.calculate(sess, [ |
| 104 | uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, |
| 105 | noclk_mids, noclk_cats |
| 106 | ]) |
| 107 | loss_sum += loss |
| 108 | aux_loss_sum = aux_loss |
| 109 | accuracy_sum += acc |
| 110 | prob_1 = prob[:, 0].tolist() |
| 111 | target_1 = target[:, 0].tolist() |
| 112 | for p, t in zip(prob_1, target_1): |
| 113 | stored_arr.append([p, t]) |
| 114 | test_auc = calc_auc(stored_arr) |
| 115 | accuracy_sum = accuracy_sum / nums |
| 116 | loss_sum = loss_sum / nums |
| 117 | aux_loss_sum / nums |
| 118 | global best_auc |
| 119 | global best_case_acc |
| 120 | if best_auc < test_auc: |
| 121 | best_auc = test_auc |
| 122 | best_case_acc = accuracy_sum |
| 123 | model.save(sess, model_path) |
| 124 | return test_auc, loss_sum, accuracy_sum, aux_loss_sum |
| 125 | |
| 126 | |
| 127 | def train(data_location='data', |