Keras将顺序类型转换为功能类型,并节省了加载权重的机会

时间:2019-05-07 18:08:00

标签: tensorflow keras

我在重新编写对功能的顺序描述时遇到了问题。

Using keras==2.1。我有一个顺序描述的旧代码(2-3年前编写):

def Word2VecModel(num_words, embedding_dim, seq_length, dropout_rate):
    model = Sequential()
    model.add(Embedding(num_words, embedding_dim, 
        input_length=seq_length, trainable=False))
    model.add(LSTM(units=512, return_sequences=True, input_shape=(seq_length, embedding_dim)))
    model.add(Dropout(dropout_rate))
    model.add(LSTM(units=512, return_sequences=False))
    model.add(Dropout(dropout_rate))
    model.add(Dense(1024, activation='tanh'))
    return model

def img_model(dropout_rate):
    model = Sequential()
    model.add(Dense(1024, input_shape=(4096,), activation='tanh'))
    return model

def vqa_model(num_words, embedding_dim, seq_length, dropout_rate, num_classes):
    vgg_model = img_model(dropout_rate)
    lstm_model = Word2VecModel(num_words, embedding_dim, seq_length, dropout_rate)
    fc_model = Sequential()
    fc_model.add(Concatenate([vgg_model, lstm_model]))
    fc_model.add(Dropout(dropout_rate))
    fc_model.add(Dense(1000, activation='tanh'))
    fc_model.add(Dropout(dropout_rate))
    fc_model.add(Dense(num_classes, activation='softmax'))
    fc_model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
        metrics=['accuracy'])
    return fc_model

我的功能描述版本:

def Word2VecModel(num_words, embedding_dim, seq_length, dropout_rate):

    w2v_input = Input((seq_length,))
    w2v_embed = Embedding(input_dim=num_words, output_dim=embedding_dim, input_length=seq_length,
                          trainable=False)(w2v_input)
    w2v_lstm1 = LSTM(512, input_shape=(seq_length, embedding_dim),return_sequences=True)(w2v_embed)
    w2v_drop1 = Dropout(dropout_rate)(w2v_lstm1)
    w2v_lstm2 = LSTM(512, return_sequences=False)(w2v_drop1)
    w2v_drop2 = Dropout(dropout_rate)(w2v_lstm2)
    w2v_dense = Dense(1024, activation='tanh')(w2v_drop2)
    model = Model(w2v_input, w2v_dense)
    return model
def img_model(dropout_rate):
    img_input = Input((4096,))
    img_dense = Dense(1024, activation='tanh')(img_input)
    model = Model(img_input, img_dense)
    return model
def vqa_model(num_words, embedding_dim, seq_length, dropout_rate, num_classes):
    vgg_model = img_model(dropout_rate)
    lstm_model = Word2VecModel(num_words, embedding_dim, seq_length, dropout_rate)
    fc_concat = Concatenate()([vgg_model.output, lstm_model.output])
    fc_drop1 = Dropout(dropout_rate)(fc_concat)
    fc_dense1 = Dense(1000, activation='tanh')(fc_drop1)
    fc_drop2 = Dropout(dropout_rate)(fc_dense1)
    fc_dense2 = Dense(num_classes, activation='softmax')(fc_drop2)
    fc_model = Model([vgg_model.input, lstm_model.input], fc_dense2)
    fc_model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
        metrics=['accuracy'])
    return fc_model

首先,我无法编辑旧版本使其可执行。 其次,当我尝试将经过训练的顺序类型的权重加载到函数中时,出现了错误:

ValueError: Layer #0 (named "embedding_1" in the current model) was
 found to correspond to layer dense_1 in the save file. However the 
new layer embedding_1 expects 1 weights, but the saved weights have 2 
elements.

所以,我只想有可能将旧的权重加载到新的功能nn中。有可能吗?

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为旧代码是写在keras==1.x

上的