1

Is it possible to load weight in NN in keras in model.add? I want to load the weight based on Xavier or another initializers. How I can do this in keras?

For instance, weight=[w1,w2,w3,w4] how we could do this in keras?

For instance, in TF we have: initializer=tf.contrib.layers.xavier_initializer()

double-beep
  • 5,031
  • 17
  • 33
  • 41
fila
  • 25
  • 7

2 Answers2

0

Assuming xxx.h5 is your weights file, do:

weights_path = 'path/xxx.h5'

You may also load weights in keras like this:

model.load_weights(weights_path, by_name=True)

Where model is your keras model and its weights architecture match the weights you want to import

double-beep
  • 5,031
  • 17
  • 33
  • 41
Alan Yang
  • 66
  • 5
  • Thanks Alan but I did not understand how I can fill the *.h5 file with for example Xavier initialization! I do not determine the weight by hand to write in *.h5 file, I prefer to use some initializer like Xavier! – fila Apr 16 '18 at 01:15
0

It is possible to load the weights according to a particular initialization using the attribute kernel_initializer for layers

model.add(Dense(2,kernel_initializer='glorot_normal'))

The default value is glorot_uniform

For initializers where you need to provide arguments

from keras import initializers

model.add(Dense(64, kernel_initializer=initializers.random_normal(stddev=0.01))

As you mentioned Xavier,in Keras, Xavier uniform and Xavier normal are known as glorot uniform and glorot normal respectively.

Refer to Keras Initializers for more initializers

EDIT: If you want to set the weights using a list of numpy arrays, refer to the this answer

user239457
  • 1,766
  • 1
  • 16
  • 26
  • Thanks but what I can do if I want to set the weights based on a definite amount. For instance, weight=[w1,w2,w3,w4] how we could do this in keras? – fila Apr 26 '18 at 16:47