IOError:[Error no:21]是目录:'./w2v-model/wordmodel3'

问题描述 投票:0回答:1
def generate_w2vModel(decTokenFlawPath, w2vModelPath):
    print("training...")
    model = Word2Vec(sentences= DirofCorpus(decTokenFlawPath), size=30, alpha=0.01, window=5, min_count=0, max_vocab_size=None, sample=0.001, seed=1, workers=1, min_alpha=0.0001, sg=1, hs=0, negative=10, iter=5)
    model.save(w2vModelPath)

def evaluate_w2vModel(w2vModelPath):
    print("\nevaluating...")
    model = Word2Vec.load(w2vModelPath)
    for sign in ['(', '+', '-', '*', 'main']:
        print(sign, ":")
        print(model.most_similar_cosmul(positive=[sign], topn=10))

def main():
    dec_tokenFlaw_path = ['./data/cdg_ddg/corpus/']
    w2v_model_path = "./w2v_model/wordmodel3" 
    generate_w2vModel(dec_tokenFlaw_path, w2v_model_path)
    evaluate_w2vModel(w2v_model_path)
    print("success!")

这是我正在运行的python。此文件用于训练word2vec模型。输入是语料库文件,输出是word2vec模型。我收到以下错误:

Traceback (most recent call last):
  File "create_w2vmodel.py", line 67, in <module>
    main()
  File "create_w2vmodel.py", line 62, in main
    generate_w2vModel(dec_tokenFlaw_path, w2v_model_path)
  File "create_w2vmodel.py", line 50, in generate_w2vModel
    model.save(w2vModelPath)
  File "/usr/local/lib/python2.7/dist-packages/gensim-3.4.0-py2.7-linux-x86_64.egg/gensim/models/word2vec.py", line 930, in save
    super(Word2Vec, self).save(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gensim-3.4.0-py2.7-linux-x86_64.egg/gensim/models/base_any2vec.py", line 281, in save
    super(BaseAny2VecModel, self).save(fname_or_handle, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/gensim-3.4.0-py2.7-linux-x86_64.egg/gensim/utils.py", line 691, in save
    self._smart_save(fname_or_handle, separately, sep_limit, ignore, pickle_protocol=pickle_protocol)
  File "/usr/local/lib/python2.7/dist-packages/gensim-3.4.0-py2.7-linux-x86_64.egg/gensim/utils.py", line 550, in _smart_save
    pickle(self, fname, protocol=pickle_protocol)
  File "/usr/local/lib/python2.7/dist-packages/gensim-3.4.0-py2.7-linux-x86_64.egg/gensim/utils.py", line 1311, in pickle
    with smart_open(fname, 'wb') as fout:  # 'b' for binary, needed on Windows
  File "build/bdist.linux-x86_64/egg/smart_open/smart_open_lib.py", line 89, in smart_open
  File "build/bdist.linux-x86_64/egg/smart_open/smart_open_lib.py", line 301, in file_smart_open
IOError: [Errno 21] Is a directory: './w2v_model/wordmodel3'

请帮助我更改此特定错误。我认为没有这样的文件夹,但是我已经在我的文件夹中创建了w2v_model / wordmodel3。我已经以多种方式尝试过。我将在下面提供smart_open_lib.py程序文件:

def file_smart_open(fname, mode='rb'):
    """
    Stream from/to local filesystem, transparently (de)compressing gzip and bz2
    files if necessary.

    """
    _, ext = os.path.splitext(fname)

    if ext == '.bz2':
        PY2 = sys.version_info[0] == 2
        if PY2:
            from bz2file import BZ2File
        else:
            from bz2 import BZ2File
        return make_closing(BZ2File)(fname, mode)

    if ext == '.gz':
        from gzip import GzipFile
        return make_closing(GzipFile)(fname, mode)

    return open(fname, mode)

这是他们告诉的追溯代码。恳请您帮助我更改此错误!!!>

def generate_w2vModel(decTokenFlawPath,w2vModelPath):print(“ training ...”)model = Word2Vec(句子= DirofCorpus(decTokenFlawPath),大小= 30,alpha = 0.01,窗口= 5,min_count = 0,...] >

python gensim word2vec
1个回答
0
投票

Word2Vec .save()方法需要一个新路径,该路径指向要将模型的主文件保存到的文件名(而不是目录!)。 (对于大多数具有较大尺寸的模型,该主文件旁边将有其他文件以及其他扩展名,这些文件也是模型保存的一部分。)

如果提供现有目录的路径,则会出现这样的错误。

© www.soinside.com 2019 - 2024. All rights reserved.