Drops unnecessary documents from the reference embeddings and updates the list of reference documents, and then recomputes the adjacency matrix. Parameters: qa_embs (numpy array): The embedding matrix of QA pairs. ref_embs (numpy array): The embedding matrix of reference senten
(qa_embs, ref_embs, ref_docs, threshold=0.1)
| 81 | |
| 82 | |
| 83 | def prune_ref_docs(qa_embs, ref_embs, ref_docs, threshold=0.1): |
| 84 | """ |
| 85 | Drops unnecessary documents from the reference embeddings and updates the list of reference documents, |
| 86 | and then recomputes the adjacency matrix. |
| 87 | |
| 88 | Parameters: |
| 89 | qa_embs (numpy array): The embedding matrix of QA pairs. |
| 90 | ref_embs (numpy array): The embedding matrix of reference sentences. |
| 91 | ref_docs (list): The list of reference documents. |
| 92 | threshold (float): The threshold below which documents are considered unnecessary. |
| 93 | |
| 94 | Returns: |
| 95 | pruned_ref_embs (numpy array): The pruned embedding matrix of reference sentences. |
| 96 | pruned_ref_docs (list): The pruned list of reference documents. |
| 97 | pruned_A (numpy array): The pruned adjacency matrix. |
| 98 | """ |
| 99 | |
| 100 | # Compute the initial adjacency matrix with full reference embeddings |
| 101 | A = gaussian_kernel_torch(qa_embs, ref_embs, sigma=0.5) |
| 102 | print(f"Before: {A.shape}") |
| 103 | # Compute the row-wise sum of the adjacency matrix |
| 104 | row_sum = torch.sum(A, dim=0) |
| 105 | |
| 106 | # Identify the indexes of the relevant documents |
| 107 | relevant_idx = torch.where(row_sum > threshold * row_sum.max())[0] |
| 108 | |
| 109 | # Drop unnecessary rows from the reference embeddings |
| 110 | pruned_ref_embs = ref_embs[relevant_idx] |
| 111 | |
| 112 | # Update the list of reference documents |
| 113 | pruned_ref_docs = [ref_docs[i] for i in relevant_idx] |
| 114 | |
| 115 | # Recompute the adjacency matrix with pruned reference embeddings |
| 116 | pruned_A = gaussian_kernel_torch(qa_embs, pruned_ref_embs, sigma=0.5) |
| 117 | print(f"After: {pruned_A.shape}") |
| 118 | return pruned_ref_embs, pruned_ref_docs, pruned_A |
| 119 | |
| 120 | |
| 121 | def compute_kernel_by_type(embs, threshold=0.65, kernel_type="cosine", sigma=1.0): |
nothing calls this directly
no test coverage detected