程序显示“ NoneType”对象没有属性“ strip”

时间:2019-05-09 07:31:19

标签: keras deep-learning question-answering

当我执行此处可用的代码时 https://keras.io/examples/babi_memnn/

我发现了这个错误

runfile('E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py', wdir='E:/Courses/PhD/ML/Sample Prog/Practicals')
Using TensorFlow backend.
Extracting stories for the challenge: single_supporting_fact_10k
Traceback (most recent call last):

  File "<ipython-input-1-9db756ed995f>", line 1, in <module>
    runfile('E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py', wdir='E:/Courses/PhD/ML/Sample Prog/Practicals')

  File "C:\Users\Khubaib\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\Khubaib\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py", line 107, in <module>
    train_stories = get_stories(tar.extractfile(challenge.format('train')))

  File "E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py", line 65, in get_stories
    data = parse_stories(f.readlines(), only_supporting=only_supporting)

  File "E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py", line 52, in parse_stories
    sent = tokenize(line)

  File "E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py", line 22, in tokenize
    return [x.strip() for x in re.split(r'(\W+)?', sent) if x.strip()]

  File "E:/Courses/PhD/ML/Sample Prog/Practicals/memNetworkBabi.py", line 22, in <listcomp>
    return [x.strip() for x in re.split(r'(\W+)?', sent) if x.strip()]

AttributeError: 'NoneType' object has no attribute 'strip'

1 个答案:

答案 0 :(得分:0)

更改re.split(r'(\W+)?', sent) 到第37行的re.split(r'(\W+)+', sent) 为我解决了该问题,另请参见Keras中的拉取请求:https://github.com/keras-team/keras/pull/13519