我正在尝试使用 CIFAR-10 数据集创建一个用于分类的 Conv-6 CNN。该 CNN 使用 3 个块,其中每个块有 2 个卷积层,后跟一个最大池化层。在传递到输出层之前,它被展平并传递到两个密集层。
我的代码是:
# input image dimensions
img_rows, img_cols = 32, 32
# Load CIFAR-10 dataset-
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
if tf.keras.backend.image_data_format() == 'channels_first':
X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
input_shape = (3, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3)
input_shape = (img_rows, img_cols, 3)
print("\n'input_shape' which will be used = {0}\n".format(input_shape))
# 'input_shape' which will be used = (32, 32, 3)
# Convert datasets to floating point types-
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# Normalize the training and testing datasets-
X_train /= 255.0
X_test /= 255.0
# Convert class vectors/target to binary class matrices or one-hot encoded values-
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
print("\nDimensions of training and testing sets are:")
print("X_train.shape = {0}, y_train.shape = {1}".format(X_train.shape, y_train.shape))
print("X_test.shape = {0}, y_test.shape = {1}".format(X_test.shape, y_test.shape))
# Dimensions of training and testing sets are:
# X_train.shape = (50000, 32, 32, 3), y_train.shape = (50000, 10)
# X_test.shape = (10000, 32, 32, 3), y_test.shape = (10000, 10)
class Conv6(Model):
def __init__(self, **kwargs):
super(Conv6, self).__init__(**kwargs)
self.conv1 = Conv2D(
filters = 64, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.conv2 = Conv2D(
filters = 64, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.pool1 = MaxPooling2D(
pool_size = (2, 2),
strides = (2, 2)
)
self.conv3 = Conv2D(
filters = 128, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.conv4 = Conv2D(
filters = 128, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.pool2 = MaxPooling2D(
pool_size = (2, 2),
strides = (2, 2)
)
self.conv5 = Conv2D(
filters = 256, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.conv6 = Conv2D(
filters = 256, kernel_size = (3, 3),
activation = 'relu', kernel_initializer = tf.initializers.GlorotNormal(),
strides = (1, 1), padding = 'same'
)
self.flatten = Flatten()
self.dense1 = Dense(
units = 256, activation = 'relu',
kernel_initializer = tf.initializers.GlorotNormal()
)
self.dense2 = Dense(
units = 256, activation = 'relu',
kernel_initializer = tf.initializers.GlorotNormal()
)
self.op = Dense(
units = num_classes, activation = 'softmax'
)
def call(self, inputs):
x = self.conv1(inputs)
x = self.conv2(x)
x = self.pool1(inputs)
x = self.conv3(x)
x = self.conv4(x)
x = self.pool2(x)
x = self.conv5(x)
x = self.conv6(x)
x = self.flatten(x)
x = self.dense1(x)
x = self.dense2(x)
return self.op(x)
# Initialize a Conv-6 CNN model-
model = Conv6()
# Compile defined model-
model.compile(
loss=tf.keras.losses.categorical_crossentropy,
# optimizer='adam',
optimizer=tf.keras.optimizers.Adam(lr = 0.0003),
metrics=['accuracy']
)
# Define early stopping callback-
early_stopping_callback = tf.keras.callbacks.EarlyStopping(
monitor = 'val_loss', min_delta = 0.001,
patience = 3)
# Train defined and compiled model-
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, shuffle = True,
epochs = num_epochs,
callbacks = [early_stopping_callback],
validation_data = (X_test, y_test)
)
在调用“model.fit()”时,它给了我以下警告:
警告:tensorflow:变量不存在梯度 ['conv6/conv2d/kernel:0', 'conv6/conv2d/bias:0', 'conv6/conv2d_1/kernel:0', 'conv6/conv2d_1/bias:0' ] 当最小化损失时。警告:tensorflow:变量不存在梯度 ['conv6/conv2d/kernel:0', 'conv6/conv2d/bias:0', 'conv6/conv2d_1/kernel:0', 'conv6/conv2d_1/bias:0' ] 当最小化损失时。
尽管有警告,但定义的 CNN 模型在 9 个 epoch 中达到了约 72% 的验证准确度。
为什么我会收到这些警告?
谢谢!