[英]How do you unroll a Numpy array of (mxn) dimentions into a single vector
[英]How to make a single vector/array?
考虑以下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
我创建了上述 model 的model_1=create_model()
实例。 现在考虑以下
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)
从model_1
,上面的代码获取卷积层/fc 层的权重,并将它们组合在一个combine_weight
数组中。 combine_weight
的 dtype 是通过print(type(combine_weights))
获得类型<class 'list'>
现在,我尝试使用以下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
如何将combine_weight
重塑为单行向量/数组?
我通过以下方式得到了预期的结果
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)
一种解决方案是在将权重张量附加到权重列表之前对其进行展平。 最初的问题是权重张量具有不同的形状,因此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.