2017-01-05 38 views
1

似乎不可能將預訓練嵌入加載到圖層。見hereCNTK:從文件加載預訓練詞嵌入的解決方法

我也作爲一種解決方法是什麼如下:

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 ... ...

請問上面看起來像一個正確的workaro UND? 我開始了一個訓練課程,與我在訓練嵌入圖層時相比,參數數量減少了,這可能是一個很好的指示。

回答

0

此問題已修復。例如:

# 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') 
2

您可以試試:E.value = emb作爲替代解決方法。

您的解決方法將嵌入凍結爲常量。如果這是不可接受的,你想進一步訓練嵌入,上述方法可能是你的選擇。

相關問題