我没有太多在Stack Overflow上发布问题的经验。请原谅我的错误。我会尽量彻底的。
我有两个numpy数组
- X with shape (78300,90,90).
- y with shape (78300, 29)
- X是一个黑白图像数组,高度和宽度分别为(90,90)。
- y是x对应的编码类标签(编码为
y = tensorflow.keras.utils.to_categorical(labels)
)
我正在尝试用这些数据训练下面的CNN。
from tensorflow.keras import utils
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.callbacks import ModelCheckpoint
model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(90, 90, 1), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(1024, activation='sigmoid'))
model.add(Dense(29, activation='softmax'))
在运行
时收到以下错误n_classes = 29
batch = 64
epochs = 5
learning_rate = 0.001
adam = Adam(lr=learning_rate)
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
cp_callback = ModelCheckpoint(filepath=os.path.join("/output_dir", "result_folder"),
save_weights_only=True,
verbose=1)
history = model.fit(x,
y,
batch_size=batch,
epochs=epochs,
validation_split=0.1,
shuffle=True,verbose=1,
callbacks=[cp_callback])
: ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 90, 90]
形状应为(78300,90,90,1)
重塑x
(添加一个维度):
x = x[..., tf.newaxis]