【发布时间】:2018-11-27 03:02:01
【问题描述】:
我正在尝试使用 fasttext python 包在 windows 中训练一个 fasttext 分类器。我有一个 utf8 文件,其中包含
之类的行__label__type1 sample sentence 1
__label__type2 sample sentence 2
__label__type1 sample sentence 3
当我跑步时
fasttext.supervised('data.train.txt','model', label_prefix='__label__', dim=300, epoch=50, min_count=1, ws=3, minn=4, pretrained_vectors='wiki.simple.vec')
我收到以下错误
File "fasttext\fasttext.pyx", line 256, in fasttext.fasttext.supervised (fasttext/fasttext.cpp:7265)
File "fasttext\fasttext.pyx", line 182, in fasttext.fasttext.train_wrapper (fasttext/fasttext.cpp:5279)
ValueError: fastText: cannot load data.train.txt
当我检查目录中的文件类型时,我得到了
__pycache__: directory
data.train.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators
train.py: Python script, ASCII text executable, with CRLF line terminators
wiki.simple.vec: UTF-8 Unicode text, with very long lines, with CRLF line terminators
另外,当我尝试在 MacOs 中使用相同的训练文件训练相同的分类器时,它可以正常工作。我试图了解为什么无法读取该 txt 文件。
谢谢!
【问题讨论】:
-
不确定是什么导致了这种行为,但 fasttext 仅适用于 Linux/MacOS。它需要良好的 C++ 支持。查看官方github repo。你也可以看看这个answer
-
您可以添加更多代码和示例(我也可以上传您的文件)吗?我已经使用 wiki.simple.vec 和 data.train.txt 样本进行了尝试,并且效果很好: >>> fasttext.supervised('data.train.txt','model', label_prefix=' label', dim=300, epoch=50, min_count=1, ws=3, minn=4, pretrained_vectors='wiki.simple.vec')
-
另外,如果你不习惯 Windows,你确定 cwd 指向正确的地方吗?您可以尝试使用 os.path.join() 并构建 .txt 文件的完整绝对路径,看看是否仍然存在问题。 Python 不像现代 MacOS 那样本身就是 Windows 的一部分,因此它的行为可能与您习惯的不同。 techibee.com/python/get-current-directory-using-python/2790
-
@user8212173,谢谢,我知道官方不支持,从lfd.uci.edu/~gohlke/pythonlibs拿了轮子,成功内置在我的windows机器上
-
如果您需要使用包含 \n 和 \t(或 \b 或 \a 或其他一些)的 Windows 路径,这里有两种方法:
"myfolder\\nfolder\\tfolder") 或在字符串常量前使用带有“r”的“原始”字符串 (r"myfolder\nfolder\tfolder")
标签: python windows utf-8 fasttext