【问题标题】:Introduction to Deep Learning with Keras: 'X_train' is not defined?Keras 深度学习简介:“X_train”未定义?
【发布时间】:2019-12-03 16:44:50
【问题描述】:

所以我试图通过 Keras 更好地理解深度学习。我已经安装了 python、pip、tensorflow 和 jupyter notebook 来运行它,但是基于来自面向datascience.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 /= 255
x_test /= 255

x_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_train而不是X_train
  • 解决了。谢谢。

标签: python tensorflow machine-learning keras jupyter-notebook


【解决方案1】:

这实际上只是示例中的一个大小写问题。 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 注意到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2018-02-03
    • 1970-01-01
    • 2016-12-23
    • 2021-04-08
    • 2017-01-03
    • 2018-10-25
    • 2021-05-26
    相关资源
    最近更新 更多