【发布时间】:2022-01-07 16:34:18
【问题描述】:
我正在尝试制作一个 CNN 网络来对蘑菇图像进行预测。
遗憾的是,我什至无法开始训练我的模型,fit() 方法总是给我错误。
有 10 个类,tf 数据集根据它们的子文件夹正确找到了它们的名称。
使用我当前的代码,它说:
InvalidArgumentError: logits and labels must have the same first
dimension, got logits shape [12800,10] and labels shape [32]
模型总结:
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 64, 64, 3)] 0
conv2d_4 (Conv2D) (None, 62, 62, 32) 896
max_pooling2d_2 (MaxPooling (None, 20, 20, 32) 0
2D)
re_lu_2 (ReLU) (None, 20, 20, 32) 0
dense_2 (Dense) (None, 20, 20, 10) 330
=================================================================
这是我的代码:
#Data loading
train_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="training")
validation_set = keras.preprocessing.image_dataset_from_directory(
data_path,
labels="inferred",
label_mode="int",
batch_size=32,
image_size=(64, 64),
shuffle=True,
seed=1446,
validation_split = 0.2,
subset="validation")
#Constructing layers
input_layer = keras.Input(shape=(64, 64, 3))
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(input_layer)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = keras.layers.ReLU()(x)
output = layers.Dense(10, activation="softmax")(x)
#Making and fitting the model
model = keras.Model(inputs=input_layer, outputs=output)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_set, epochs=5, validation_data=validation_set)
【问题讨论】:
-
欢迎。请告诉我们您的错误到底是什么。另外,不要使用
input作为变量名,因为input是一个内置函数。 -
我更正了输入变量的名称。使用当前代码,它吐出:“logits 和 labels 必须具有相同的第一维,得到 logits 形状 [12800,10] 和标签形状 [32]”
标签: python tensorflow machine-learning keras conv-neural-network