zoukankan      html  css  js  c++  java
  • 词向量入门

    词向量

    one hot 编码

    在自然语言处理中,为了将自然语言转化为计算机所能识别的语言,就要对它重新编码,起初使用one hot编码。

    一共能产生14901维。

    问题:占用太大空间,词和词之间的相识度无法体现。也就是所说的稀疏化。

    one hot代码如下:

    from sklearn.preprocessing import OneHotEncoder
    # lables = ['ni','号','ni','meimei']
    lables = [0,1,0,4]
    lables = np.array(lables).reshape(len(lables),-1)
    enc =   OneHotEncoder()
    enc.fit(lables)
    target = enc.transform(lables).toarray()
    
    print(target)
    

    词向量编码思想

    我们需要将上面的one hot 编码转化为如图所示的编码:

    主要有两种假说,今天我们只谈当今的主流思想: Distributed models

    Word2Vec

    Word2vec 是代码项目的名字,只是计算词嵌入(word embedding)的一个工具,是CBOW和Skip-Gram这两个模型的合体,目前这套工具完全开源。

    CBOW是利用词的上下文预测当前的单词;而Skip-Gram则是利用当前词来预测上下文。

    Word2Vec代码

    # 训练模型定义
    from gensim.models import Word2Vec
    model = Word2Vec(sentences, sg=1, size=100,  window=5,  min_count=5,  negative=3, sample=0.001, hs=1, workers=4)
    
    # 训练后的模型保存与加载
    model.save("model_name")
    
    # 加载模型
    model = Word2Vec.load("model_name")
    
    # 模型的使用
    
    #词向量加减
    model.most_similar(positive=['woman', 'king'], negative=['man'])
    #输出[('queen', 0.50882536), ...]
        
    # 寻找指定词语最相似的词语
    print model.most_similar('morning', topn=1)
    
    
    model.doesnt_match("breakfast cereal dinner lunch".split())
    #输出'cereal'
     
    # 计算词语的相似度
    model.similarity('woman', 'man')
    #输出0.73723527
     
    model['computer']  # raw numpy vector of a word
    #输出array([-0.00449447, -0.00310097,  0.02421786, ...], dtype=float32)
    

    Word2Vec参数描述:

    1.sg=1是skip-gram算法,对低频词敏感;默认sg=0为CBOW算法。

    2.size是输出词向量的维数,值太小会导致词映射因为冲突而影响结果,值太大则会耗内存并使算法计算变慢,一般值取为100到200之间。

    3.window是句子中当前词与目标词之间的最大距离,3表示在目标词前看3-b个词,后面看b个词(b在0-3之间随机)。

    4.min_count是对词进行过滤,频率小于min-count的单词则会被忽视,默认值为5。

    5.negative和sample可根据训练结果进行微调,sample表示更高频率的词被随机下采样到所设置的阈值,默认值为1e-3。

    6.hs=1表示层级softmax将会被使用,默认hs=0且negative不为0,则负采样将会被选择使用。

    7.workers控制训练的并行,此参数只有在安装了Cpython后才有效,否则只能使用单核。

    Embedding

    Word2Vec中从输入到隐层的过程就是Embedding的过程。 Embedding的过程就是把多维的onehot进行降维的过程,是个深度学习的过程。满足:

    1. 嵌入层向量长度可设置
    2. 映射过程是全连接
    3. 嵌入层的值可训练
    4. 由高维度映射到低纬度,减少参数量

    参考博客:Word2VecEmbeddingSkip-gram的原理负采样

  • 相关阅读:
    mac上虚拟机:VMWare Fusion, VirtualBox, Parallels Desktop, CrossOver, Veertu
    linux使用其它用户 su
    CentOS7 rc.local开机开法启动
    taskkill
    Make sure that the controller has a parameterless public constructor.
    An Autofac Lifetime Primer
    Controlling Scope and Lifetime
    Instance scope
    Linq的TakeWhile的用法
    Git does not apply deleted files when merging an old branch into the master. How can I tell Git to apply deleted files?
  • 原文地址:https://www.cnblogs.com/jimlau/p/12976617.html
Copyright © 2011-2022 走看看