社区网站免费制作如何建设一个收费的影视图文网站

当前位置: 首页 > news >正文

社区网站免费制作,如何建设一个收费的影视图文网站,做网站的工具怎么使用,离线网页制作工具文章目录 0. 前言1. 基础介绍1.1 基本参数1.2 可选参数1.3 属性1.4 PyTorch源码注释 2. 实例演示3. embedding_dim的合理设定4. 结论 0. 前言 按照国际惯例#xff0c;首先声明#xff1a;本文只是我自己学习的理解#xff0c;虽然参考了他人的宝贵见解及成果#xff0c;但… 文章目录 0. 前言1. 基础介绍1.1 基本参数1.2 可选参数1.3 属性1.4 PyTorch源码注释 2. 实例演示3. embedding_dim的合理设定4. 结论 0. 前言 按照国际惯例首先声明本文只是我自己学习的理解虽然参考了他人的宝贵见解及成果但是内容可能存在不准确的地方。如果发现文中错误希望批评指正共同进步。 在自然语言处理(NLP)中torch.nn.Embedding是PyTorch框架中一个至关重要的模块用于将离散的词汇转换成连续的向量空间表示。这种转换允许模型捕捉词汇之间的语义关系并在诸如情感分析、文本分类和机器翻译等任务中发挥关键作用。 本文将深入探讨torch.nn.Embedding的工作原理并通过示例代码演示其在PyTorch中的使用。

  1. 基础介绍 torch.nn.Embedding的本质是一个映射表Lookup table它用于储存自然语言词典和嵌入向量的映射关系。 1.1 基本参数 torch.nn.Embedding的初始化接受两个基本参数num_embeddings和embedding_dim。 num_embeddings这个参数直观理解为“要嵌入的自然语言的词汇数量”表示上面所述的自然语言词典的大小即可能的唯一词汇数量。比如英语中的常用单词从abandon开始一共有3000个那num_embeddings就可以设定为3000embedding_dim表示每个词汇映射的嵌入向量的维度。 1.2 可选参数 padding_idx用于指定词汇表中的填充词汇索引该位置的向量将被初始化为零。max_norm用于限制嵌入向量的L2范数。norm_type用于指定范数类型。scale_grad_by_freq如果设置为True则将梯度按词汇频率缩放。sparse如果设置为True则将嵌入梯度标记为稀疏。 1.3 属性 torch.nn.Embedding 模块只有一个属性 weight。这个属性代表了嵌入层要学习的权重即存储所有嵌入向量的矩阵。这是嵌入层的学习权重形状为 (num_embeddings, embedding_dim)也就是上文所说的lookup table映射表。这些权重代表实际的嵌入向量它们是可学习的参数并且在训练过程中会被优化算法更新。默认情况下weight 是从标准正态分布 N(0, 1) 随机初始化的。这意味着每个元素都独立地从均值为 0、标准差为 1 的正态分布中采样。 1.4 PyTorch源码注释 以下是nn.Embedding的源码注释用于上面说明的参考 Args:num_embeddings (int): size of the dictionary of embeddingsembedding_dim (int): the size of each embedding vectorpadding_idx (int, optional): If specified, the entries at :attr:padding_idx do not contribute to the gradient;therefore, the embedding vector at :attr:padding_idx is not updated during training,i.e. it remains as a fixed pad. For a newly constructed Embedding,the embedding vector at :attr:padding_idx will default to all zeros,but can be updated to another value to be used as the padding vector.max_norm (float, optional): If given, each embedding vector with norm larger than :attr:max_normis renormalized to have norm :attr:max_norm.norm_type (float, optional): The p of the p-norm to compute for the :attr:max_norm option. Default 2.scale_grad_by_freq (bool, optional): If given, this will scale gradients by the inverse of frequency ofthe words in the mini-batch. Default False.sparse (bool, optional): If True, gradient w.r.t. :attr:weight matrix will be a sparse tensor.See Notes for more details regarding sparse gradients.Attributes:weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)initialized from :math:\mathcal{N}(0, 1)Shape:- Input: :math:(), IntTensor or LongTensor of arbitrary shape containing the indices to extract- Output: :math:(, H), where * is the input shape and :math:H\text{embedding_dim}

  2. 实例演示 这里我将给出一个简单的例子来说明如何使用 PyTorch 的 torch.nn.Embedding 模块创建一个嵌入层并获取一些单词的嵌入向量。 假设我们有一个小型的词汇表包含以下单词 “the”“cat”“dog”“sat”“on”“mat” 我们将这些单词映射到索引上例如 “the” - 0“cat” - 1“dog” - 2“sat” - 3“on” - 4“mat” - 5 现在我们可以创建一个 torch.nn.Embedding 层将这些单词映射到嵌入向量中。我们将使用一个 3 维的嵌入向量来表示每个单词。 下面是具体的代码示例 import torch import torch.nn as nn# 创建一个 Embedding 层

    num_embeddings: 词汇表的大小这里是 6

    embedding_dim: 嵌入向量的维度这里是 3

    embedding nn.Embedding(num_embeddings6, embedding_dim3)# 定义一些单词的索引 word_indices torch.LongTensor([0, 1, 2, 3, 4, 5]) # the, cat, dog, sat, on, mat# 通过索引获取嵌入向量 word_embeddings embedding(word_indices)# 输出嵌入向量 print(word_embeddings)运行上述代码后word_embeddings 将是一个形状为 (6, 3) 的张量其中每一行代表一个单词的嵌入向量。 tensor([[ 0.0439, 0.7314, -0.3546],[ 0.6975, 1.2725, 1.4042],[-1.7532, -2.0642, -0.1434],[ 0.2538, 1.1123, -0.8636],[-0.7238, -0.0585, 0.5242],[ 0.6485, 0.6885, -1.2045]], grad_fnEmbeddingBackward0)例如word_embeddings[0] 对应于单词 “the” 的嵌入向量word_embeddings[1] 对应于单词 “cat” 的嵌入向量以此类推。 这就是一个简单的英语单词嵌入向量的例子。在实际应用中词汇表会更大嵌入向量的维度也会更高而且通常会使用预训练的嵌入向量来初始化这些权重。

  3. embedding_dim的合理设定 通过上文说明我们可以轻松地掌握nn.Embedding模块的使用但是这里有个问题embedding_dim设定为多少比较合适呢 这里首先要说明下嵌入向量它应该是代表单词“语义”的向量而不是像one-hot那样是简单的字母映射。 举个例子meet和meat两个词拼写十分接近即它们的one-hot编码十分接近但是它们的语义完全不同也就是说嵌入向量应该相差很远。而huge和enormous情况刚好相反它们的one-hot编码完全不同而嵌入向量应该比较接近。 那回到embedding_dim的设定选择上来我觉得可以参考以下3个方面来设定比较合理的embedding_dim 平衡信息量与过拟合风险 信息量: 较高的 embedding_dim 可以捕获更多的信息和细微差别从而提高模型的表达能力。然而这也可能会导致过拟合因为高维空间容易出现稀疏性问题。过拟合风险: 较低的 embedding_dim 可以减少参数数量降低过拟合的风险但可能会丢失一些信息。 考虑词汇表的大小 较小的词汇表: 如果词汇表相对较小例如几千个词较低的 embedding_dim如 50 或 100可能就足够了。较大的词汇表: 对于较大的词汇表例如几十万或更多可以选择较高的 embedding_dim如 200 至 500以更好地捕捉语义信息。 实验验证 交叉验证: 最终的选择通常需要通过实验来确定。使用交叉验证来评估不同 embedding_dim 下的模型性能可以帮助找到最佳值。预训练嵌入: 如果有可用的预训练嵌入如 Word2Vec、GloVe 或 FastText可以考虑使用它们的维度作为参考。 一点点思考在Embedding方法中embedding_dim一般是要比num_embeddings小很多的这会导致矩阵的秩不满最终会导致Embedding方法中的单词可以通过线性变换变成另一个单词。比如把abandon的词向量×2得到get的词向量而one-hot不会有这个问题这是Embedding小小的局限性。 4. 结论 torch.nn.Embedding模块在PyTorch中为NLP任务提供了强大的工具允许模型从词汇索引中学习有意义的向量表示。通过初始化和调用这个模块我们可以轻松地将文本数据转换为适合深度学习模型的格式从而挖掘文本数据中的丰富语义信息。