使用Keras使用get_variable创建自定义初始化程序时出错

时间:2017-09-25 14:18:02

标签: tensorflow keras keras-layer keras-2

我用Keras创建了一个自定义初始化程序。部分代码是:

def my_init(shape):
    P = tf.get_variable("P", shape=shape,    initializer = tf.contrib.layers.xavier_initializer())
    return P

model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),strides=(1, 1), padding='same', input_shape = input_shape, kernel_initializer = my_init))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(1, 1) , strides=(1, 1) , padding='same' , kernel_initializer = my_init))

当在卷积层中第二次调用“my_init”初始值设定项时,它会抛出此错误:

Variable P already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

不允许重复使用变量P.有没有办法在每次调用中创建一个新变量?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Keras中提供的Xavier初始值设定项,名称为glorot_uniformglorot_normal

在此处查看:https://keras.io/initializers/

model.add(Conv2D(32, kernel_size=(1, 1) , strides=(1, 1) , 
          padding='same' , kernel_initializer =keras.initializers.glorot_uniform())