简体   繁体   English

减小Keras LSTM型号的尺寸

[英]Reduce the size of Keras LSTM model

Essentially, I am training an LSTM model using Keras, but when I save it, its size takes up to 100MB. 基本上,我正在使用Keras训练LSTM模型,但是当我保存它时,它的大小需要100MB。 However, my purpose of the model is to deploy to a web server in order to serve as an API, my web server is not able to run it since the model size is too big. 但是,我的模型的目的是部署到Web服务器以作为API,我的Web服务器无法运行它,因为模型大小太大。 After analyzing all parameters in my model, I figured out that my model has 20,000,000 parameters but 15,000,000 parameters are untrained since they are word embeddings. 在分析了我的模型中的所有参数后,我发现我的模型有20,000,000参数,但是15,000,000参数是未经训练的,因为它们是字嵌入。 Is there any way that I can minimize the size of the model by removing that 15,000,000 parameters but still preserving the performance of the model? 有没有什么方法可以通过删除15,000,000参数来最小化模型的大小,但仍保留模型的性能? Here is my code for the model: 这是我的模型代码:

def LSTModel(input_shape, word_to_vec_map, word_to_index):


    sentence_indices = Input(input_shape, dtype="int32")

    embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)


    embeddings = embedding_layer(sentence_indices)


    X = LSTM(256, return_sequences=True)(embeddings)
    X = Dropout(0.5)(X)
    X = LSTM(256, return_sequences=False)(X)
    X = Dropout(0.5)(X)    
    X = Dense(NUM_OF_LABELS)(X)
    X = Activation("softmax")(X)

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

    return model

Define the layers you want to save outside the function and name them. 定义要在函数外部保存的图层并命名它们。 Then create two functions foo() and bar() . 然后创建两个函数foo()bar() foo() will have the original pipeline including the embedding layer. foo()将拥有包含嵌入层的原始管道。 bar() will have only the part of pipeline AFTER embedding layer. bar()将只包含管道AFTER嵌入层的一部分。 Instead, you will define new Input() layer in bar() with dimensions of your embeddings: 相反,您将在bar()具有嵌入尺寸的新Input()图层:

lstm1 = LSTM(256, return_sequences=True, name='lstm1')
lstm2 = LSTM(256, return_sequences=False, name='lstm2')
dense = Dense(NUM_OF_LABELS, name='Susie Dense')

def foo(...):
    sentence_indices = Input(input_shape, dtype="int32")
    embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)
    embeddings = embedding_layer(sentence_indices)
    X = lstm1(embeddings)
    X = Dropout(0.5)(X)
    X = lstm2(X)
    X = Dropout(0.5)(X)    
    X = dense(X)
    X = Activation("softmax")(X)
    return Model(inputs=sentence_indices, outputs=X)


def bar(...):
    embeddings = Input(embedding_shape, dtype="float32")
    X = lstm1(embeddings)
    X = Dropout(0.5)(X)
    X = lstm2(X)
    X = Dropout(0.5)(X)    
    X = dense(X)
    X = Activation("softmax")(X)
    return Model(inputs=sentence_indices, outputs=X)

foo_model = foo(...)
bar_model = bar(...)

foo_model.fit(...)
bar_model.save_weights(...)

Now, you will train the original foo() model. 现在,您将训练原始的foo()模型。 Then you can save the weights of the reduced bar() model. 然后,您可以保存缩小的bar()模型的权重。 When loading the model, don't forget to specify by_name=True parameter: 加载模型时,不要忘记指定by_name=True参数:

foo_model.load_weights('bar_model.h5', by_name=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM