@@ -162,20 +162,50 @@ def transform(self, X):
162
162
163
163
164
164
class BilinearMixin (BaseMetricLearner , metaclass = ABCMeta ):
165
+ r"""Bilinear similarity learning algorithms.
166
+
167
+ Algorithm that learns a Bilinear (pseudo) similarity :math:`s_M(x, x')`,
168
+ defined between two column vectors :math:`x` and :math:`x'` by: :math:
169
+ `s_M(x, x') = x M x'`, where :math:`M` is a learned matrix. This matrix
170
+ is not guaranteed to be symmetric nor positive semi-definite (PSD). Thus
171
+ it cannot be seen as learning a linear transformation of the original
172
+ space like Mahalanobis learning algorithms.
173
+
174
+ Attributes
175
+ ----------
176
+ components_ : `numpy.ndarray`, shape=(n_components, n_features)
177
+ The learned bilinear matrix ``M``.
178
+ """
165
179
166
180
def score_pairs (self , pairs ):
167
- r"""
181
+ r"""Returns the learned Bilinear similarity between pairs.
182
+
183
+ This similarity is defined as: :math:`s_M(x, x') = x M x'`
184
+ where ``M`` is the learned Bilinear matrix, for every pair of points
185
+ ``x`` and ``x'``.
186
+
168
187
Parameters
169
188
----------
170
189
pairs : array-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)
171
190
3D Array of pairs to score, with each row corresponding to two points,
172
- for 2D array of indices of pairs if the metric learner uses a
191
+ for 2D array of indices of pairs if the similarity learner uses a
173
192
preprocessor.
174
193
175
194
Returns
176
195
-------
177
196
scores : `numpy.ndarray` of shape=(n_pairs,)
178
- The learned Mahalanobis distance for every pair.
197
+ The learned Bilinear similarity for every pair.
198
+
199
+ See Also
200
+ --------
201
+ get_metric : a method that returns a function to compute the similarity
202
+ between two points. The difference with `score_pairs` is that it works
203
+ on two 1D arrays and cannot use a preprocessor. Besides, the returned
204
+ function is independent of the similarity learner and hence is not
205
+ modified if the similarity learner is.
206
+
207
+ :ref:`Bilinear_similarity` : The section of the project documentation
208
+ that describes Bilinear similarity.
179
209
"""
180
210
check_is_fitted (self , ['preprocessor_' , 'components_' ])
181
211
pairs = check_input (pairs , type_of_inputs = 'tuples' ,
@@ -184,36 +214,44 @@ def score_pairs(self, pairs):
184
214
# Note: For bilinear order matters, dist(a,b) != dist(b,a)
185
215
# We always choose first pair first, then second pair
186
216
# (In contrast with Mahalanobis implementation)
187
- return (np .dot (pairs [:, 0 , :], self .components_ ) * pairs [:, 1 , :]).sum (- 1 )
217
+ return np .sum (np .dot (pairs [:, 0 , :], self .components_ ) * pairs [:, 1 , :],
218
+ axis = - 1 )
188
219
189
220
def get_metric (self ):
190
221
check_is_fitted (self , 'components_' )
191
222
components = self .components_ .copy ()
192
223
193
- def metric_fun (u , v ):
194
- """This function computes the metric between u and v, according to the
195
- previously learned metric .
224
+ def similarity_fun (u , v ):
225
+ """This function computes the similarity between u and v, according to the
226
+ previously learned similarity .
196
227
197
228
Parameters
198
229
----------
199
230
u : array-like, shape=(n_features,)
200
- The first point involved in the distance computation.
231
+ The first point involved in the similarity computation.
201
232
202
233
v : array-like, shape=(n_features,)
203
- The second point involved in the distance computation.
234
+ The second point involved in the similarity computation.
204
235
205
236
Returns
206
237
-------
207
- distance : float
208
- The distance between u and v according to the new metric .
238
+ similarity : float
239
+ The similarity between u and v according to the new similarity .
209
240
"""
210
241
u = validate_vector (u )
211
242
v = validate_vector (v )
212
243
return np .dot (np .dot (u .T , components ), v )
213
244
214
- return metric_fun
245
+ return similarity_fun
215
246
216
247
def get_bilinear_matrix (self ):
248
+ """Returns a copy of the Bilinear matrix learned by the similarity learner.
249
+
250
+ Returns
251
+ -------
252
+ M : `numpy.ndarray`, shape=(n_features, n_features)
253
+ The copy of the learned Bilinear matrix.
254
+ """
217
255
check_is_fitted (self , 'components_' )
218
256
return self .components_
219
257
0 commit comments