重塑数组后,在keras中出现索引错误



我使用。csv文件作为图像数据,我将其重塑为2d numpy数组来表示图像。

我不知道我错过了什么,我不断得到一个&;IndexError:列表索引超出范围&;

train_data = pd.read_csv('sign_mnist_train.csv')
test_data = pd.read_csv('sign_mnist_test.csv')
train_labels = train_data.pop('label').to_numpy().reshape(-1, 1)
test_labels = test_data.pop('label').to_numpy().reshape(-1, 1)
train_images = train_data.to_numpy().reshape(-1, 28, 28, 1) / 255.0
test_images = test_data.to_numpy().reshape(-1, 28, 28, 1) / 255.0
def make_model():
model = models.Sequential()

model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPool2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPool2D(2,2))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64))
model.add(layers.Dense(25))
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy,
metrics=['accuracy'])

return model
model = make_model()
model.fit(train_images, train_labels, epochs=5, validation_data(test_images, test_labels))

完整的回溯:1 2 3

我想问题是来自这个train.data,根据您命名为test_data的测试图像。此外,你正在将它们重塑为4D张量,而不是基于代码的2D张量。

train_images = train_data.to_numpy().reshape(-1, 28, 28, 1) 
test_images = test_data.to_numpy().reshape(-1, 28, 28, 1) 
train_images = train_images / 255.0
test_images = test_images / 255.0

编辑:检查对目标变量应用one-hot编码是否有帮助

train_labels = tf.keras.utils.to_categorical(train_labels)

相关内容

  • 没有找到相关文章

最新更新