没有为任何变量提供梯度 - Keras

时间:2021-02-08 14:10:55

标签: python tensorflow machine-learning keras deep-learning

我正在训练基于 Keras 框架的自动编码器,但在模型拟合过程中遇到错误。 我发布了完整的代码:

  • 数据集:
train_data_gen = image.ImageDataGenerator(rescale=1/255.)
valid_data_gen = image.ImageDataGenerator(rescale=1/255.)

train_gen = train_data_gen.flow_from_directory(
    training_path,
    class_mode = None,
    target_size = IMAGE_SIZE,
    color_mode = 'grayscale',
    batch_size = BS,
    seed = SEED,
    shuffle = 'Yes',
)

valid_gen = valid_data_gen.flow_from_directory(
    training_path,
    class_mode = None,
    target_size = IMAGE_SIZE,
    color_mode = 'grayscale',
    batch_size = BS,
    seed = SEED,
    shuffle = 'Yes',
)
  • 型号:
input_img = Input(shape=(256, 256, 1))

conv = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #256 x 256 x 32
pool = MaxPooling2D(pool_size=(2, 2))(conv) #128 x 128 x 32
conv = Conv2D(64, (3, 3), activation='relu', padding='same')(pool) #128 x 128 x 64
pool = MaxPooling2D(pool_size=(2, 2))(conv) #64 x 64 x 64
conv = Conv2D(128, (3, 3), activation='relu', padding='same')(pool) #64 x 64 x 128
pool = MaxPooling2D(pool_size=(2, 2))(conv) #32 x 32 x 128
conv = Conv2D(256, (3, 3), activation='relu', padding='same')(pool) #32 x 32 x 256
pool = MaxPooling2D(pool_size=(2, 2))(conv) #16 x 16 x 256
conv = Conv2D(512, (3, 3), activation='relu', padding='same')(pool) #16 x 16 x 512
flat = Flatten()(conv) # 16 x 16 x 512 -> 131072
encoder = Dense(units=2048, activation='relu')(flat) # 131072 -> 2048 (Bottleneck)

dense = Dense(131072, activation='relu')(encoder) 
reshape = Reshape((16, 16, 512))(dense) #16 x 16x 512
up = UpSampling2D((2,2))(reshape) #32 x 32 x 512
conv = Conv2D(256, (3, 3), activation='relu', padding='same')(up) # 32 x 32 x 256
up = UpSampling2D((2,2))(conv) # 64 x 64 x 256
conv = Conv2D(128, (3, 3), activation='relu', padding='same')(up) # 64 x 64 x 128
up = UpSampling2D((2,2))(conv) # 128 x 128 x 128
conv = Conv2D(64, (3, 3), activation='relu', padding='same')(up) # 128 x 128 x 64
up = UpSampling2D((2,2))(conv) # 256 x 256 x 64
decoder = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up) # 256 x 256 x 1 

autoencoder = Model(input_img, decoder)
  • 优化器和训练:
model_callbacks = [
    EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20),
    ModelCheckpoint(os.path.join(checkpoint_path, 'autoencoder_best.h5'), monitor='val_acc', save_best_only=True, verbose=1)
]

autoencoder.compile(optimizer='adam', loss=tf.keras.losses.MeanSquaredError())
autoencoder.fit(train_gen, epochs=EPOCHS, batch_size=BS, validation_data=valid_gen, callbacks=model_callbacks)

错误信息如下:

ValueError: 没有为任何变量提供梯度:['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'conv2d_2/kernel: 0', 'conv2d_2/bias:0', 'conv2d_3/kernel:0', 'conv2d_3/bias:0', 'conv2d_4/kernel:0', 'conv2d_4/bias:0', 'conv2d_5/kernel:0' , 'conv2d_5/bias:0', 'conv2d_6/kernel:0', 'conv2d_6/bias:0', 'conv2d_7/kernel:0', 'conv2d_7/bias:0', 'conv2d_8/kernel:0', ' conv2d_8/bias:0']。

此错误的原因可能是什么?

1 个答案:

答案 0 :(得分:0)

我想什么时候没有提供标签,它会抛出这个错误。

ValueError: No gradients provided for any variable: ['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'conv2d_2/kernel:0', 'conv2d_2/bias:0', 'conv2d_3/kernel:0', 'conv2d_3/bias:0', 'conv2d_4/kernel:0', 'conv2d_4/bias:0', 'conv2d_5/kernel:0', 'conv2d_5/bias:0', 'conv2d_6/kernel:0', 'conv2d_6/bias:0', 'conv2d_7/kernel:0', 'conv2d_7/bias:0', 'conv2d_8/kernel:0', 'conv2d_8/bias:0'].

设置 class_mode = 'input' 已经解决了这个问题。之后所有课程都将相同。