[英]How to make a single vector/array?
Consider the following cnn model考虑以下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.我创建了上述 model 的
model_1=create_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
.从
model_1
,上面的代码获取卷积层/fc 层的权重,并将它们组合在一个combine_weight
数组中。 The dtype of combine_weight
is attained through print(type(combine_weights))
giving the type <class 'list'>
combine_weight
的 dtype 是通过print(type(combine_weights))
获得类型<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现在,我尝试使用以下
combine_weights_reshape=tf.reshape(tf.stack(combine_weights,[-1]))
重塑所有这些权重以产生单行向量/一维数组,这会产生以下错误
<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?如何将
combine_weight
重塑为单行向量/数组?
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)
One solution is to flatten the weight tensor before appending it to the list of weights.一种解决方案是在将权重张量附加到权重列表之前对其进行展平。 The original problem was that the weight tensors had different shapes, so
tf.stack
would not work.最初的问题是权重张量具有不同的形状,因此
tf.stack
不起作用。
combine_weights = []
for layer in model.layers:
if "conv" in layer.name or "fc" in layer.name:
print(layer)
# Flatten the weights tensor.
we = tf.reshape(layer.weights, shape=-1)
combine_weights.append(we)
# Concatenate all of the (flat) weight vectors.
combine_weights = tf.concat(combine_weights, axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.