Reputation: 1325
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>
Upvotes: 0
Views: 1387
Reputation: 86630
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.
Upvotes: 2