How to fix “IndexError: list index out of range” in Tensorflow

半城伤御伤魂 提交于 2021-02-19 04:29:48

问题


I'm creating an Image Classifier using Tensorflow and Keras, but when I tried to train my model I got an error:

IndexError: list index out of range.

I think the problem is with my model, because when I remove the conv2D layers, then the code throws no error.

model = Sequential()

model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
model.add(MaxPool2D((2,2),strides=(2,2)))

model.add(Conv2D(128,(3,3),activation='relu',padding='same'))
model.add(Conv2D(128,(3,3),activation='relu',padding='same'))
model.add(MaxPool2D((2,2),strides=(2,2)))

model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
model.add(MaxPool2D((2,2),strides=(2,2)))

model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
model.add(MaxPool2D((2,2),strides=(2,2)))

model.add(Flatten())
model.add(Dense(4096,activation='relu'))
model.add(Dense(4096,activation='relu'))
model.add(Dense(2,activation='softmax'))

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',
metrices=['accuracy'])

model.fit(x_train,y_train,epochs=10)

#What is wrong in this model?

The error I got is:

    IndexError Traceback (most recent call last)
<ipython-input-49-83b981a8bf39> in <module>()
----> 1 model.fit(x_train,y_train,10)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
   1534         steps_name='steps_per_epoch',
   1535         steps=steps_per_epoch,
-> 1536         validation_split=validation_split)
   1537 
   1538     # Prepare validation data.

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py in __getitem__(self, key)
    614         return TensorShape(self._dims[key])
    615       else:
--> 616         return self._dims[key]
    617     else:
    618       if isinstance(key, slice):

IndexError: list index out of range

回答1:


Elaborating the Comment of @Anubhav Singh in the Answer clearly for the benefit of Community.

After model = Sequential(), the first Convolution Layer should include the input_shape as one its arguments.

Example Code Snippet can be shown below:

model.add(Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1), adding='same'))

One more correction needed is in the line, model.compile. Name of the argument should be metrics instead of metrices.



来源:https://stackoverflow.com/questions/56155048/how-to-fix-indexerror-list-index-out-of-range-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!