语句的向量表示方法——单词向量组合…

本文探讨了单词向量组合的不同方法,包括逻辑表示法、连接主义和语义空间方法。重点讨论了张量积、矩阵运算在组合中的应用,以及这些方法如何处理语法、词序和语义关系。文章指出,理想的组合方式应能体现短语的真实含义,同时克服维度灾难和保持元素的独立性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:本文讨论单词向量的组合方式(composition of word vectors),一般指两个单词向量的组合方式,使得输出的新的向量能够表达组合后的短语的语义。

本文使用符号
u,v:普通的单词向量(u_1,u_2,u_3)和(v_1,v_2,v_3);
p:需要求出的目标短语的向量;
R:一个表征语法关系的矩阵;
K:世界知识等其他非单词语义、语法关系构成的信息;

词语组合的一些假想
1、整体的含义是一个以部分作为自变量的函数的输出(The meaning of a whole is a function of the meaning of the parts. )
语句的向量表示方法——单词向量组合(composition <wbr>of <wbr>word <wbr>vectors)方式探讨
2、整体的含义是一个以部分、以及相应的语法规则作为输入的函数(The meaning of a whole is a function of the meaning of the parts and of the way they are syntactically combined.)
语句的向量表示方法——单词向量组合(composition <wbr>of <wbr>word <wbr>vectors)方式探讨
3、除了上述输入之外,还应添加其他而外的信息作为输入
语句的向量表示方法——单词向量组合(composition <wbr>of <wbr>word <wbr>vectors)方式探讨
实现上述论断的主要困难是对于一个具体的短语,很难判断使用什么方程来进行组合(There is the fundamental difficulty of specifying what sort "function of the meanings of the parts" is involved in semantic composition.)。
为了降低困难,可以认为组成短语的单词保持了独立性,适用于这些单词的规则在这些单词出现在其他短语中时也适用。( systematicity: The ability to understand some sentences is intrinsically connected to the ability to understand certain others. In other words, if one understands some sentence and the rules that govern its construction, one can understand a different sentence made up of the same elements according to the same set of rules. In combing parts to form a whole, the parts remain independent and maintain their identities.)当然这种假设和上文提到的Frege (1884)的论断是矛盾的。

论断三是目前普遍接受的假设,但也并不是无懈可击的。像固定搭配、成语等,是不能从单个词语的含义推断出整体的含义的,这和论断1是完全矛盾的(The difficulty in defining compositionality is highlighted by Frege (1884) himself who cautions never to ask for the meaning of a word in isolation but only in the context of a statement. In other words, it seems that the meaning of the whole is constructed from its parts, and the meaning of the parts is derived from the whole.  Linguistic structures range from full
Word2Vec.Net 是单词转换成向量形式工具Word2Vec .NET版本。使用示例代码:            var builder = Word2VecBuilder.Create();             if ((i = ArgPos("-train",  args)) > -1)                 builder.WithTrainFile&#40;args[i   1]&#41;;             if ((i = ArgPos("-output", args)) > -1)                 builder.WithOutputFile&#40;args[i   1]&#41;;             //to all other parameters will be set default values             var word2Vec = builder.Build();             word2Vec.TrainModel();             var distance = new Distance(args[i   1]);             BestWord[] bestwords = distance.Search("some_word");或者//more explicit option         string trainfile="C:/data.txt";         string outputFileName = "C:/output.bin";         var word2Vec = Word2VecBuilder.Create()             .WithTrainFile&#40;trainfile&#41;// Use text data to train the model;             .WithOutputFile&#40;outputFileName&#41;//Use to save the resulting word vectors / word clusters             .WithSize(200)//Set size of word vectors; default is 100             .WithSaveVocubFile&#40;&#41;//The vocabulary will be saved to <file>             .WithDebug(2)//Set the debug mode (default = 2 = more info during training)             .WithBinary(1)//Save the resulting vectors in binary moded; default is 0 (off)             .WithCBow(1)//Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)             .WithAlpha(0.05)//Set the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW             .WithWindow(7)//Set max skip length between words; default is 5             .WithSample((float) 1e-3)//Set threshold for occurrence of words. Those that appear with higher frequency in the training data twill be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)             .WithHs(0)//Use Hierarchical Softmax; default is 0 (not used)             .WithNegative(5)//Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)             .WithThreads(5)//Use <int
### Transformer 模型理解与处理语句的方式 #### 位置编码的重要性 由于注意力机制本身并不具备捕捉序列中元素顺序的能力,这意味着不同排列的输入会得到相同的输出,这显然不符合自然语言处理的需求。因此,在原始输入被转换成词嵌入之后,还需要加入额外的信息来指示每个单词在其所在句子中的具体位置。通过引入固定模式的位置编码并将其直接加到对应的词向量上,使得模型能够区分相同词语的不同出现次序[^4]。 #### Sinusoidal Positional Encoding 实现细节 一种常用的方法是采用正弦波形式的位置编码方案。对于任意给定的位置pos以及维度i, 编码值由频率不同的两个分量组成——一个是基于sin函数计算得出的结果;另一个则是cos函数所对应的部分。这种设计允许模型自动学习如何利用这些周期性的特征组合去感知距离关系,即使当涉及到非常长的距离时也能保持良好的表现力[^3]。 #### Attention Mechanism 工作流程概述 一旦获得了带有位置信息的增强版词嵌入矩阵X (即包含了词汇意义及时空属性),就可以进入核心部分—多头自注意层(Multi-head Self-Attention Layer)了。在这里面,每一个token都会与其他所有tokens建立联系,并据此调整自身的表征强度。具体来说就是先分别计算查询Query(Q), 键Key(K) 和值Value(V)三个子空间内的投影版本; 接着按照一定规则衡量两两之间的相似度得分(通常使用缩放点积法); 最终依据权重分布重新分配来自其他节点的影响程度从而形成新的上下文敏感表达[^1]。 ```python import numpy as np def scaled_dot_product_attention(q, k, v): """实现简单的缩放点积注意力""" matmul_qk = np.matmul(q, k.T) dk = q.shape[-1] logits = matmul_qk / np.sqrt(dk) attention_weights = softmax(logits, axis=-1) output = np.matmul(attention_weights, v) return output def softmax(x, axis=None): e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=axis) q = [[0.1], [0.3], [-0.2]] k = [[0.2], [0.5], [0.7]] v = [[0.9], [0.8], [0.6]] output = scaled_dot_product_attention(np.array(q), np.array(k), np.array(v)) print(output) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值