fasttext 无法加载训练 txt 文件

fasttext cannot load training txt file

我正在尝试使用 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 文件。

谢谢!

TL;DR: 使用 os module 安全地构建路径,尤其是在 Python 2

错误提示无法加载文件。由于您的环境之间的唯一区别是操作系统,因此线索是您没有正确定位文件,因为每个 OS 处理路径的方式不同。我觉得这是 mostpython 程序员至少犯过一次的错误,因为这是出乎意料的。

您可以对路径进行硬编码,但是如果您曾经使用 things cross 平台,以后就会遇到问题。就我而言,有时我会在 Windows 中快速开发一些东西,但随后会在 *nix 平台上进行大规模部署。

我建议改为习惯使用 os 模块,因为它可以在 across 平台上工作。在评论中说他们有 "myfolder\nfolder\tfolder" 的路径;通过尝试为路径构建自己的字符串而不是使用 os 模块.. 在 windows 上,即使文件夹不是以换行符 \n 和制表符 \t 开头,它仍然不会'没有用,因为 windows 路径需要转义斜杠 (\)。使用 os,您不必知道。

>>> import os
>>> os.getcwd()
'C:\Python27'
>>> os.path.abspath(os.sep)
'C:\'
>>> os.chdir(os.path.join(os.path.abspath(os.sep, "Users", "Jeff"))
>>> os.getcwd()
'C:\Users\Jeff'

通常,您将使用项目根目录的相对路径,而不是绝对路径。这个os比较简单,当前的OS的根有点棘手(你可以找到答案here

(我提供了我们从评论中得出的完整答案)

编辑:也许 python 3 有一些东西 this link says is better than os, pathlib。我从未使用过 python 3 所以我不能说。

我花了一点时间来创建一个环境来测试你的代码。但是我在 Windows 所做的和为我工作的是在 Cygwin 中安装 fastText。我希望这个答案对有类似问题的人有用。

Environment

  • Windows 10

  • CYGWIN_NT-10.0 DESKTOP-RR909JI 2.10.0(0.325/5/3) 2018-02-02 15:16 x86_64

  • gcc-g++: 7.3 | gcc-核心 7.3

  • Python 2.7 | Python2-Cython 0.25.2 | python2pip | Python2-开发

  • pip 安装 fastText

Files

user@DESKTOP-RR909JI ~/projects
$ file *
data.txt:         ASCII text
data.train.txt:   Big-endian UTF-16 Unicode text
fasttext_ie.py:   Python script, ASCII text executable
model.bin:        data
wiki.simple.vec:  UTF-8 Unicode text, with very long lines 

fastest_ie.py

#!/usr/bin/python
import fasttext

fasttext.supervised('data.txt','model', label_prefix='__label__', dim=300, epoch=50, min_count=1, ws=3, minn=4, pretrained_vectors='wiki.simple.vec')

我已经下载了预训练词向量(wiki.simple.vec)from here。 我已经在 data.txt 中复制了您的输入示例并使用 UTF-16 data.train.txt

制作了一个版本

执行您的代码片段后,需要一段时间但会生成一个文件,但它只发生在 ASCII 文本文件中:

user@DESKTOP-RR909JI ~/projects
$ ls -ltrh model.bin
-rw-r--r-- 1 user user 129M jun. 28 00:56 model.bin

它有很多字符串:

qateel
olympiques
lesothosaurus
delillo
satrapi
conferencing
numan
echinodermata
haast
tangerines
duat
vesey
rotaviruses
velox
chepstow
capitale
rock/pop
belasco
sardanapalus
jadis
macintyre

When trying with UTF-16

没有生成文件,也没有完成处理,只是保持运行没有完成。

所以我们可以说,它失败了。

尽管 fastText 说 UTF-8 it's supported:

where data.txt is a training file containing UTF-8 encoded text. By default the word vectors will take into account character n-grams from 3 to 6 characters. At the end of optimization the program will save two files: model.bin and model.vec. model.vec is a text file containing the word vectors, one per line. model.bin is a binary file containing the parameters of the model along with the dictionary and all hyper parameters. The binary file can be used later to compute word vectors or to restart the optimization.

我通过 Cygwin 安装的版本可能会有所不同。

而且在阅读了 Whosebug 中的 this question 之后,我想问一下:您是否尝试过将文件更改为 ASCII 并测试会发生什么?

我的所有文件都在同一个根目录中。

我不知道 fastText,但我想执行你的代码,它可以工作。我的 gcc 库有问题,我必须为 g++ 和核心安装相同的版本。