(model, data_generator, Ks)
| 50 | |
| 51 | |
| 52 | def eval_PyTorch(model, data_generator, Ks): |
| 53 | result = {'recall': np.zeros(len(Ks)), 'ndcg': np.zeros(len(Ks))} |
| 54 | |
| 55 | test_users = list(data_generator.test_set.keys()) |
| 56 | |
| 57 | u_batch_size = data_generator.batch_size |
| 58 | |
| 59 | n_test_users = len(test_users) |
| 60 | n_user_batchs = n_test_users // u_batch_size + 1 |
| 61 | |
| 62 | batch_rating_list = [] |
| 63 | ground_truth_list = [] |
| 64 | count = 0 |
| 65 | for u_batch_id in range(n_user_batchs): |
| 66 | start = u_batch_id * u_batch_size |
| 67 | end = (u_batch_id + 1) * u_batch_size |
| 68 | |
| 69 | user_batch = test_users[start: end] |
| 70 | rate_batch = model.predict(user_batch) |
| 71 | |
| 72 | count += rate_batch.shape[0] |
| 73 | |
| 74 | exclude_index = [] |
| 75 | exclude_items = [] |
| 76 | ground_truth = [] |
| 77 | for i in range(len(user_batch)): |
| 78 | train_items = list(data_generator.train_items[user_batch[i]]) |
| 79 | exclude_index.extend([i] * len(train_items)) |
| 80 | exclude_items.extend(train_items) |
| 81 | ground_truth.append(list(data_generator.test_set[user_batch[i]])) |
| 82 | rate_batch[exclude_index, exclude_items] = -(1 << 20) |
| 83 | _, rate_batch_k = torch.topk(rate_batch, k=max(Ks)) |
| 84 | batch_rating_list.append(rate_batch_k.cpu()) |
| 85 | ground_truth_list.append(ground_truth) |
| 86 | |
| 87 | X = zip(batch_rating_list, ground_truth_list) |
| 88 | batch_results = [] |
| 89 | for x in X: |
| 90 | batch_results.append(test_one_batch(x, Ks)) |
| 91 | for batch_result in batch_results: |
| 92 | result['recall'] += batch_result['recall'] / n_test_users |
| 93 | result['ndcg'] += batch_result['ndcg'] / n_test_users |
| 94 | |
| 95 | assert count == n_test_users |
| 96 | |
| 97 | return result |
no test coverage detected