Pomiar podobieństwa z wektorów dystrybucyjnych
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Teraz masz wstępy dla dowolnej pary przykładów. Zastosowana metoda nadzorowana do pomiaru podobieństwa przyjmuje te wektory dystrybucyjne i zwraca liczbę, która mierzy ich podobieństwo.
Pamiętaj, że reprezentacje właściwościowe to wektory liczb. Aby znaleźć podobieństwo między 2 wektorami \(A = [a_1,a_2,...,a_n]\) i \(B = [b_1,b_2,...,b_n]\), wybierz jedną z tych 3 miar podobieństwa:
Pomiary | Znaczenie | Formuła |
Wraz ze wzrostem podobieństwa ta wartość... |
Odległość euklidesowa | Odległość między końcami wektorów |
\(\sqrt{(a_1-b_1)^2+(a_2-b_2)^2+...+(a_N-b_N)^2}\) |
Zmniejszenie |
Cosinus | Cosinus kąta \(\theta\) między wektorami |
\(\frac{a^T b}{|a| \cdot |b|}\) |
Zwiększanie |
Iloczyn skalarny | Cosinus pomnożony przez długości obu wektorów |
\(a_1b_1+a_2b_2+...+a_nb_n\) \(=|a||b|cos(\theta)\) |
Zwiększa. Wzrasta również wraz z długością wektorów. |
Wybieranie miary podobieństwa
W przeciwieństwie do cosinusa iloczyn skalarny jest proporcjonalny do długości wektora.
Jest to ważne, ponieważ przykłady, które występują bardzo często w zbiorze treningowym (np. popularne filmy w YouTube), mają zwykle duże wektory embeddingu.
Jeśli chcesz uwzględnić popularność, wybierz kropkę. Istnieje jednak ryzyko, że popularne przykłady mogą zafałszować dane o podobieństwie. Aby zrównoważyć to zniekształcenie, możesz podnieść długość do wykładnika \(\alpha\ < 1\) , aby obliczyć iloczyn kropek jako \(|a|^{\alpha}|b|^{\alpha}\cos(\theta)\).
Aby lepiej zrozumieć, jak długość wektora zmienia miarę podobieństwa, znormalizuj długości wektorów do 1 i zauważ, że te 3 miary stają się proporcjonalne względem siebie.
Po normalizacji wartości a i b tak, aby \(||a||=1\) i \(||b||=1\),
te 3 wartości są powiązane w ten sposób:
- Odległość euklidesowa = \(||a-b|| = \sqrt{||a||^2 + ||b||^2 - 2a^{T}b}
= \sqrt{2-2\cos(\theta_{ab})}\).
- Iloczyn skalarny = \( |a||b| \cos(\theta_{ab})
= 1\cdot1\cdot \cos(\theta_{ab}) = cos(\theta_{ab})\).
- Cosinus = \(\cos(\theta_{ab})\).
W związku z tym wszystkie 3 wskaźniki podobieństwa są równoważne, ponieważ są proporcjonalne do wartości \(cos(\theta_{ab})\).
Sprawdzanie podobieństwa
Miernik podobieństwa określa podobieństwo pary przykładów w stosunku do innych par przykładów. Poniżej porównujemy 2 rodzaje: ręczny i nadzorowany:
Typ | Jak utworzyć | Najlepsze zastosowania | Konsekwencje |
Ręcznie | Ręczne łączenie danych funkcji. |
małe zbiory danych z cechami, które łatwo połączyć; |
Umożliwia uzyskanie informacji o wynikach obliczeń podobieństwa. Jeśli dane funkcji ulegną zmianie, musisz ręcznie zaktualizować miarę podobieństwa. |
Nadzorowane | pomiar odległości między wektorami dystrybucyjnymi wygenerowanymi przez nadzorowany DNN; |
Duże zbiory danych z cechami, które trudno połączyć. |
nie daje żadnych informacji o wynikach, Sieć DNN może jednak automatycznie dostosowywać się do zmieniających się danych o cechach. |
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-02-25 UTC.
[null,null,["Ostatnia aktualizacja: 2025-02-25 UTC."],[[["\u003cp\u003eSupervised similarity measures leverage embeddings to quantify the similarity between data examples using Euclidean distance, cosine, or dot product.\u003c/p\u003e\n"],["\u003cp\u003eDot product incorporates vector length, reflecting popularity, while cosine similarity focuses solely on the angle between vectors, ignoring popularity.\u003c/p\u003e\n"],["\u003cp\u003eNormalizing vector lengths makes Euclidean distance, cosine, and dot product proportional, essentially measuring the same thing.\u003c/p\u003e\n"],["\u003cp\u003eSupervised similarity, using embeddings and a distance metric, is suitable for large, complex datasets, while manual similarity, relying on feature combinations, is better for small, straightforward datasets.\u003c/p\u003e\n"]]],[],null,["# Measuring similarity from embeddings\n\nYou now have embeddings for any pair of examples. A supervised similarity\nmeasure takes these embeddings and returns a number measuring their similarity.\nRemember that embeddings are vectors of numbers. To find the similarity between\ntwo vectors \\\\(A = \\[a_1,a_2,...,a_n\\]\\\\) and \\\\(B = \\[b_1,b_2,...,b_n\\]\\\\),\nchoose one of these three similarity measures:\n\n| Measure | Meaning | Formula | As similarity increases, this measure... |\n|--------------------|-----------------------------------------------|--------------------------------------------------------------|---------------------------------------------------|\n| Euclidean distance | Distance between ends of vectors | \\\\(\\\\sqrt{(a_1-b_1)\\^2+(a_2-b_2)\\^2+...+(a_N-b_N)\\^2}\\\\) | Decreases |\n| Cosine | Cosine of angle \\\\(\\\\theta\\\\) between vectors | \\\\(\\\\frac{a\\^T b}{\\|a\\| \\\\cdot \\|b\\|}\\\\) | Increases |\n| Dot product | Cosine multiplied by lengths of both vectors | \\\\(a_1b_1+a_2b_2+...+a_nb_n\\\\) \\\\(=\\|a\\|\\|b\\|cos(\\\\theta)\\\\) | Increases. Also increases with length of vectors. |\n\nChoosing a similarity measure\n-----------------------------\n\nIn contrast to the cosine, the dot product is proportional to the vector length.\nThis is important because examples that appear very frequently in the training\nset (for example, popular YouTube videos) tend to have embedding vectors with\nlarge lengths.\n\nIf you\nwant to capture popularity, then choose dot product. However, the risk is that\npopular examples may skew the similarity metric. To balance this skew, you can\nraise the length to an exponent \\\\(\\\\alpha\\\\ \\\u003c 1\\\\) to calculate the dot product\nas \\\\(\\|a\\|\\^{\\\\alpha}\\|b\\|\\^{\\\\alpha}\\\\cos(\\\\theta)\\\\).\n\nTo better understand how vector length changes the similarity measure, normalize\nthe vector lengths to 1 and notice that the three measures become proportional\nto each other. \nProof: Proportionality of Similarity Measures \nAfter normalizing a and b such that \\\\(\\|\\|a\\|\\|=1\\\\) and \\\\(\\|\\|b\\|\\|=1\\\\), these three measures are related as:\n\n- Euclidean distance = \\\\(\\|\\|a-b\\|\\| = \\\\sqrt{\\|\\|a\\|\\|\\^2 + \\|\\|b\\|\\|\\^2 - 2a\\^{T}b} = \\\\sqrt{2-2\\\\cos(\\\\theta_{ab})}\\\\).\n- Dot product = \\\\( \\|a\\|\\|b\\| \\\\cos(\\\\theta_{ab}) = 1\\\\cdot1\\\\cdot \\\\cos(\\\\theta_{ab}) = cos(\\\\theta_{ab})\\\\).\n- Cosine = \\\\(\\\\cos(\\\\theta_{ab})\\\\).\nThus, all three similarity measures are equivalent because they are proportional to \\\\(cos(\\\\theta_{ab})\\\\).\n\nReview of similarity measures\n-----------------------------\n\nA similarity measure quantifies the similarity between a pair of\nexamples, relative to other pairs of examples. The two types, manual and\nsupervised, are compared below:\n\n| Type | How to create | Best for | Implications |\n|------------|--------------------------------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|\n| Manual | Manually combine feature data. | Small datasets with features that are straightforward to combine. | Gives insight into results of similarity calculations. If feature data changes, you must manually update the similarity measure. |\n| Supervised | Measure distance between embeddings generated by a supervised DNN. | Large datasets with hard-to-combine features. | Gives no insight into results. However, a DNN can automatically adapt to changing feature data. |"]]