gpt4 book ai didi

python - 如何修复 Tensorflow 中的 "IndexError: list index out of range"

转载 作者:行者123 更新时间:2023-11-28 18:57:49 24 4
gpt4 key购买 nike

我正在使用 Tensorflow 和 Keras 创建图像分类器,但是当我尝试训练我的模型时出现错误:

IndexError: list index out of range.

我认为问题出在我的模型上,因为当我删除 conv2D 层时,代码不会抛出任何错误。

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?

我得到的错误是:

    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

最佳答案

为了社区的利益,在答案中明确阐述@Anubhav Singh 的评论。

model = Sequential() 之后,第一个卷积层应该包含 input_shape 作为其参数之一。

示例代码片段如下所示:

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

行中还需要更正,model.compile。参数的名称应该是 metrics 而不是 metrics

关于python - 如何修复 Tensorflow 中的 "IndexError: list index out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56155048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com