1

I created a custom initializer with Keras. Part of the code is:

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))

When "my_init" initializer is called for the second time in the convolution layer it throws this error:

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

It is not allowing to reuse the variable P. Is there any way to create a new variable in each call?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Darshi
  • 55
  • 4
  • You could try `K.variable(....)`. It doesn't require a name. --- `import keras.backend as K`. – Daniel Möller Sep 25 '17 at 14:28
  • I need to use "xavier" initializer available in tensorflow (tf.contrib.layers.xavier_initializer()) but k.variable is not allowing to call xavier initializer. – Darshi Sep 26 '17 at 14:52

1 Answers1

1

You could try using the Xavier initializers available in Keras, under the names glorot_uniform and glorot_normal.

See them here: https://keras.io/initializers/

model.add(Conv2D(32, kernel_size=(1, 1) , strides=(1, 1) , 
          padding='same' , kernel_initializer =keras.initializers.glorot_uniform())
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • how about initializing any variable to Xavier initializers, I mean not within Conv2D layer. for example: 'L1 = tf.keras.backend.variable( value= np.random.normal(0.0, 1.0, (feature_dim, feature_dim)), dtype= tf.float32) '. I want to initialize the same variable but using Xaivier initializer instead but couldn't find a proper way to do it. – W. Sam Feb 11 '19 at 21:24
  • Just call the initializer with the desired shape. `L1 = keras.initializers.glorot_uniform()(shape)` – Daniel Möller Feb 11 '19 at 22:07