You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _downloads/434a24c66bbfd5f8398470a8c859e27b/plot_metric_learning_examples.ipynb
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -367,7 +367,7 @@
367
367
},
368
368
"outputs": [],
369
369
"source": [
370
-
"def create_constraints(labels):\n import itertools\n import random\n \n # aggregate indices of same class\n zeros = np.where(y == 0)[0]\n ones = np.where(y == 1)[0]\n twos = np.where(y == 2)[0]\n # make permutations of all those points in the same class\n zeros_ = list(itertools.combinations(zeros, 2))\n ones_ = list(itertools.combinations(ones, 2))\n twos_ = list(itertools.combinations(twos, 2))\n # put them together!\n sim = np.array(zeros_ + ones_ + twos_)\n \n # similarily, put together indices in different classes\n dis = []\n for zero in zeros:\n for one in ones:\n dis.append((zero, one))\n for two in twos:\n dis.append((zero, two))\n for one in ones:\n for two in twos:\n dis.append((one, two))\n \n # pick up just enough dissimilar examples as we have similar examples\n dis = np.array(random.sample(dis, len(sim)))\n \n # return an array of pairs of indices of shape=(2*len(sim), 2), and the corresponding labels, array of shape=(2*len(sim))\n #\u00a0Each pair of similar points have a label of +1 and each pair of dissimilar points have a label of -1\n return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]), np.column_stack([dis[:, 0], dis[:, 1]])]),\n np.concatenate([np.ones(len(sim)), -np.ones(len(sim))]))\n\npairs, pairs_labels = create_constraints(y)"
370
+
"def create_constraints(labels):\n import itertools\n import random\n\n # aggregate indices of same class\n zeros = np.where(y == 0)[0]\n ones = np.where(y == 1)[0]\n twos = np.where(y == 2)[0]\n # make permutations of all those points in the same class\n zeros_ = list(itertools.combinations(zeros, 2))\n ones_ = list(itertools.combinations(ones, 2))\n twos_ = list(itertools.combinations(twos, 2))\n # put them together!\n sim = np.array(zeros_ + ones_ + twos_)\n\n # similarily, put together indices in different classes\n dis = []\n for zero in zeros:\n for one in ones:\n dis.append((zero, one))\n for two in twos:\n dis.append((zero, two))\n for one in ones:\n for two in twos:\n dis.append((one, two))\n\n # pick up just enough dissimilar examples as we have similar examples\n dis = np.array(random.sample(dis, len(sim)))\n\n # return an array of pairs of indices of shape=(2*len(sim), 2), and the\n # corresponding labels, array of shape=(2*len(sim))\n # Each pair of similar points have a label of +1 and each pair of\n # dissimilar points have a label of -1\n return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]),\n np.column_stack([dis[:, 0], dis[:, 1]])]),\n np.concatenate([np.ones(len(sim)), -np.ones(len(sim))]))\n\n\npairs, pairs_labels = create_constraints(y)"
0 commit comments