加载张量流模型时出现“ Unknown initializer:_initializer”

时间:2019-08-14 11:37:41

标签: python tensorflow

我使用xavier_initializer训练了一个模型,我将模型的代码粘贴得更低一些。问题是,当使用“从tensorflow.keras.models import load_model”加载该模型时,出现错误:

“ ValueError:未知的初始值设定项:_initializer”

我尝试添加

  • custom_objects = {“ xavier_initializer”:xavier_initializer} 或

  • custom_objects = {“ _ initializer”:xavier_initializer}

到load_model,但是它们都不起作用。我也尝试使用tf.keras.models.load_model和keras.models.load_model,两者都没有帮助。有任何解决办法的想法吗?

模型定义:

def initialize_model(activation_f, if_maxpooling, metric, optimizer):
    inputs = Input(shape = train_x.shape[1:])

    conv2d_1 = Conv2D(
        filters = 32,
        kernel_size = (3,3),
        strides = 1,
        activation = activation_f,
        kernel_initializer = initializer,
    )(inputs)
    batch_norm_1 = BatchNormalization()(conv2d_1)
    if not if_maxpooling:
        conv2d_2 = Conv2D(
            filters = 8,
            kernel_size = (3,3),
            strides = 2,
            activation = activation_f,
            kernel_initializer = initializer,
        )(batch_norm_1)
        batch_norm_2 = BatchNormalization()(conv2d_2)
        flatten = Flatten()(batch_norm_2)
    else:
        conv2d_2 = Conv2D(
            filters = 8,
            kernel_size = (3,3),
            strides = 1,
            activation = activation_f,
            kernel_initializer = initializer,
        )(batch_norm_1)
        batch_norm_2 = BatchNormalization()(conv2d_2)
        maxpool_1 = MaxPooling2D(
            pool_size = (3, 3),
            strides = 2
        )(batch_norm_2)
        flatten = Flatten()(maxpool_1)

    fully_connected_1 = Dense(
        256,
    )(flatten)
    batch_norm_3 = BatchNormalization()(fully_connected_1)
    fully_connected_2 = Dense(
        64,
        activation = activation_f
    )(batch_norm_3)
    batch_norm_4 = BatchNormalization()(fully_connected_2)

    outputs = Dense(10)(batch_norm_4)

    model = Model(
        inputs = inputs,
        outputs = outputs
    )

    if optimizer == RMSprop:
        model.compile(
            loss = mean_squared_error, # log_loss
            optimizer = RMSprop(0.001),
            metrics = [metric]
        )
    else:
        model.compile(
            loss = mean_squared_error, # log_loss
            optimizer = Adam(0.001, 0.9, 0.999),
            metrics = [metric]
        )

    return model

0 个答案:

没有答案