Vishal singh rajpoot
Vishal singh rajpoot

Reputation: 123

Layer weight shape (1, 1) not compatible with provided weight shape (1,) for keras model

I trained a model using Keras but forgot to save the model. This model was the part of a project where many other models were developed but now I can not proceed with the project. Fortunately I saved initial and final training weights. Now, I am trying to create a model with same final weights to get the predictions. I am compiling the keras model and using function model.set_weights to set the final training weights of lost model to new model. Here is the code.

model = Sequential()
model.add(Dense(1,input_dim = 1, activation = 'relu'))
model.add(Dense(1, activation = 'relu'))
model.compile(loss = 'mean_squared_error', optimizer = 'Adam', metrics = ['mse'])
listOfNumpyArrays = [np.array([0.2]),np.array([0.2])]
listOfNumpyArrays1 = listOfNumpyArrays
model.layers[0].set_weights(listOfNumpyArrays)
model.layers[1].set_weights(listOfNumpyArrays1)

Traceback

ValueError                                Traceback (most recent call last)
<ipython-input-31-e63437554e30> in <module>()
----> 1 model.layers[0].set_weights(listOfNumpyArrays)
      2 model.layers[1].set_weights(listOfNumpyArrays1)
1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in set_weights(self, weights)
   1124                                  str(pv.shape) +
   1125                                  ' not compatible with '
-> 1126                                  'provided weight shape ' + str(w.shape))
   1127             weight_value_tuples.append((p, w))
   1128         K.batch_set_value(weight_value_tuples)
ValueError: Layer weight shape (1, 1) not compatible with provided weight shape (1,)

Upvotes: 1

Views: 4076

Answers (1)

Swapnil Pande
Swapnil Pande

Reputation: 196

The numpy array you created using np.array([0.2]) has a shape (1,) whereas your weight array has a shape (1,1). While they store the same amount data, numpy regards these as different shapes. You can fix this by doing the following:

Instead of:

listOfNumpyArrays = [np.array([0.2]),np.array([0.2])]

Use:

listOfNumpyArrays = [np.empty(shape = (1,1), dtype = np.float32), np.empty(shape = (1,1), dtype = np.float32)]
listOfNumpyArrays[0][0] = 0.2
listOfNumpyArrays[1][0] = 0.2

UNRELATED NOTE:

In this line:

listOfNumpyArrays1 = listOfNumpyArrays

It seems like you want to create two different lists of numpy arrays that are initialized to the same value. listOfNumpyArrays1 will actually refer to the same list as listOfNumpyArrays, however. Therefore, when you do set_weights on listOfNumpyArrays1, it will modify listOfNumpyArrays as well. To initialize them to the same value while creating two different lists, you can use the following code:

listOfNumpyArrays1 = [np.copy(listOfNumpyArrays[0]), np.copy(listOfNumpyArrays[1])]

np.copy creates a new array that is a copy of the one you pass. This is can be written in a more pythonic way using list comprehension as follows:

listOfNumpyArrays1 = [np.copy(x) for x in listOfNumpyArrays]

Upvotes: 2

Related Questions