所以我设计了一个 CNN 并使用以下参数进行编译,
training_file_loc = "8-SignLanguageMNIST/sign_mnist_train.csv"
testing_file_loc = "8-SignLanguageMNIST/sign_mnist_test.csv"
def getData(filename):
images = []
labels = []
with open(filename) as csv_file:
file = csv.reader(csv_file, delimiter = ",")
next(file, None)
for row in file:
label = row[0]
data = row[1:]
img = np.array(data).reshape(28,28)
images.append(img)
labels.append(label)
images = np.array(images).astype("float64")
labels = np.array(labels).astype("float64")
return images, labels
training_images, training_labels = getData(training_file_loc)
testing_images, testing_labels = getData(testing_file_loc)
print(training_images.shape, training_labels.shape)
print(testing_images.shape, testing_labels.shape)
training_images = np.expand_dims(training_images, axis = 3)
testing_images = np.expand_dims(testing_images, axis = 3)
training_datagen = ImageDataGenerator(
rescale = 1/255,
rotation_range = 45,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = "nearest"
)
training_generator = training_datagen.flow(
training_images,
training_labels,
batch_size = 64,
)
validation_datagen = ImageDataGenerator(
rescale = 1/255,
rotation_range = 45,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = "nearest"
)
validation_generator = training_datagen.flow(
testing_images,
testing_labels,
batch_size = 64,
])
但是,当我运行 model.fit() 时,出现以下错误,
ValueError: Shapes (None, 1) and (None, 24) are incompatible
将损失函数更改为 后sparse_categorical_crossentropy,程序运行良好。
我不明白为什么会这样。
谁能解释这一点以及这些损失函数之间的区别?
largeQ
BIG阳
相关分类