gpt4 book ai didi

python - reshape 数组后keras中的索引错误

转载 作者:行者123 更新时间:2023-12-04 08:10:05 25 4
gpt4 key购买 nike

我将 .csv 文件用于图像数据,我将其 reshape 为 2d numpy 数组来表示图像。
我不确定我错过了什么,并且在拟合模型时我不断收到“IndexError:list index out of range”。

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 .
此外,您正在将它们 reshape 为 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)

关于python - reshape 数组后keras中的索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66018721/

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