我使用gensim軟件包訓練了word2vec模型並使用以下名稱進行保存。Word2vec保存的模型不是UTF-8編碼,但輸入到Word2vec模型的語句是UTF-8編碼
我得到了這些日誌信息。而模型正在接受培訓和保存。
INFO : not storing attribute syn0norm
INFO : not storing attribute cum_table
然後,我試圖加載使用這個模型,
from gensim.models import Word2Vec
model = Word2Vec.load("300features_1minwords_10context")
我得到了下面的錯誤。
2017-06-22 21:27:14,975 : INFO : loading Word2Vec object from 300features_1minwords_10context
2017-06-22 21:27:15,496 : INFO : loading wv recursively from 300features_1minwords_10context.wv.* with mmap=None
2017-06-22 21:27:15,497 : INFO : setting ignored attribute syn0norm to None
2017-06-22 21:27:15,498 : INFO : setting ignored attribute cum_table to None
2017-06-22 21:27:15,499 : INFO : loaded 300features_1minwords_10context
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-9d90db0f07c0> in <module>()
1 from gensim.models import Word2Vec
2 model = Word2Vec.load("300features_1minwords_10context")
----> 3 model.syn0.shape
AttributeError: 'Word2Vec' object has no attribute 'syn0'
此外,在文件 「300features_1minwords_10context」,它表明
"300features_1minwords_10context" is not UTF-8 encoded
Saving disabled.
Open console for more details
要解決上述屬性的錯誤,我也試圖從谷歌論壇下面,
import gensim
model = gensim.models.KeyedVectors.load_word2vec_format("300features_1minwords_10context")
model.syn0.shape
它導致另一個錯誤,它是
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
該模型是用UTF-8編碼句子訓練的。我不知道爲什麼它會拋出這個錯誤?
更多信息:
df = pd.read_csv('UNSPSCdataset.csv',encoding='mac_roman',low_memory=False)
features = ['MaterialDescription']
temp_features = df[features]
temp_features.to_csv('materialDescription', encoding='UTF-8')
X = pd.read_csv('materialDescription',encoding='UTF-8')
在這裏,我不得不爲了利用大熊貓據幀來訪問它使用「mac_roman」編碼。由於數據框中的文本在訓練模型時必須使用UTF-8格式,因此我通過使用UTF-8編碼並將其保存在單獨的csv文件中,然後我可以訪問該特定列。
任何幫助是明顯的
嗨@gojomo,我正在使用最新的gensim。是的,我直接加載() 我得到「沒有屬性」錯誤我已附加問題中的錯誤堆棧。沒有生成像syn0一樣的東西。即使模型保存的文件也沒有任何內容。由於Unicode問題,我猜這個模型根本就沒有保存。 而當我打印句子時,我沒有得到像這樣的unicode符號u'Airtel' –
請參閱上面的內聯更新;實際的錯誤不在'load()'中,而是在你自己的下一行嘗試訪問'model.syn0' - 試圖訪問'syn0'的地方(在最近的gensim版本中)它不再存在,移動到' model.wv.syn0'。 – gojomo
嗨@gojomo,請試試這個。 https://stackoverflow.com/questions/44740161/how-to-preserve-number-of-records-in-word2vec –