如何在Keras中重新初始化现有模型的层权? [英] How to re-initialize layer weights of an existing model in Keras?

查看:1465
本文介绍了如何在Keras中重新初始化现有模型的层权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际的问题是为Keras中的现有(已构建)模型生成随机层权重.有一些使用Numpy [2]的解决方案,但是选择该解决方案并不是很好.因为在Keras中,有一些特殊的初始化程序会为每种图层类型使用不同的分布.当使用Numpy代替初始值设定项时,生成的权重与其原始值的分布不同.让我们举个例子:

The actual problem is generating random layer weights for an existing (already built) model in Keras. There are some solutions using Numpy [2] but it is not good to choice that solutions. Because, in Keras, there are special initializers using different distributions for each layer type. When Numpy is used instead of the initializers, the generated weights have different distribution then its original. Let's give an example:

模型的第二层是卷积(1D)层,其初始值设定项是GlorotUniform [1].如果使用Numpy生成随机权重,则生成的权重的分布将不是GlorotUniform.

Second layer of my model is a convolutional (1D) layer and its initializer is GlorotUniform [1]. If you generate random weights using Numpy, the distribution of generated weights will not be the GlorotUniform.

我有解决此问题的方法,但有一些问题.这是我所拥有的:

I have a solution for this problem but it has some problems. Here is what I have:

def set_random_weights(self, tokenizer, config):
    temp_model = build_model(tokenizer, config)
    self.model.set_weights(temp_model.get_weights())

我正在构建现有模型.构建过程完成后,将重新初始化模型的权重.然后,我得到重新初始化的权重并将其设置为另一个模型.用于生成新权重的构建模型具有多余的过程.因此,我需要一个新的解决方案,而无需构建模型和Numpy.

I am building the existing model. After the building process, weights of the model are re-initialized. Then I get the re-initalized weights and set them to another model. Building model to generate new weights has redundant processes. So, I need a new solution without building a model and Numpy.

  1. https://keras.io/initializers/
  2. https://www.codementor .io/nitinsurya/如何重新初始化keras-model-weights-et41zre2g
  1. https://keras.io/initializers/
  2. https://www.codementor.io/nitinsurya/how-to-re-initialize-keras-model-weights-et41zre2g

推荐答案

请参阅此问题的先前答案这里. 具体来说,如果您想使用Keras图层的原始权重初始化程序,则可以执行以下操作:

See previous answers to this question here. Specifically, if you want to use the original weights initializer of a Keras layer, you can do the following:

import tensorflow as tf
import keras.backend as K

def init_layer(layer):
    session = K.get_session()
    weights_initializer = tf.variables_initializer(layer.weights)
    session.run(weights_initializer)


layer = model.get_layer('conv2d_1')
init_layer(layer)

这篇关于如何在Keras中重新初始化现有模型的层权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆