解决text2vec模型加载报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode bytes in position。。。的方法

在使用text2vec模型时遇到UnicodeDecodeError,官方建议通过两种方式修复:1) 使用支持unicode和utf8的工具存储模型;2) 在加载模型时设置unicode_errors='ignore'来忽略错误。此外,提到了腾讯AI提供的轻量版和全量版词向量资源,以及加载轻量版模型时的代码示例。

这个错误是加载模型失败,以下是官方的解决方法:

Answer: The strings (words) stored in your model are not valid utf8. By default, gensim decodes the words using the strict encoding settings, which results in the above exception whenever an invalid utf8 sequence is encountered.
The fix is on your side and it is to either:
a) Store your model using a program that understands unicode and utf8 (such as gensim). Some C and Java word2vec tools are known to truncate the strings at byte boundaries, which can result in cutting a multi-byte utf8 character in half, making it non-valid utf8, leading to this error.
b) Set the unicode_errors flag when running load_word2vec_model, e.g. load_word2vec_model(…, unicode_errors=‘ignore’). Note that this silences the error, but the utf8 problem is still there – invalid utf8 characters will just be ignored in this case.

给出了两种办法:

  1. 可以用C和JAVA的相关工具去处文本中的无效编码字符;
  2. 建议在load_word2vec_model函数中设置参数unicode_errors=‘ignore’,可以暂时忽略这个错误。

使用text2vec时报错如何修改?

text2vec是一个文本转向量库,封装了word2vec、bert等方法。

腾讯ai官方给出了两种word2vec:

加载word2vec模型时出现UnicodeDecodeError: 'utf-8' codec can't decode bytes in position。。。错误的话

如果使用的轻量版(light_Tencent_AILab_ChineseEmbedding.bin)模型加载代码改成如下所示:

    w2v_model = Word2Vec(WORD2VEC_MODEL_PATH, w2v_kwargs={"unicode_errors": "ignore", "binary": True})

关键点在于添加初始化参数:

w2v_kwargs={"unicode_errors": "ignore", "binary": True}

使用Tencent_AILab_ChineseEmbedding版本,模型初始化代码改成这样:

    w2v_model = Word2Vec(WORD2VEC_MODEL_PATH, w2v_kwargs={"unicode_errors": "ignore"})

以上就是word2vec模型初始化报错的解决办法~

如果帮助到了你,就点个赞吧~

完结撒花~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值