tensorflow 中的embedding 報錯問題解決


    今天TensorFlow中的tf.contrib.layers.embed_sequence來對輸入進行embedding時候,發現報了如下的錯誤:

InvalidArgumentError (see above for traceback): indices[1,2] = 6 is not in [0, 6)
[[Node: EmbedSequence_8/embedding_lookup = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@EmbedSequence_8/embeddings"], validate_indices=true, _dev
ice="/job:localhost/replica:0/task:0/cpu:0"](EmbedSequence_8/embeddings/read, EmbedSequence_8/embedding_lookup/indices)]]

   后來就找了一些資料,尋求解決方法,發現是vocab_size要加1才可以,因為vocab_to_int是從1開始編碼的,0留作評論單詞不足的補全位,代碼如下:

 
   import tensorflow as tffeatures = [[1,2,3],[4,5,6]]n_words=6outputs = tf.contrib.layers.embed_sequence(features, vocab_size=n_words+1, embed_dim=4)with tf.Session(config=config) as sess:    sess.run(tf.global_variables_initializer())    a=sess.run(outputs)    print(a)   

詞向量結果如下:

[[[ 0.42639822 -0.45257723  0.44895023  0.17683214]
[ 0.68834776 0.25755352 0.18518716 -0.36953419]
[-0.20138246 -0.35034212 0.44844049 0.3326121 ]]

[[-0.55106479 -0.64119202 -0.06463015 -0.68032914]
[ 0.58467633 0.58155423 0.63106912 0.17282218]
[ 0.46636218 -0.73744893 0.38337153 0.64258808]]]



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com