简体   繁体   English

如何修复 AttributeError: 'module' 对象没有属性 'function'?

[英]How to fix AttributeError: 'module' object has no attribute 'function'?

I'm totally new to Python and I currently working on the program in Michael Nielsen's online book: "neural-networks-and-deep-learning", while I'm trying to run a pre-made module named mnist_loader.py, a function named load_data_wrapper() within the module is not attributed.我对 Python 完全陌生,我目前正在研究 Michael Nielsen 的在线书籍:“神经网络和深度学习”中的程序,同时我正在尝试运行一个名为 mnist_loader.py 的预制模块,一个模块中名为 load_data_wrapper() 的函数没有属性。

The link of the code can be found here: https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/src/mnist_loader.py代码链接可以在这里找到: https : //github.com/mnielsen/neural-networks-and-deep-learning/blob/master/src/mnist_loader.py

Here's the code for the module, mnist_loader.py:下面是模块的代码,mnist_loader.py:

import cPickle

import gzip

import numpy as np

def load_data():
    f = gzip.open('../data/mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = cPickle.load(f)
    f.close()
    return (training_data, validation_data, test_data)

def load_data_wrapper():
    tr_d, va_d, te_d = load_data()
    training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
    training_results = [vectorized_result(y) for y in tr_d[1]]
    training_data = zip(training_inputs, training_results)
    validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
    validation_data = zip(validation_inputs, va_d[1])
    test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
    test_data = zip(test_inputs, te_d[1])
    return (training_data, validation_data, test_data)

def vectorized_result(j):
    e = np.zeros((10, 1))
    e[j] = 1.0
    return e

While I'm trying the run the following code:当我尝试运行以下代码时:

>>> import mnist_loader
>>> training_data, validation_data, test_data = \
... mnist_loader.load_data_wrapper()

The interpreter shows:解释器显示:

AttributeError: 'module' object has no attribute 'load_data_wrapper'

I check the directory and the mnist_loader.py module has no error itself.我检查了目录,并且 mnist_loader.py 模块本身没有错误。 Then I tried to take the function load_data_wrapper out and use it by itself by using:然后我尝试取出函数 load_data_wrapper 并通过使用它自己使用它:

from mnist_loader import load_data_wrapper 

and it shows:它显示:

ImportError: cannot import name load_data_wrapper

I expect the code to load the mnist.pkl.gz file and output MNIST data.我希望代码加载 mnist.pkl.gz 文件并输出 MNIST 数据。

You need to store it in the same directory, where stored your file, in which you try import.您需要将其存储在您尝试导入的文件所在的同一目录中。

Oh, if you are using JupyterLab, perhaps, you need to do something like this:哦,如果你正在使用 JupyterLab,也许你需要做这样的事情:

$ # Imports the workspace file `file_name.json`.
$ jupyter lab workspaces import file_name.json
Saved workspace: <workspaces-directory>/labworkspacesfoo-54d5.jupyterlab-workspace

Items 7.2 & 7.6 here: JupyterLab manual此处的 7.2 和 7.6 项: JupyterLab 手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM