【问题标题】:missing attribute in lda2vec module in notebook笔记本中的lda2vec模块中缺少属性
【发布时间】:2021-02-08 17:33:50
【问题描述】:

jupyter notebook 无法从 lda2vec 导入 dirichlet_likelihood.py。
此 py 文件存在于当前 lda2vec 的 github 中。

我安装了模块并打开了工作簿,然后尝试运行它。我怀疑我的问题有一个非常简单的原因。

笔记本是 https://github.com/cemoody/lda2vec/blob/master/examples/twenty_newsgroups/lda2vec/lda2vec.ipynb

当我在 python 命令行(在当前环境中)尝试以下操作时,它不会给出以下错误,而是需要我安装的 keras。在命令行上它说它不能导入预处理。

uname -a
Linux ubuntu 4.18.0-15-generic #16~18.04.1-Ubuntu SMP Thu Feb 7 14:06:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
sudo apt-get install python3-venv
python3.6 -m venv .env  
source .env/bin/activate
pip install --upgrade pip
pip install jupyter
pip install lda2vec
from lda2vec import preprocess, Corpus
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-2b87256bea6b> in <module>
----> 1 from lda2vec import preprocess, Corpus
      2 import matplotlib.pyplot as plt
      3 import numpy as np
      4 get_ipython().run_line_magic('matplotlib', 'inline')
      5 

~/.env/lib/python3.6/site-packages/lda2vec/__init__.py in <module>
----> 1 import lda2vec.dirichlet_likelihood as dirichlet_likelihood
      2 import lda2vec.embedding_mixture as embedding_mixture
      3 from lda2vec.Lda2vec import Lda2vec as model
      4 import lda2vec.word_embedding as word_embedding
      5 import lda2vec.nlppipe as nlppipe

AttributeError: module 'lda2vec' has no attribute 'dirichlet_likelihood'
python
from lda2vec import preprocess, Corpus
Using TensorFlow backend.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'preprocess'

编辑

我通过做这些事情得到了它 - 获取 ubuntu-18.04.2-desktop-amd64.iso - 为 hyper-V 更改 BIOS 虚拟化设置 - 在 VMWare 中制作虚拟机 - 将内存增加到 3G - 给它40G的磁盘

然后在终端中

sudo apt install python2.7
sudo apt install python-pip
pip install virtualenv
Mkdir 2.7env
Cd 2.7env
python2.7 -m venv .env
python2.7 -m virtualenv .env
source .env/bin/activate
pip install --upgrade pip
pip install jupyter
pip install -U spacy
python -m spacy download en
pip install wheel nltk gensim pyLDAvis lda2vec
sudo apt install git
git clone https://github.com/cemoody/lda2vec.git
cp ~/lda2vec/build/lib.linux-x86_64-2.7/lda2vec/corpus.py ~/2.7env/.env/lib/python2.7/site-packages/lda2vec/Corpus.py
cp ~/lda2vec/build/lib.linux-x86_64-2.7/lda2vec/preprocess.py ~/2.7env/.env/lib/python2.7/site-packages/lda2vec/preprocess.py
python -m pip install ipykernel
python -m ipykernel install --user
python lda2vec/examples/twenty_newsgroups/lda2vec/lda2vec_run.py
cd lda2vec/examples/twenty_newsgroups/lda2vec/
jupyter notebook
change the kernel to 2

在火狐打开lda2vec.ipynb

如上所述,我现在正试图让它重新创建二十个新闻组 npz 文件,以便我最终可以提供自己的内容。如果有人更好地理解这一点,我怀疑尝试在 RAM 很少的 VM 上运行此脚本可能是问题,但错误报告为

(.env) craig@ubuntu:~/whcjimmy/lda2vec/examples/twenty_newsgroups/data$ python preprocess.py 
Traceback (most recent call last):
  File "preprocess.py", line 31, in <module>
    n_threads=4)
  File "/home/craig/whcjimmy/.env/lib/python3.6/site-packages/lda2vec-0.1-py3.6.egg/lda2vec/preprocess.py", line 104, in tokenize
    vocab = {v: nlp.vocab[v].lower_ for v in uniques if v != skip}
  File "/home/craig/whcjimmy/.env/lib/python3.6/site-packages/lda2vec-0.1-py3.6.egg/lda2vec/preprocess.py", line 104, in <dictcomp>
    vocab = {v: nlp.vocab[v].lower_ for v in uniques if v != skip}
  File "vocab.pyx", line 242, in spacy.vocab.Vocab.__getitem__
  File "lexeme.pyx", line 44, in spacy.lexeme.Lexeme.__init__
  File "vocab.pyx", line 157, in spacy.vocab.Vocab.get_by_orth
  File "strings.pyx", line 138, in spacy.strings.StringStore.__getitem__
KeyError: "[E018] Can't retrieve string for hash '9243420536193520'. This usually refers to an issue with the `Vocab` or `StringStore`."

【问题讨论】:

  • 您是否创建了名为 lda2vec.py 或文件夹 lda2vec.py 的文件?如果你有它,那么import 加载这个文件(或文件夹)而不是模块lda2vec,它在你的文件/文件夹中找不到preprocess。删除lda2vec.py 或重命名。
  • 问题是~/.env/lib/python3.6/site-packages/lda2vec/dirichlet_likelihood.py 存在,但__init__.pyimport lda2vec.dirichlet_likelihood as dirichlet_likelihood 导致错误module 'lda2vec' has no attribute 'dirichlet_likelihood'
  • 也许它需要一些其他模块来运行它。我几分钟前安装了lda2vec,我还必须安装pyLDAvis 才能导入lda2vec。 Linux Mint 19.2、Python 3.7.4
  • 我解决了这个问题 - 结果发现 dirichlet_likelihood.py 只是丢失了,并且没有从 git repo 复制到正确的位置。如果您按照上面的编辑说明操作,它可以在 2.7 下运行。

标签: python jupyter-notebook


【解决方案1】:

好的,我得到了这个工作。问题是

  1. 选择正确的 python 来运行一个有 4 年历史的 git 项目。 Python 2.7。
  2. 检查安装的模块是否有来自 git repo 的代码
  3. 在 python 终端中解决问题
  4. 复制和编辑python文件转到3

上面的一个问题与依赖项的 API 更改有关。 ImportError: No module named 'spacy.en' 最初的问题可能是由于我不熟悉的 git 或 python 引起的。 git 项目自检仍然失败,构建失败。 但我的 jupyter notebook 正在运行并产生令人信服的输出。

【讨论】:

  • 嗨,谢谢分享。您还可以与运行代码共享笔记本吗?我们很多人都会非常感激它。谢谢!
  • 您好,“正在运行”的笔记本正在使用预先生成的 npz 文件。我遇到的问题是,我无法让它运行 preprocess.py 以用于 20_newsgroups 示例并吃掉一些新内容。最新的是github.com/whcjimmy/lda2vec fork 工作,包括笔记本,但 spacy 与 [E018] Can't retrieve string for hash '9243420536193520' 中断。这通常是指VocabStringStore 的问题
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2015-05-27
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
相关资源
最近更新 更多