(self, Cui, show_progress=True, callback=None)
| 74 | super().__init__() |
| 75 | |
| 76 | def fit(self, Cui, show_progress=True, callback=None): |
| 77 | self.model.fit(Cui, show_progress, callback=callback) |
| 78 | |
| 79 | item_factors = self.model.item_factors |
| 80 | if implicit.gpu.HAS_CUDA and isinstance(item_factors, implicit.gpu.Matrix): |
| 81 | item_factors = item_factors.to_numpy() |
| 82 | item_factors = item_factors.astype("float32") |
| 83 | |
| 84 | self.factors = item_factors.shape[1] |
| 85 | |
| 86 | self.quantizer = faiss.IndexFlat(self.factors) |
| 87 | |
| 88 | if self.use_gpu: |
| 89 | self.gpu_resources = faiss.StandardGpuResources() |
| 90 | |
| 91 | if self.approximate_recommend: |
| 92 | log.debug("Building faiss recommendation index") |
| 93 | |
| 94 | # build up a inner product index here |
| 95 | if self.use_gpu: |
| 96 | index = faiss.GpuIndexIVFFlat( |
| 97 | self.gpu_resources, self.factors, self.nlist, faiss.METRIC_INNER_PRODUCT |
| 98 | ) |
| 99 | else: |
| 100 | index = faiss.IndexIVFFlat( |
| 101 | self.quantizer, self.factors, self.nlist, faiss.METRIC_INNER_PRODUCT |
| 102 | ) |
| 103 | |
| 104 | index.train(item_factors) |
| 105 | index.add(item_factors) |
| 106 | index.nprobe = self.nprobe |
| 107 | self.recommend_index = index |
| 108 | |
| 109 | if self.approximate_similar_items: |
| 110 | log.debug("Building faiss similar items index") |
| 111 | |
| 112 | # likewise build up cosine index for similar_items, using an inner product |
| 113 | # index on normalized vectors` |
| 114 | norms = np.linalg.norm(item_factors, axis=1) |
| 115 | norms[norms == 0] = 1e-10 |
| 116 | |
| 117 | normalized = (item_factors.T / norms).T.astype("float32") |
| 118 | if self.use_gpu: |
| 119 | index = faiss.GpuIndexIVFFlat( |
| 120 | self.gpu_resources, self.factors, self.nlist, faiss.METRIC_INNER_PRODUCT |
| 121 | ) |
| 122 | else: |
| 123 | index = faiss.IndexIVFFlat( |
| 124 | self.quantizer, self.factors, self.nlist, faiss.METRIC_INNER_PRODUCT |
| 125 | ) |
| 126 | |
| 127 | index.train(normalized) |
| 128 | index.add(normalized) |
| 129 | index.nprobe = self.nprobe |
| 130 | self.similar_items_index = index |
| 131 | |
| 132 | def similar_items( |
| 133 | self, itemid, N=10, recalculate_item=False, item_users=None, filter_items=None, items=None |
no outgoing calls