【问题标题】:Python3 Glove Attribute Error: 'generator' object has no attribute 'shape'Python3 Glove 属性错误:“生成器”对象没有属性“形状”
【发布时间】:2020-02-09 17:40:52
【问题描述】:

我正在尝试使用手套训练我的模型。我的代码如下:

#!/usr/bin/env python3

from __future__ import print_function
import argparse
import pprint
import gensim

from glove import Glove 
from tensorflow.python.keras.utils.data_utils import Sequence
def read_corpus(filename):

delchars = [chr(c) for c in range(256)]
delchars = [x for x in delchars if not x.isalnum()]
delchars.remove(' ')
delchars = ''.join(delchars)

with open(filename, 'r') as datafile:
    for line in datafile:
        yield line.lower().translate(None, delchars).split(' ')


if __name__ == '__main__':


base_path = "/home/hunzala_awan/vocab.pubmed1.txt"

get_data = read_corpus(base_path)

glove = Glove(no_components=100, learning_rate=0.05)
glove.fit(get_data, epochs=10, verbose=True) 

pprint.pprint(glove.most_similar("cancer", number=10))

当我尝试运行此代码时,我收到以下错误:

Traceback(最近一次调用最后一次): 文件“mytest3.py”,第 36 行,在 glove.fit(get_data, epochs=10, verbose=True) 文件“/usr/local/lib/python3.5/dist-packages/glove/glove.py”,第 86 行,适合 形状 = 矩阵. 形状 AttributeError:“生成器”对象没有属性“形状”

我错过了什么?对此问题的任何帮助将不胜感激。

提前致谢

【问题讨论】:

    标签: python-3.x tensorflow deep-learning glove


    【解决方案1】:

    我对 Glove 不熟悉,但它似乎不适用于生成器功能。您可以尝试提前生成它并转换为列表(它会更消耗内存):

    glove.fit(list(get_data), epochs=10, verbose=True)
    

    【讨论】:

    • 感谢您的回复,我这样做了,但现在出现了新错误:Traceback (most recent call last): File "mytest3.py", line 36, in <module> glove.fit(list(get_data), epochs=10, verbose=True) File "mytest3.py", line 25, in read_corpus yield line.lower().translate(None, delchars).split(' ') TypeError: translate() takes exactly one argument (2 given)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多