对带有嵌入层的keras模型进行序列化

时间:2018-08-01 21:17:17

标签: tensorflow keras word-embedding

我已经用这样的预训练词嵌入训练了一个模型:

embedding_matrix = np.zeros((vocab_size, 100))
for word, i in text_tokenizer.word_index.items():
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None:
        embedding_matrix[i] = embedding_vector

embedding_layer = Embedding(vocab_size,
                        100,
                        embeddings_initializer=Constant(embedding_matrix),
                        input_length=50,
                        trainable=False)

架构如下:

sequence_input = Input(shape=(50,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
text_cnn = Conv1D(filters=5, kernel_size=5, padding='same',     activation='relu')(embedded_sequences)
text_lstm = LSTM(500, return_sequences=True)(embedded_sequences)


char_in = Input(shape=(50, 18, ))
char_cnn = Conv1D(filters=5, kernel_size=5, padding='same', activation='relu')(char_in)
char_cnn = GaussianNoise(0.40)(char_cnn)
char_lstm = LSTM(500, return_sequences=True)(char_in)



merged = concatenate([char_lstm, text_lstm]) 

merged_d1 = Dense(800, activation='relu')(merged)
merged_d1 = Dropout(0.5)(merged_d1)

text_class = Dense(len(y_unique), activation='softmax')(merged_d1)
model = Model([sequence_input,char_in], text_class)

当我将模型转换为json时,出现此错误:

ValueError: can only convert an array of size 1 to a Python scalar

类似地,如果我使用model.save()函数,它似乎可以正确保存,但是当我加载它时,我得到了Type Error: Expected Float32

我的问题是:尝试序列化此模型时,我是否缺少某些东西?我需要某种Lambda层还是某种类型的层?

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

您可以在weights层中使用Embedding参数来提供初始权重。

embedding_layer = Embedding(vocab_size,
                            100,
                            weights=[embedding_matrix],
                            input_length=50,
                            trainable=False)

在保存/加载模型后,权重应保持不可训练的状态:

model.save('1.h5')
m = load_model('1.h5')
m.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_3 (InputLayer)            (None, 50)           0
__________________________________________________________________________________________________
input_4 (InputLayer)            (None, 50, 18)       0
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 50, 100)      1000000     input_3[0][0]
__________________________________________________________________________________________________
lstm_4 (LSTM)                   (None, 50, 500)      1038000     input_4[0][0]
__________________________________________________________________________________________________
lstm_3 (LSTM)                   (None, 50, 500)      1202000     embedding_1[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 50, 1000)     0           lstm_4[0][0]
                                                                 lstm_3[0][0]
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 50, 800)      800800      concatenate_2[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 50, 800)      0           dense_2[0][0]
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 50, 15)       12015       dropout_2[0][0]
==================================================================================================
Total params: 4,052,815
Trainable params: 3,052,815
Non-trainable params: 1,000,000
__________________________________________________________________________________________________

答案 1 :(得分:0)

我希望您在编译后保存模型。喜欢:

    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

要保存模型,您可以执行以下操作:

    from keras.models import load_model

    model.save('model.h5')
    model = load_model('model_detect1.h5')
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)

要加载模型,

    from keras.models import model_from_json

    json_file = open('model.json', 'r')
    model_json = json_file.read()
    model = model_from_json(model_json)
    model.load_weights("model.h5")

答案 2 :(得分:0)

我尝试了多种方法。问题是当我们在嵌入层中工作时,pickle不起作用,并且无法保存数据。 因此,当您有以下几层时,您可以做什么:-

std::vector<Father>

然后,您可以使用 将h5扩展名保存为d = save文件,然后将其转换为json,此处将模型转换为model2

## Creating model
embedding_vector_features=100
model=Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))
model.add(LSTM(100))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())

这可以加载数据:-

from tensorflow.keras.models import load_model
model.save('model.h5')
model = load_model('model.h5')
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)