{"id":3122,"date":"2024-04-13T21:20:05","date_gmt":"2024-04-13T13:20:05","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=3122"},"modified":"2024-04-28T19:55:54","modified_gmt":"2024-04-28T11:55:54","slug":"openai-%e4%bd%bf%e7%94%a8%e5%b5%8c%e5%85%a5%e5%92%8c%e6%9c%80%e8%bf%91%e9%82%bb%e6%90%9c%e7%b4%a2%e7%9a%84%e5%bb%ba%e8%ae%ae","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=3122","title":{"rendered":"OpenAI \u4f7f\u7528\u5d4c\u5165\u548c\u6700\u8fd1\u90bb\u641c\u7d22\u7684\u5efa\u8bae"},"content":{"rendered":"\n<p>\u5efa\u8bae\u5728\u7f51\u7edc\u4e0a\u5e7f\u4e3a\u6d41\u4f20\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201c\u4e70\u4e86\u90a3\u4ef6\u4e1c\u897f\uff1f\u8bd5\u8bd5\u8fd9\u4e9b\u7c7b\u4f3c\u7684\u9879\u76ee\u3002<\/li>\n\n\n\n<li>\u201c\u559c\u6b22\u90a3\u672c\u4e66\u5417\uff1f\u8bd5\u8bd5\u8fd9\u4e9b\u7c7b\u4f3c\u7684\u6807\u9898\u3002<\/li>\n\n\n\n<li>\u201c\u4e0d\u662f\u60a8\u8981\u627e\u7684\u5e2e\u52a9\u9875\u9762\uff1f\u8bd5\u8bd5\u8fd9\u4e9b\u7c7b\u4f3c\u7684\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6b64\u7b14\u8bb0\u672c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u5d4c\u5165\u6765\u67e5\u627e\u8981\u63a8\u8350\u7684\u7c7b\u4f3c\u9879\u76ee\u3002\u7279\u522b\u662f\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;<a href=\"http:\/\/groups.di.unipi.it\/~gulli\/AG_corpus_of_news_articles.html\">AG \u7684\u65b0\u95fb\u6587\u7ae0\u8bed\u6599\u5e93<\/a>\u4f5c\u4e3a\u6211\u4eec\u7684\u6570\u636e\u96c6\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684\u6a21\u578b\u5c06\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7bc7\u6587\u7ae0\uff0c\u8fd8\u6709\u54ea\u4e9b\u6587\u7ae0\u4e0e\u5b83\u6700\u76f8\u4f3c\uff1f<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport pickle\n\nfrom embeddings_utils import (\n    get_embedding,\n    distances_from_embeddings,\n    tsne_components_from_embeddings,\n    chart_from_components,\n    indices_of_nearest_neighbors_from_distances,\n)\n\nEMBEDDING_MODEL = \"text-embedding-3-small\"\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u52a0\u8f7d AG \u65b0\u95fb\u6570\u636e\uff0c\u770b\u770b\u5b83\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/openai\/openai-cookbook\/blob\/main\/examples\/data\/AG_news_samples.csv\">openai-cookbook\/examples\/data\/AG_news_samples.csv at main \u00b7 openai\/openai-cookbook (github.com)<\/a><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># load data (full dataset available at http:\/\/groups.di.unipi.it\/~gulli\/AG_corpus_of_news_articles.html)\ndataset_path = \"data\/AG_news_samples.csv\"\ndf = pd.read_csv(dataset_path)\n\nn_examples = 5\ndf.head(n_examples)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"217\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-22-1024x217.png\" alt=\"\" class=\"wp-image-3129\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-22-1024x217.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-22-300x63.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-22-768x162.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-22.png 1295w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u8fd9\u4e9b\u76f8\u540c\u7684\u793a\u4f8b\uff0c\u4f46\u6ca1\u6709\u88ab\u7701\u7565\u53f7\u622a\u65ad\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># print the title, description, and label of each example\nfor idx, row in df.head(n_examples).iterrows():\n    print(\"\")\n    print(f\"Title: {row['title']}\")\n    print(f\"Description: {row['description']}\")\n    print(f\"Label: {row['label']}\")\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"359\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23-1024x359.png\" alt=\"\" class=\"wp-image-3131\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23-1024x359.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23-300x105.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23-768x269.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23-1536x539.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-23.png 1654w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-build-cache-to-save-embeddings\"><strong>2. \u6784\u5efa\u7f13\u5b58\u4ee5\u4fdd\u5b58\u5d4c\u5165<\/strong><\/h3>\n\n\n\n<p>\u5728\u83b7\u53d6\u8fd9\u4e9b\u6587\u7ae0\u7684\u5d4c\u5165\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u7f13\u5b58\u6765\u4fdd\u5b58\u6211\u4eec\u751f\u6210\u7684\u5d4c\u5165\u3002\u901a\u5e38\uff0c\u6700\u597d\u4fdd\u5b58\u5d4c\u5165\u5185\u5bb9\uff0c\u4ee5\u4fbf\u4ee5\u540e\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u5b83\u4eec\u3002\u5982\u679c\u60a8\u4e0d\u4fdd\u5b58\u5b83\u4eec\uff0c\u5219\u6bcf\u6b21\u518d\u6b21\u8ba1\u7b97\u5b83\u4eec\u65f6\u90fd\u4f1a\u518d\u6b21\u4ed8\u6b3e\u3002<\/p>\n\n\n\n<p>\u7f13\u5b58\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u5c06\u5143\u7ec4<code>(text, model)<\/code>\u6620\u5c04\u5230\u5d4c\u5165\uff0c\u5d4c\u5165\u662f\u6d6e\u70b9\u6570\u7684\u5217\u8868\u3002\u7f13\u5b58\u4fdd\u5b58\u4e3a Python pickle \u6587\u4ef6\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># establish a cache of embeddings to avoid recomputing\n# cache is a dict of tuples (text, model) -&gt; embedding, saved as a pickle file\n\n# set path to embedding cache\nembedding_cache_path = \"data\/recommendations_embeddings_cache.pkl\"\n\n# load the cache if it exists, and save a copy to disk\ntry:\n    embedding_cache = pd.read_pickle(embedding_cache_path)\nexcept FileNotFoundError:\n    embedding_cache = {}\nwith open(embedding_cache_path, \"wb\") as embedding_cache_file:\n    pickle.dump(embedding_cache, embedding_cache_file)\n\n# define a function to retrieve embeddings from the cache if present, and otherwise request via the API\ndef embedding_from_string(\n    string: str,\n    model: str = EMBEDDING_MODEL,\n    embedding_cache=embedding_cache\n) -&gt; list:\n    \"\"\"Return embedding of given string, using a cache to avoid recomputing.\"\"\"\n    if (string, model) not in embedding_cache.keys():\n        embedding_cache[(string, model)] = get_embedding(string, model)\n        with open(embedding_cache_path, \"wb\") as embedding_cache_file:\n            pickle.dump(embedding_cache, embedding_cache_file)\n    return embedding_cache[(string, model)]\n<\/pre><\/div>\n\n\n\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u5d4c\u5165\u6765\u68c0\u67e5\u5b83\u662f\u5426\u6709\u6548\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># as an example, take the first description from the dataset\nexample_string = df[\"description\"].values[0]\nprint(f\"\\nExample string: {example_string}\")\n\n# print the first 10 dimensions of the embedding\nexample_embedding = embedding_from_string(example_string)\nprint(f\"\\nExample embedding: {example_embedding[:10]}...\")\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"97\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24-1024x97.png\" alt=\"\" class=\"wp-image-3134\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24-1024x97.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24-300x29.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24-768x73.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24-1536x146.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-24.png 1660w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-recommend-similar-articles-based-on-embeddings\"><strong>3. \u57fa\u4e8e\u5d4c\u5165\u63a8\u8350\u7c7b\u4f3c\u6587\u7ae0<\/strong><\/h2>\n\n\n\n<p>\u8981\u67e5\u627e\u7c7b\u4f3c\u7684\u6587\u7ae0\uff0c\u8ba9\u6211\u4eec\u9075\u5faa\u4e09\u6b65\u8ba1\u5212\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u83b7\u53d6\u6240\u6709\u6587\u7ae0\u63cf\u8ff0\u7684\u76f8\u4f3c\u6027\u5d4c\u5165<\/li>\n\n\n\n<li>\u8ba1\u7b97\u6e90\u6807\u9898\u4e0e\u6240\u6709\u5176\u4ed6\u6587\u7ae0\u4e4b\u95f4\u7684\u8ddd\u79bb<\/li>\n\n\n\n<li>\u6253\u5370\u51fa\u6700\u63a5\u8fd1\u6e90\u6807\u9898\u7684\u5176\u4ed6\u6587\u7ae0<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >def print_recommendations_from_strings(\n    strings: list[str],\n    index_of_source_string: int,\n    k_nearest_neighbors: int = 1,\n    model=EMBEDDING_MODEL,\n) -&gt; list[int]:\n    \"\"\"Print out the k nearest neighbors of a given string.\"\"\"\n    # get embeddings for all strings\n    embeddings = [embedding_from_string(string, model=model) for string in strings]\n\n    # get the embedding of the source string\n    query_embedding = embeddings[index_of_source_string]\n\n    # get distances between the source embedding and other embeddings (function from utils.embeddings_utils.py)\n    distances = distances_from_embeddings(query_embedding, embeddings, distance_metric=\"cosine\")\n    \n    # get indices of nearest neighbors (function from utils.utils.embeddings_utils.py)\n    indices_of_nearest_neighbors = indices_of_nearest_neighbors_from_distances(distances)\n\n    # print out source string\n    query_string = strings[index_of_source_string]\n    print(f\"Source string: {query_string}\")\n    # print out its k nearest neighbors\n    k_counter = 0\n    for i in indices_of_nearest_neighbors:\n        # skip any strings that are identical matches to the starting string\n        if query_string == strings[i]:\n            continue\n        # stop after printing out k articles\n        if k_counter &gt;= k_nearest_neighbors:\n            break\n        k_counter += 1\n\n        # print out the similar strings and their distances\n        print(\n            f\"\"\"\n        --- Recommendation #{k_counter} (nearest neighbor {k_counter} of {k_nearest_neighbors}) ---\n        String: {strings[i]}\n        Distance: {distances[i]:0.3f}\"\"\"\n        )\n\n    return indices_of_nearest_neighbors\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-example-recommendations\"><strong>4. \u793a\u4f8b\u5efa\u8bae<\/strong><\/h2>\n\n\n\n<p>\u8ba9\u6211\u4eec\u5bfb\u627e\u4e0e\u7b2c\u4e00\u7bc7\u7c7b\u4f3c\u7684\u6587\u7ae0\uff0c\u8fd9\u662f\u5173\u4e8e\u6258\u5c3c\u00b7\u5e03\u83b1\u5c14\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >article_descriptions = df[\"description\"].tolist()\n\ntony_blair_articles = print_recommendations_from_strings(\n    strings=article_descriptions,  # let's base similarity off of the article description\n    index_of_source_string=0,  # articles similar to the first one about Tony Blair\n    k_nearest_neighbors=5,  # 5 most similar articles\n)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p>\u633a\u597d\u7684\uff015\u9879\u5efa\u8bae\u4e2d\u67094\u9879\u660e\u786e\u63d0\u5230\u4e86\u6258\u5c3c\u00b7\u5e03\u83b1\u5c14\uff0c\u7b2c\u4e94\u9879\u662f\u4f26\u6566\u5173\u4e8e\u6c14\u5019\u53d8\u5316\u7684\u6587\u7ae0\uff0c\u8fd9\u4e9b\u8bdd\u9898\u53ef\u80fd\u7ecf\u5e38\u4e0e\u6258\u5c3c\u00b7\u5e03\u83b1\u5c14\u6709\u5173\u3002<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u6211\u4eec\u7684\u63a8\u8350\u8005\u5728\u7b2c\u4e8c\u7bc7\u5173\u4e8eNVIDIA\u65b0\u82af\u7247\u7ec4\u7684\u793a\u4f8b\u6587\u7ae0\u4e2d\u7684\u8868\u73b0\uff0c\u8be5\u82af\u7247\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >chipset_security_articles = print_recommendations_from_strings(\n    strings=article_descriptions,  # let's base similarity off of the article description\n    index_of_source_string=1,  # let's look at articles similar to the second one about a more secure chipset\n    k_nearest_neighbors=5,  # let's look at the 5 most similar articles\n)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ece\u6253\u5370\u7684\u8ddd\u79bb\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230 #1 \u63a8\u8350\u6bd4\u5176\u4ed6\u6240\u6709\u63a8\u8350\u66f4\u63a5\u8fd1\uff080.11 \u4e0e 0.14+\uff09\u3002#1 \u5efa\u8bae\u770b\u8d77\u6765\u4e0e\u8d77\u59cb\u6587\u7ae0\u975e\u5e38\u76f8\u4f3c &#8211; \u8fd9\u662f PC World \u5173\u4e8e\u63d0\u9ad8\u8ba1\u7b97\u673a\u5b89\u5168\u6027\u7684\u53e6\u4e00\u7bc7\u6587\u7ae0\u3002\u633a\u597d\u7684\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"appendix-using-embeddings-in-more-sophisticated-recommenders\"><strong>\u9644\u5f55\uff1a\u5728\u66f4\u590d\u6742\u7684\u63a8\u8350\u5668\u4e2d\u4f7f\u7528\u5d4c\u5165<\/strong><\/h2>\n\n\n\n<p>\u6784\u5efa\u63a8\u8350\u7cfb\u7edf\u7684\u4e00\u79cd\u66f4\u590d\u6742\u7684\u65b9\u6cd5\u662f\u8bad\u7ec3\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u8be5\u6a21\u578b\u63a5\u6536\u6570\u5341\u6216\u6570\u767e\u4e2a\u4fe1\u53f7\uff0c\u4f8b\u5982\u9879\u76ee\u53d7\u6b22\u8fce\u7a0b\u5ea6\u6216\u7528\u6237\u70b9\u51fb\u6570\u636e\u3002\u5373\u4f7f\u5728\u8fd9\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u5d4c\u5165\u4e5f\u53ef\u4ee5\u6210\u4e3a\u63a8\u8350\u5668\u4e2d\u975e\u5e38\u6709\u7528\u7684\u4fe1\u53f7\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5c1a\u672a\u201c\u51b7\u542f\u52a8\u201d\u4e14\u5c1a\u672a\u83b7\u5f97\u7528\u6237\u6570\u636e\u7684\u9879\u76ee\uff08\u4f8b\u5982\uff0c\u5728\u6ca1\u6709\u4efb\u4f55\u70b9\u51fb\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u5230\u76ee\u5f55\u4e2d\u7684\u5168\u65b0\u4ea7\u54c1\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"appendix-using-embeddings-to-visualize-similar-articles\">\u9644\u5f55\uff1a\u4f7f\u7528\u5d4c\u5165\u53ef\u89c6\u5316\u7c7b\u4f3c\u6587\u7ae0<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u4e86\u89e3\u6211\u4eec\u6700\u8fd1\u7684\u90bb\u5c45\u63a8\u8350\u5668\u5728\u505a\u4ec0\u4e48\uff0c\u8ba9\u6211\u4eec\u53ef\u89c6\u5316\u6587\u7ae0\u5d4c\u5165\u3002\u867d\u7136\u6211\u4eec\u65e0\u6cd5\u7ed8\u5236\u6bcf\u4e2a\u5d4c\u5165\u5411\u91cf\u7684 2048 \u4e2a\u7ef4\u5ea6\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/T-distributed_stochastic_neighbor_embedding\">t-SNE<\/a>&nbsp;\u6216&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Principal_component_analysis\">PCA<\/a>&nbsp;\u7b49\u6280\u672f\u5c06\u5d4c\u5165\u538b\u7f29\u4e3a 2 \u6216 3 \u4e2a\u7ef4\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed8\u5236\u8fd9\u4e9b\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<p>\u5728\u53ef\u89c6\u5316\u6700\u8fd1\u90bb\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528 t-SNE \u53ef\u89c6\u5316\u6240\u6709\u6587\u7ae0\u63cf\u8ff0\u3002\u8bf7\u6ce8\u610f\uff0ct-SNE \u4e0d\u662f\u786e\u5b9a\u6027\u7684\uff0c\u8fd9\u610f\u5473\u7740\u7ed3\u679c\u53ef\u80fd\u56e0\u8fd0\u884c\u800c\u5f02\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># get embeddings for all article descriptions\nembeddings = [embedding_from_string(string) for string in article_descriptions]\n# compress the 2048-dimensional embeddings into 2 dimensions using t-SNE\ntsne_components = tsne_components_from_embeddings(embeddings)\n# get the article labels for coloring the chart\nlabels = df[\"label\"].tolist()\n\nchart_from_components(\n    components=tsne_components,\n    labels=labels,\n    strings=article_descriptions,\n    width=600,\n    height=500,\n    title=\"t-SNE components of article descriptions\",\n)\n<\/pre><\/div>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5373\u4f7f\u662f\u9ad8\u5ea6\u538b\u7f29\u7684\u5d4c\u5165\u4e5f\u80fd\u5f88\u597d\u5730\u6309\u7c7b\u522b\u5bf9\u6587\u7ae0\u63cf\u8ff0\u8fdb\u884c\u805a\u7c7b\u3002\u503c\u5f97\u5f3a\u8c03\u7684\u662f\uff1a\u8fd9\u79cd\u805a\u7c7b\u662f\u5728\u4e0d\u4e86\u89e3\u6807\u7b7e\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u7684\uff01<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5982\u679c\u60a8\u4ed4\u7ec6\u89c2\u5bdf\u6700\u4ee4\u4eba\u9707\u60ca\u7684\u5f02\u5e38\u503c\uff0c\u5b83\u4eec\u901a\u5e38\u662f\u7531\u4e8e\u6807\u8bb0\u9519\u8bef\u800c\u4e0d\u662f\u5d4c\u5165\u4e0d\u826f\u9020\u6210\u7684\u3002\u4f8b\u5982\uff0c\u7eff\u8272\u8fd0\u52a8\u805a\u7c7b\u4e2d\u7684\u5927\u591a\u6570\u84dd\u8272\u4e16\u754c\u70b9\u4f3c\u4e4e\u662f\u4f53\u80b2\u6545\u4e8b\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u6839\u636e\u5b83\u4eec\u662f\u6e90\u6587\u7ae0\u3001\u5176\u6700\u8fd1\u7684\u90bb\u5c45\u8fd8\u662f\u5176\u4ed6\u6765\u91cd\u65b0\u7740\u8272\u8fd9\u4e9b\u70b9\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># create labels for the recommended articles\ndef nearest_neighbor_labels(\n    list_of_indices: list[int],\n    k_nearest_neighbors: int = 5\n) -&gt; list[str]:\n    \"\"\"Return a list of labels to color the k nearest neighbors.\"\"\"\n    labels = [\"Other\" for _ in list_of_indices]\n    source_index = list_of_indices[0]\n    labels[source_index] = \"Source\"\n    for i in range(k_nearest_neighbors):\n        nearest_neighbor_index = list_of_indices[i + 1]\n        labels[nearest_neighbor_index] = f\"Nearest neighbor (top {k_nearest_neighbors})\"\n    return labels\n\n\ntony_blair_labels = nearest_neighbor_labels(tony_blair_articles, k_nearest_neighbors=5)\nchipset_security_labels = nearest_neighbor_labels(chipset_security_articles, k_nearest_neighbors=5\n)\n<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># a 2D chart of nearest neighbors of the Tony Blair article\nchart_from_components(\n    components=tsne_components,\n    labels=tony_blair_labels,\n    strings=article_descriptions,\n    width=600,\n    height=500,\n    title=\"Nearest neighbors of the Tony Blair article\",\n    category_orders={\"label\": [\"Other\", \"Nearest neighbor (top 5)\", \"Source\"]},\n)\n<\/pre><\/div>\n\n\n\n<p>\u770b\u770b\u4e0a\u9762\u76842D\u56fe\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5173\u4e8e\u6258\u5c3c\u00b7\u5e03\u83b1\u5c14\u7684\u6587\u7ae0\u5728\u4e16\u754c\u65b0\u95fb\u96c6\u7fa4\u4e2d\u6709\u4e9b\u63a5\u8fd1\u3002\u6709\u8da3\u7684\u662f\uff0c\u5c3d\u7ba1 5 \u4e2a\u6700\u8fd1\u90bb\uff08\u7ea2\u8272\uff09\u5728\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\u6700\u63a5\u8fd1\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u662f\u8fd9\u4e2a\u538b\u7f29 2D \u7a7a\u95f4\u4e2d\u6700\u8fd1\u7684\u70b9\u3002\u5c06\u5d4c\u5165\u538b\u7f29\u5230 2 \u7ef4\u4f1a\u4e22\u5f03\u5b83\u4eec\u7684\u5927\u90e8\u5206\u4fe1\u606f\uff0c\u5e76\u4e14 2D \u7a7a\u95f4\u4e2d\u6700\u8fd1\u7684\u90bb\u5c45\u4f3c\u4e4e\u4e0d\u5982\u5b8c\u6574\u5d4c\u5165\u7a7a\u95f4\u4e2d\u7684\u90bb\u5c45\u91cd\u8981\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># a 2D chart of nearest neighbors of the chipset security article\nchart_from_components(\n    components=tsne_components,\n    labels=chipset_security_labels,\n    strings=article_descriptions,\n    width=600,\n    height=500,\n    title=\"Nearest neighbors of the chipset security article\",\n    category_orders={\"label\": [\"Other\", \"Nearest neighbor (top 5)\", \"Source\"]},\n)\n<\/pre><\/div>\n\n\n\n<p>\u5bf9\u4e8e\u82af\u7247\u7ec4\u5b89\u5168\u793a\u4f8b\uff0c\u5b8c\u6574\u5d4c\u5165\u7a7a\u95f4\u4e2d\u7684 4 \u4e2a\u6700\u8fd1\u90bb\u5728\u6b64\u538b\u7f29\u7684 2D \u53ef\u89c6\u5316\u4e2d\u4ecd\u7136\u662f\u6700\u8fd1\u90bb\u3002\u7b2c\u4e94\u4e2a\u663e\u793a\u5f97\u66f4\u8fdc\uff0c\u5c3d\u7ba1\u5728\u6574\u4e2a\u5d4c\u5165\u7a7a\u95f4\u4e2d\u66f4\u8fd1\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u9700\u8981\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570 <code>chart_from_components_3D<\/code> \u5236\u4f5c\u5d4c\u5165\u7684\u4ea4\u4e92\u5f0f 3D \u56fe\u3002\uff08\u8fd9\u6837\u505a\u9700\u8981\u4f7f\u7528<code>n_components=3<\/code>\u91cd\u65b0\u8ba1\u7b97 t-SNE \u7ec4\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u8bae\u5728\u7f51\u7edc\u4e0a\u5e7f\u4e3a\u6d41\u4f20\u3002 \u6b64\u7b14\u8bb0\u672c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u5d4c\u5165\u6765\u67e5\u627e\u8981\u63a8\u8350\u7684\u7c7b\u4f3c\u9879\u76ee\u3002\u7279\u522b\u662f\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;AG \u7684\u65b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[289,442,312],"tags":[242,314],"class_list":["post-3122","post","type-post","status-publish","format-standard","hentry","category-gpt","category-llms","category-openai","tag-chatgpt","tag-openai-api"],"views":1907,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3122"}],"version-history":[{"count":17,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3122\/revisions"}],"predecessor-version":[{"id":3144,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3122\/revisions\/3144"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}