0

I make a small keras model and get weights of model using following code:

from keras.models import Sequential
from keras.layers import Dense, Flatten,Conv2D, MaxPooling2D
input_shape = (28, 28, 1)
model = Sequential()
model.add(Conv2D(1, kernel_size=(2, 2),
                 activation='relu',
                 input_shape=input_shape,trainable=False))
model.add(MaxPooling2D(pool_size=(16,16)))
model.add(Flatten())
model.add(Dense(3, activation='softmax',trainable=False))
a=model.get_weights()

Now i want to initialize weights as same shape of a using keras initializers, i am using following code:

from keras.initializers import glorot_uniform
W1 = glorot_uniform((a,))

Is my approach is right? If it is wrong then please suggest me the solution and if it is right then why i am not able to see the weights, it's showing:

<keras.initializers.VarianceScaling at 0x7f65746ba128>
Hitesh
  • 1,285
  • 6
  • 20
  • 36

1 Answers1

2

About get_weights():

The method model.get_weights() will return a list of numpy arrays. So you have to take care to create a list with the same number of arrays, in the same order, with the same shapes.

In this model, it seems there will be 4 arrays in the list, convolution kernel and bias plus dense kernel and bias. Each one with a different shape.

About the initializers:

Initializers are functions that take a shape as input and return a tensor.

You see VarianceScaling because it's probably the name of the function. You should call the function with a shape to get the result:

weights = [glorot_uniform()(npArray.shape) for npArray in a]   

They will be keras tensors, though, not numpy arrays. You should K.eval(arr) them to get them as numpy arrays.

If using model.set_weights(), pass the list with numpy arrays (same number that is present in get_weights(), same shapes)

Standard usage of initializers:

But actually, initializers are meant to be used directly in the creation of the layers, and if you don't want to specify seeds and other initializer parameters, you can use just strings:

from keras.models import Sequential
from keras.layers import Dense, Flatten,Conv2D, MaxPooling2D
input_shape = (28, 28, 1)
model = Sequential()
model.add(Conv2D(1, kernel_size=(2, 2),
                 activation='relu',
                 input_shape=input_shape,
                 trainable=False,
                 kernel_initializer='glorot_uniform', #example with string
                 bias_initializer='zeros'))
model.add(MaxPooling2D(pool_size=(16,16)))
model.add(Flatten())
model.add(Dense(3, 
                activation='softmax',
                trainable=False, 
                kernel_initializer=glorot_uniform(seed=None), #example creating a function
                bias_initializer='zeros'))

Read more about initializers here.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • `weights = [glorot_uniform()(npArray.shape) for npArray in a] ` this line help me alot @Daniel. Thnx – Hitesh Nov 01 '17 at 08:08
  • what is difference between nparray and npArray – Hitesh Nov 02 '17 at 05:40
  • 1
    `npArray` is just a var name. I used this name just to remember that this var is a numpy array. – Daniel Möller Nov 02 '17 at 13:28
  • from ` weights = [glorot_uniform()(npArray.shape) for npArray in a]` i want to replace glorot_uniform to numpy array like following code: `unif = glorot_uniform() X = K.eval(unif((1,11))) Z=[X()(npArray.shape) for npArray in a]` but getting error `TypeError: 'numpy.ndarray' object is not callable` – Hitesh Nov 02 '17 at 13:32
  • `weights = [K.eval(glorot_uniform()(npArray.shape)) for npArray in a]` – Daniel Möller Nov 02 '17 at 22:08
  • Suppose i got a array X from another program which have a size of (1,11). How to convert this X into shape of a. Both X and a have same no. of elements. and previously i know the shape of a (as mention in above question and comments). – Hitesh Nov 03 '17 at 05:21
  • when i tried `weights = [(X(npArray.shape)) for npArray in a]` then it gives error `TypeError: 'numpy.ndarray' object is not callable` – Hitesh Nov 03 '17 at 05:23
  • Waiting for response – Hitesh Nov 06 '17 at 07:38
  • 1
    To reshape a numpy array, you do `X.reshape(someShape)` -- In that list, you can try: `weights = [X.reshape(W.shape) for X,W in zip(arraysFromOtherProgram, a)]` – Daniel Möller Nov 06 '17 at 11:08
  • X is the array i got from another program. what is meant by W here? – Hitesh Nov 07 '17 at 05:23
  • I this kind of **list comprehension**, `X` and `W` (vars in the `for` part) are samples taken from the lists/arrays coming from the `in` part. `zip` puts the two lists together, `arraysFromOtherProgram` and `a`. The loop is taking `X` from `arraysFromOtherProgram` and `W` from `a` (which is your weight list you got from the model). – Daniel Möller Nov 07 '17 at 11:07