Chinese-Word-Vectors icon indicating copy to clipboard operation
Chinese-Word-Vectors copied to clipboard

Need Help...

Open cyzLoveDream opened this issue 6 years ago • 68 comments

大佬能不能提供下训练词向量的语料呢,需要怎么使用这个词向量?使用Word2Vec.load()会报错

cyzLoveDream avatar May 15 '18 02:05 cyzLoveDream

语料可能没法提供,涉及到版权之类的问题。 这些词向量都是十进制的txt不是二进制的bin,你可能需要调整一下load()的参数。

shenshen-hungry avatar May 15 '18 02:05 shenshen-hungry

@shenshen-hungry 您好,请问一下词向量的读取问题。 在您提供的预训练词向量上运行evaluation toolkit会显示错误如下: UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 13: illegal multibyte sequence 谢谢!

xlx0010 avatar May 15 '18 03:05 xlx0010

@xlx0010 你应该是用Windows跑的,Windows上Python默认是GBK来解码,所以会出问题,你读的时候用open(filename, encoding='utf-8')就能解决。兼容性问题我们稍后会更新。

shenshen-hungry avatar May 15 '18 03:05 shenshen-hungry

@shenshen-hungry 我试过utf8,还是会报错: UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 7593-7594: invalid continuation byte

xlx0010 avatar May 15 '18 03:05 xlx0010

@shenshen-hungry 我用的词向量文件是sgns.zhihu.bigram

xlx0010 avatar May 15 '18 03:05 xlx0010

@xlx0010 读哪个文件会报错?我查一下。

shenshen-hungry avatar May 15 '18 03:05 shenshen-hungry

@shenshen-hungry image

xlx0010 avatar May 15 '18 03:05 xlx0010

@xlx0010 读特别长的符号时候会报错,解决方法是:

with open('sgns.zhihu.bigram', 'rb) as f:
    for line_b in f:
        line_u = line_b.decode('utf-8')

shenshen-hungry avatar May 15 '18 05:05 shenshen-hungry

麻烦看下,用的sgns.target.word-character.char1-2.dynwin5.thr10.neg5.dim300.iter5.bz2这个文件。 下面这个代码还是会报错UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 96-97: invalid continuation byte

 with bz2.open(self.__embedding_path, mode='rb') as f:
            for line_b in f:
                line = line_b.decode('utf-8')

laddie132 avatar May 15 '18 06:05 laddie132

@laddie132 bz2是bzip2格式的压缩包,得先解压一下才能读。

shenshen-hungry avatar May 15 '18 06:05 shenshen-hungry

用的就是python的bz2包啊。看了下,是下面这个word在decode出错了 \xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc\x9d\xef\xbc

在字典里面是================================�这个word,后面貌似原本就是乱码的。

貌似字典里面还有很多这样的━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━�,后面带个莫名其妙的字符

laddie132 avatar May 15 '18 06:05 laddie132

解压后,试了下还是一样的错,用latin-1可以解码,但是乱码的

laddie132 avatar May 15 '18 06:05 laddie132

大佬你们可以分享下你们清洗各个语料的代码嘛,我们自己去下载原始语料来处理下,这样既没有版权问题,我们还省掉了处理不同格式语料的问题。

XiaolinZHONG avatar May 15 '18 07:05 XiaolinZHONG

@shenshen-hungry 谢谢你!我在代码里过滤掉了几处会报错的地方,基本可以使用了。错误也是出在文件中有几处很长的‘=====’和‘――――’。

xlx0010 avatar May 15 '18 07:05 xlx0010

什么时候更新微博的词向量,非常期待

Zyue10969 avatar May 15 '18 07:05 Zyue10969

@laddie132 我刚用Python3试了下,可以正常读取的。如果报错的话可以跳过这行。

shenshen-hungry avatar May 15 '18 07:05 shenshen-hungry

@XiaolinZHONG 我们把网页数据去掉了html和xml标记,用HanLP(1.5.3)分词,之后训练的。没有更多的复杂操作。

shenshen-hungry avatar May 15 '18 07:05 shenshen-hungry

@shenshen-hungry Is there any possibility to public the source code of the crawler for Chinese text materials?

imWildCat avatar May 15 '18 09:05 imWildCat

@imWildCat We used a simple crawler built with Python3 and urllib package. Using Headers and Sleep can help you obtain enough data : )

shenshen-hungry avatar May 15 '18 15:05 shenshen-hungry

@shenshen-hungry Many thanks! It is really helpful!

imWildCat avatar May 15 '18 16:05 imWildCat

Same thing happened to me. You'll need to ignore those errors. When loading mixed-large word2vec model, 37 lines contain errors related to utf8 encoding, 5 lines contain errors due to "empty word surface", 1.3M word vectors are loaded successfully.

AquaskyCamus avatar May 16 '18 01:05 AquaskyCamus

更新了词向量,应该不会有unicode编码问题了。 如果还有问题,可以在打开的时候加参数。 I updated files. If there are still errors with utf8 encoding, add errors='ignore' in open():

open(filename, errors='ignore')

shenshen-hungry avatar May 17 '18 06:05 shenshen-hungry

Word + Character 是把字向量和词向量放到一起了吗?

mfxss avatar May 18 '18 01:05 mfxss

@Zyue10969 微博已更新。

shenshen-hungry avatar May 18 '18 02:05 shenshen-hungry

@mfxss 在context中把每个组成词的字也作为单独的token。 2018-05-18 10 18 44

shenshen-hungry avatar May 18 '18 02:05 shenshen-hungry

保存词向量文件时用得是什么编码?sgns.merge.word模型载入时同样报错UnicodeDecodeError: UnicodeDecodeError: 'utf8' codec can't decode bytes in position 96-97: invalid continuation byte

wangzhe258369 avatar May 18 '18 06:05 wangzhe258369

@wangzhe258369 用utf8编码的,最早期上传的词向量可能由于最大长度的限制在保存词表的时候错误截断了一些超长单词导致了编码不完整。昨天上传的词向量已经修复了这个问题。另外,如果还有编码问题可以用open(filename, encoding='utf-8', errors='ignore')来打开,跳过出bug的行。

shenshen-hungry avatar May 18 '18 06:05 shenshen-hungry

OK,谢谢。涉及到文本文件读写的一个好习惯是全部明确指定编码:open(filename, encoding='utf-8'),使用系统默认值不一定出什么幺蛾子。

wangzhe258369 avatar May 18 '18 06:05 wangzhe258369

还有一个问题,请问未登录词是被忽略了吗?在sgns.merge.word模型里没有看到类似<UNK>等表示未登录词的Token。

wangzhe258369 avatar May 18 '18 07:05 wangzhe258369

@wangzhe258369 具体操作虽然Readme里面有写,但是再多啰嗦一下:我们在训练词向量的时候先对于在语料中出现次数小于10次的词会过滤掉,并且没有设置,因为通常和下游任务有关,在下游任务中也有可能会删掉一些词语。一个常见的办法是把整个词表的词取平均作为的词向量。

shenshen-hungry avatar May 18 '18 07:05 shenshen-hungry