openAI|以 2D 或 3D 形式可视化嵌入

本文介绍了如何使用t-SNE算法将高维的语言嵌入降至2维,并将结果可视化成散点图。在这个例子中,我们使用了亚马逊上的食品评论数据集,并将每个评论的打分映射成了散点图上点的颜色。

在 2D 中可视化嵌入

我们将使用 t-SNE 将嵌入的维数从 1536 减少到 2。一旦嵌入减少到二维,我们就可以在二维散点图中绘制它们。 数据集在 Obtain_dataset Notebook 中创建。

1.降维

我们使用 t-SNE 分解将维度降为 2 维。

import pandas as pd
from sklearn.manifold import TSNE
import numpy as np

# Load the embeddings
datafile_path = "data/fine_food_reviews_with_embeddings_1k.csv"
df = pd.read_csv(datafile_path)

# Convert to a list of lists of floats
matrix = np.array(df.embedding.apply(eval).to_list())

# Create a t-SNE model and transform the data
tsne = TSNE(n_components=2, perplexity=15, random_state=42, init='random', learning_rate=200)
vis_dims = tsne.fit_transform(matrix)
vis_dims.shape
(1000, 2)

2.绘制嵌入

我们根据星级评分为每条评论着色,从红色到绿色。

即使在降维的情况下,我们也可以观察到良好的数据分离。

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

colors = ["red", "darkorange", "gold", "turquoise", "darkgreen"]
x = [x for x,y in vis_dims]
y = [y for x,y in vis_dims]
color_indices = df.Score.values - 1

colormap = matplotlib.colors.ListedColormap(colors)
plt.scatter(x, y, c=color_indices, cmap=colormap, alpha=0.3)
for score in [0,1,2,3,4]:
    avg_x = np.array(x)[df.Score-1==score].mean()
    avg_y = np.array(y)[df.Score-1==score].mean()
    color = colors[score]
    plt.scatter(avg_x, avg_y, marker='x', color=color, s=100)

plt.title("Amazon ratings visualized in language using t-SNE")
Text(0.5, 1.0, 'Amazon ratings visualized in language using t-SNE')
openAI|以 2D 或 3D 形式可视化嵌入

此文章由OpenAI开源维基百科原创发布,如若转载请注明出处:https://openai.wiki/visualizing_embeddings_in_2d.html

(0)
上一篇 2023-02-20 14:10
下一篇 2023-02-20 14:44

相关推荐

  • ChatGPT|插件教程 – 2

    ChatGPT插件Keyplays Live Soccer、OwlJourney、Prompt Perfect、AskYourPDF、Word Sneak、Link Reader等使用教程。

    2023-05-21
    001.4K
  • GPT-3|文本写作示例

    本文介绍了如何使用GPT-3语言模型在各种写作任务中协助您,例如博客文章、电子邮件、广告文案等。使用简单的提示,GPT-3可以生成满足特定需求的文本。

    ChatGPT 2023-02-20
    00907
  • openAI|文本比较示例

    OpenAI API 嵌入终结点可用于衡量文本片段之间的相关性或相似性,例如语义搜索、问答、建议和自定义嵌入等操作。余弦相似性分数可以用作排名搜索结果中众多特征中的一个。

    ChatGPT 2023-02-20
    00915
  • Prompt|高级用法

    本文详细介绍了ChatGPT Prompt的使用方法,包括少样本提示、思维链提示、演示、自洽性等概念。其中,少样本提示利用样本数据进行模型训练,提高模型对特定任务的预测能力;

    ChatGPT 2023-02-18
    012.0K
  • Auto-GPT|ChatGPT自动化

    Auto-GPT是一个实验性开源应用程序,展示了GPT-4语言模型的能力。它具备互联网搜索、长期和短期记忆管理、文本生成、访问流行网站和平台等功能,使用GPT-3.5和GPT-4进行文件存储和摘要。

    ChatGPT 2023-04-13
    0315.4K

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

微信