【问题标题】:CNTK: Workaround for loading pretrained word embeddings from fileCNTK:从文件加载预训练词嵌入的解决方法
【发布时间】:2017-01-05 21:18:06
【问题描述】:

似乎无法将预训练的嵌入加载到层。见here

我的解决方法如下:

    model = create_model()

    E = [p for p in model.parameters if p.name == 'E'][0]
    emb = np.asarray(np.loadtxt('embeddings.txt', delimiter=' '), dtype='float32')
    model = model.clone(CloneMethod.clone, { E: constant(emb) })

embeddings.txt 具有以下格式,其中行数是我使用的词汇表中的单词数,列数是我为嵌入选择的维度: -0.05952413007617 0.12596195936203 -0.189506858587265 ... -0.0871662572026253 -0.0454806201159954 -0.126074999570847 ... ...

以上看起来是正确的解决方法吗? 我开始了一个训练课程,与训练嵌入层时相比,参数数量减少了,这可能是一个很好的指示。

【问题讨论】:

    标签: python cntk


    【解决方案1】:

    您能否尝试一下:E.value = emb 作为替代解决方法。

    您的解决方法将嵌入冻结为常量。如果这是不可接受的,并且您想进一步训练嵌入,则上述方法可能是您的选择。

    【讨论】:

      【解决方案2】:

      此问题已修复。例如:

      # embedding, initialized from a user-supplied constant weight table
      e = Embedding(weights=[[1, 3, 2], [3, 4, 1]])
      
      # (you would get the weights from a file instead)
      
      # test it:
      y = Input(2)
      
      dat = np.array([[-1., 1.]], dtype=np.float32)
      res = e(y).eval({y: dat})
      
      npout = np.matrix(dat[0]) * e.E.value
      np.testing.assert_array_equal(res[0], npout, err_msg='Error in constant embedding layer')
      

      【讨论】:

        猜你喜欢
        • 2019-12-30
        • 1970-01-01
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多