我正在尝试通过Keras更好地理解深度学习。我已经安装了Python、pip、TensorFlow和Jupyter Notebook来运行这个程序,但是根据来自towardsdatascience.com的“使用Keras进行深度学习的介绍”中的以下示例,我已经遇到了一个错误。如果这看起来很明显,请原谅我,这是我第一次做这种事情,当你第一次运行的示例中就出现了错误时,很难判断问题所在。
给出的第一块代码是:
from keras.datasets import mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()
它声明使用TensorFlow后端。给出的第二块代码是:
x_train = x_train.astype('float32')x_test = x_test.astype('float32')x_train /= 255x_test /= 255x_train = X_train.reshape(X_train.shape[0], 28, 28, 1)x_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
这会引发以下错误:
---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-2-e2db9b91827f> in <module> 4 x_test /= 255 5 ----> 6 x_train = X_train.reshape(X_train.shape[0], 28, 28, 1) 7 x_test = X_test.reshape(X_test.shape[0], 28, 28, 1)NameError: name 'X_train' is not defined
然而,我不明白在这种情况下x_train怎么会未定义,因为显然有一行代码是“x_train = x_train.astype(‘float32’)”
回答:
实际上,这只是示例中的大小写问题。x_test和x_train都被更改为
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
非常感谢oneturkmen指出这一点。