【问题标题】:How to merge a trained model and an untrained one?如何合并经过训练的模型和未经训练的模型?
【发布时间】:2020-05-21 17:34:48
【问题描述】:

我很难做到以下几点:

  • 我有一个模型,我已经训练了 125,089,410 个可训练参数;
  • 模型输出两个形状为 (None, 96) 的张量;
  • 我想通过冻结之前模型的层来构建一个新模型,然后输出一个 (None, 96) 张量。

重要提示:我无意在我训练的原始模型中添加更多层。

这是我一直在尝试的:

def get_output_model (prev_model):

    # Freezing prev model
    for l in prev_model.layers:
        l.trainable = False

    # Compiling so it won't complain about parameters number
    prev_model.compile(loss='binary_crossentropy')

    # Sanity check
    print (prev_model.summary())

    # Loss function
    def loss_fn(y_true, y_pred):
        pass # This doesn't matter here

    # Building model
    out_model_in = prev_model.output
    out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model_in)
    out_model = tf.keras.layers.Activation('relu')(out_model)
    out_model = tf.keras.layers.BatchNormalization()(out_model)
    out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model)
    out_model = tf.keras.layers.Activation('relu')(out_model)
    out_model = tf.keras.layers.BatchNormalization()(out_model)
    out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model)
    out_model = tf.keras.layers.Activation('relu')(out_model)
    out_model = tf.keras.layers.BatchNormalization()(out_model)
    out_model = tf.keras.layers.Dense(MAX_LEN, activation='linear')(out_model)
    out_model = tf.keras.layers.Activation('softmax')(out_model)
    model = tf.keras.models.Model(inputs=[out_model_in], outputs=[out_model])
    model.compile(loss=loss_fn, optimizer='nadam', metrics=['accuracy', f1_m])

但它给了我:

ValueError: Layer dense_49 expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor 'activation_57/Identity:0' shape=(None, 96) dtype=float32>, <tf.Tensor 'activation_58/Identity:0' shape=(None, 96) dtype=float32>]

我了解此错误是意料之中的,但我不知道如何解决此问题。

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0 tf.keras


    【解决方案1】:

    嗯,我找到了一种方法,它对我有用:

    def get_output_model (prev_model):
    
        # Freezing prev model
        for l in prev_model.layers:
            l.trainable = False
    
        # Compiling so it won't complain about parameters number
        prev_model.compile(loss='binary_crossentropy')
    
        # Sanity check
        print (prev_model.summary())
    
        # Loss function
        def loss_fn(y_true, y_pred):
            pass # This doesn't matter here
    
        # Building model
        x1 = prev_model.output[0]
        x2 = prev_model.output[1]
        out_model = tf.keras.layers.Add()([x1, x2])
        out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model)
        out_model = tf.keras.layers.Activation('relu')(out_model)
        out_model = tf.keras.layers.BatchNormalization()(out_model)
        out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model)
        out_model = tf.keras.layers.Activation('relu')(out_model)
        out_model = tf.keras.layers.BatchNormalization()(out_model)
        out_model = tf.keras.layers.Dense(2 * MAX_LEN, activation='linear')(out_model)
        out_model = tf.keras.layers.Activation('relu')(out_model)
        out_model = tf.keras.layers.BatchNormalization()(out_model)
        out_model = tf.keras.layers.Dense(MAX_LEN, activation='linear')(out_model)
        out_model = tf.keras.layers.Activation('softmax')(out_model)
        model = tf.keras.models.Model(inputs=[prev_model.input], outputs=[out_model])
        model.compile(loss=loss_fn, optimizer='nadam', metrics=['accuracy', f1_m])
        return model
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2018-04-21
      • 2018-01-30
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      相关资源
      最近更新 更多