简体   繁体   English

在构建和编译keras顺序模型时“列表索引超出范围”

[英]“list index out of range” while building and compiling keras sequential model

I was trying to build CNN model for classifying mnist data. 我试图建立CNN模型以对mnist数据进行分类。 Thus I tried these codes, but 'list index out of range' error has been raised 因此,我尝试了这些代码,但是出现了“列表索引超出范围”错误

I'm working with python 3.6 and tensorflow 1.12.0, Windows10, and my IDE is PyCharm. 我正在使用python 3.6和tensorflow 1.12.0,Windows10,并且我的IDE是PyCharm。

sess = tf.Session()

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
y_train = sess.run(tf.one_hot(y_train, 10))
y_test = sess.run(tf.one_hot(y_test, 10))

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=32, kernel_size=[3, 3], strides=[1, 1],
                       padding='same', activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(pool_size=[2, 2], strides=2, padding='same'),
    tf.keras.layers.Dropout(rate=0.3),
    tf.keras.layers.Conv2D(filters=64, kernel_size=[3, 3], strides=[1, 1],
                       padding='same', activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(pool_size=[2, 2], strides=2, padding='same'),
    tf.keras.layers.Dropout(rate=0.3),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(units=512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(rate=0.5),
    tf.keras.layers.Dense(units=10, activation=tf.nn.softmax)
 ])

model.compile(optimizer='adam',
          loss='categorical_crossentropy',
          metrics=['accuracy'])

model.fit(x_train, y_train, epochs=15)
print("Accuracy: {}".format(model.evaluate(x_test, y_test)))

but the result was like this: 但结果是这样的:

Traceback (most recent call last):
  File 
"C:/Users/wltjd/Desktop/Computer/TensorFlow/mnist_using_CNN_advanced.py", line 38, in <module>
model.fit(x_train, y_train, epochs=15)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1536, in fit
validation_split=validation_split)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\engine\training.py", line 992, in _standardize_user_data
class_weight, batch_size)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1032, in _standardize_weights
self._set_inputs(x)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 474, in _method_wrapper
method(self, *args, **kwargs)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1242, in _set_inputs
self.build(input_shape=input_shape)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 222, in build
layer.build(shape)
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 190, in build
self.rank + 2))
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 828, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
  File "C:\Users\wltjd\Anaconda3\envs\TensorFlow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 616, in __getitem__
return self._dims[key]
IndexError: list index out of range

The error IndexError: list index out of range simply means you are trying to access a location within a list that doesn't exist. 错误IndexError: list index out of range仅表示您正在尝试访问列表中不存在的位置。

Here, you are trying to fit x_train with y_train with one of these dataframes larger than another. 在这里,您尝试将x_trainy_train配合使用,其中一个数据y_train大于另一个数据y_train I would try comparing the lengths of x_train and y_train to see which one is larger than the other and then change the sizes so they both are of equal length. 我会尝试比较x_trainy_train的长度,以查看哪个大于另一个,然后更改大小,使它们的长度相等。

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

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