Consider the following cnn model
def create_model():
x_1=tf.Variable(24)
bias_initializer = tf.keras.initializers.HeNormal()
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(28,28,1),activation="relu", name='conv2d_1', use_bias=True,bias_initializer=bias_initializer))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (5, 5), activation="relu",name='conv2d_2', use_bias=True,bias_initializer=bias_initializer))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(320, name='dense_1',activation="relu", use_bias=True,bias_initializer=bias_initializer),)
model.add(Dense(10, name='dense_2', activation="softmax", use_bias=True,bias_initializer=bias_initializer),)
return model
I create a model_1=create_model()
instance of the above model. Now consider the following
combine_weights=[]
for layer in model.layers:
if 'conv' in layer.name or 'fc' in layer.name:
print(layer)
we=layer.weights
combine_weights.append(we)
From model_1
, the above code takes the weights of convolutional layers/fc layers and combine them in a single array of combine_weight
.
The dtype of combine_weight
is attained through print(type(combine_weights))
giving the type <class 'list'>
Now, I try to reshape all these weights to result in a single row vector/1-d array by using the following
combine_weights_reshape=tf.reshape(tf.stack(combine_weights,[-1]))
which gives the following error
<ipython-input-80-dee21fe38c89> in <module>
----> 1 combine_weights_reshape=tf.reshape(tf.stack(combine_weights,[-1]))
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
7184 def raise_from_not_ok_status(e, name):
7185 e.message = (" name: " name if name is not None else "")
-> 7186 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7187
7188
InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [5,5,1,32] != values[1].shape = [32] [Op:Pack] name: stack
How can I reshape the combine_weight
into a single row vector/array?
CodePudding user response:
I got the desired result with the following
combine_weights=[]
con=[]
for layer in model.layers:
if 'conv' in layer.name or 'fc' in layer.name:
print(layer.name)
we=layer.weights[0]
we_reshape=tf.reshape(we,[-1])
# bi=layer.weights[1]
combine_weights.append(we_reshape)
print(combine_weights)
print(len(combine_weights))
con=tf.concat([con,we_reshape], axis=[0])
print(con)