{"id":1852,"date":"2023-04-21T06:58:41","date_gmt":"2023-04-21T06:58:41","guid":{"rendered":"https:\/\/feellikelearning.com\/?p=1852"},"modified":"2023-04-21T06:58:41","modified_gmt":"2023-04-21T06:58:41","slug":"how-to-compute-cosine-similarity-python-example","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/","title":{"rendered":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b"},"content":{"rendered":"\n<h2>1 \u4f7f\u7528Tensorflow \/ Keras<\/h2>\n\n\n\n<p>Keras\u63d0\u4f9b\u7684loss function\u6709cosine similarity<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tensorflow as tf\nimport math\n\nvector1 = tf.constant([1, 2, 3], dtype=tf.float32)\nvector2 = tf.constant([1, 2, 3], dtype=tf.float32)\n\nprint(f'similarity type: {type(similarity)}')\nprint(f'similarity: {similarity}')  # Output: 0.97463185\nprint(f'similarity = {similarity.numpy()}')  # Output: 0.97463185<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">similarity type: &lt;class 'tensorflow.python.framework.ops.EagerTensor'>\nsimilarity: -0.9999998807907104\nsimilarity = -0.9999998807907104\n<\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cKeras\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u662f\u8d1f\u7684\u3002\u56e0\u4e3a\u5b83\u662f\u4f5c\u4e3a\u4e00\u79cdloss function\uff0c\u5982\u679c2\u4e2a\u5411\u91cf\u8d8a\u76f8\u4f3c\uff0c\u4ed6\u4eec\u8d8a\u63a5\u8fd11\uff0c\u6240\u4ee5loss\u8d8a\u5c0f\uff0c\u90a3\u4e48Keras\u5c31\u628a\u4e00\u822c\u610f\u4e49\u4e0a\u7684Consine similarity\u52a0\u4e86\u4e2a\u8d1f\u53f7\u3002<\/p>\n\n\n\n<h2>2 \u53ea\u4f7f\u7528Raw Python<\/h2>\n\n\n\n<p>\u53ea\u7528Raw Python\u5199\u4e2a\u51fd\u6570\u4e5f\u4e0d\u96be\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def cosine_similarity(x, y):\n    dot_product = sum(i*j for i, j in zip(x, y))\n    norm_x = math.sqrt(sum(i*i for i in x))\n    norm_y = math.sqrt(sum(i*i for i in y))\n    return dot_product \/ (norm_x * norm_y)\n\nsimilarity2 = cosine_similarity([1,2,3], [4, 5, 6])\n\nprint(f'similarity2 = {similarity2}')<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">similarity2 = 0.9746318461970762<\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u6ca1\u6709\u8d1f\u6570\u95ee\u9898\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u4f7f\u7528Tensorflow \/ Keras Keras\u63d0\u4f9b\u7684loss function\u6709cosine similarity \u8f93\u51fa\u7ed3\u679c \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cKeras\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u662f\u8d1f\u7684\u3002\u56e0\u4e3a\u5b83\u662f\u4f5c\u4e3a\u4e00\u79cdloss function\uff0c\u5982\u679c2\u4e2a\u5411\u91cf\u8d8a\u76f8\u4f3c\uff0c\u4ed6\u4eec\u8d8a\u63a5\u8fd11\uff0c\u6240\u4ee5loss\u8d8a\u5c0f\uff0c\u90a3\u4e48Keras\u5c31\u628a\u4e00\u822c\u610f\u4e49\u4e0a\u7684Consine similarity\u52a0\u4e86\u4e2a\u8d1f\u53f7\u3002 2 \u53ea\u4f7f\u7528Raw Python \u53ea\u7528Raw Python\u5199\u4e2a\u51fd\u6570\u4e5f\u4e0d\u96be\uff0c\u4ee3\u7801\u5982\u4e0b \u8f93\u51fa\u7ed3\u679c \u8fd9\u6837\u5c31\u6ca1\u6709\u8d1f\u6570\u95ee\u9898\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[10,7,3],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"1 \u4f7f\u7528Tensorflow \/ Keras Keras\u63d0\u4f9b\u7684loss function\u6709cosine similarity \u8f93\u51fa\u7ed3\u679c \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cKeras\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u662f\u8d1f\u7684\u3002\u56e0\u4e3a\u5b83\u662f\u4f5c\u4e3a\u4e00\u79cdloss function\uff0c\u5982\u679c2\u4e2a\u5411\u91cf\u8d8a\u76f8\u4f3c\uff0c\u4ed6\u4eec\u8d8a\u63a5\u8fd11\uff0c\u6240\u4ee5loss\u8d8a\u5c0f\uff0c\u90a3\u4e48Keras\u5c31\u628a\u4e00\u822c\u610f\u4e49\u4e0a\u7684Consine similarity\u52a0\u4e86\u4e2a\u8d1f\u53f7\u3002 2 \u53ea\u4f7f\u7528Raw Python \u53ea\u7528Raw Python\u5199\u4e2a\u51fd\u6570\u4e5f\u4e0d\u96be\uff0c\u4ee3\u7801\u5982\u4e0b \u8f93\u51fa\u7ed3\u679c \u8fd9\u6837\u5c31\u6ca1\u6709\u8d1f\u6570\u95ee\u9898\u4e86\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-21T06:58:41+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b\",\"datePublished\":\"2023-04-21T06:58:41+00:00\",\"dateModified\":\"2023-04-21T06:58:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\"},\"wordCount\":21,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"articleSection\":[\"Python\",\"\u673a\u5668\u5b66\u4e60\",\"\u7f16\u7a0b\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\",\"name\":\"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2023-04-21T06:58:41+00:00\",\"dateModified\":\"2023-04-21T06:58:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/","og_locale":"en_US","og_type":"article","og_title":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning","og_description":"1 \u4f7f\u7528Tensorflow \/ Keras Keras\u63d0\u4f9b\u7684loss function\u6709cosine similarity \u8f93\u51fa\u7ed3\u679c \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cKeras\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u662f\u8d1f\u7684\u3002\u56e0\u4e3a\u5b83\u662f\u4f5c\u4e3a\u4e00\u79cdloss function\uff0c\u5982\u679c2\u4e2a\u5411\u91cf\u8d8a\u76f8\u4f3c\uff0c\u4ed6\u4eec\u8d8a\u63a5\u8fd11\uff0c\u6240\u4ee5loss\u8d8a\u5c0f\uff0c\u90a3\u4e48Keras\u5c31\u628a\u4e00\u822c\u610f\u4e49\u4e0a\u7684Consine similarity\u52a0\u4e86\u4e2a\u8d1f\u53f7\u3002 2 \u53ea\u4f7f\u7528Raw Python \u53ea\u7528Raw Python\u5199\u4e2a\u51fd\u6570\u4e5f\u4e0d\u96be\uff0c\u4ee3\u7801\u5982\u4e0b \u8f93\u51fa\u7ed3\u679c \u8fd9\u6837\u5c31\u6ca1\u6709\u8d1f\u6570\u95ee\u9898\u4e86\u3002","og_url":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/","og_site_name":"Feel Like Learning","article_published_time":"2023-04-21T06:58:41+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b","datePublished":"2023-04-21T06:58:41+00:00","dateModified":"2023-04-21T06:58:41+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/"},"wordCount":21,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"articleSection":["Python","\u673a\u5668\u5b66\u4e60","\u7f16\u7a0b"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/","url":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/","name":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2023-04-21T06:58:41+00:00","dateModified":"2023-04-21T06:58:41+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/04\/21\/how-to-compute-cosine-similarity-python-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"\u600e\u6837\u8ba1\u7b97cosine similarity? Python\u5b9e\u4f8b"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1852"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1852"}],"version-history":[{"count":2,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1852\/revisions"}],"predecessor-version":[{"id":1854,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1852\/revisions\/1854"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}