keras:使用get_weights函数提取权重。

时间:2021-01-22 18:02:13

I would like to extract weights of 1d CNN layer, and understand how exactly the prediction values are computed. I am not able to re-produce the prediction values using the weights from get_weights() function.

我想要提取1d CNN层的权重,并了解如何精确计算预测值。我不能使用get_weights()函数的权重来重新生成预测值。

In order to explain my understanding, here is a small data set.

为了解释我的理解,这里有一个小的数据集。

n_filter = 64
kernel_size = 10
len_timeseries = 123
n_feature = 3
X = np.random.random(sample_size*len_timeseries*n_feature).reshape(sample_size,len_timeseries,n_feature)
y = np.random.random(sample_size*(len_timeseries-kernel_size+1)*n_filter).reshape(sample_size,
                                                                                  (len_timeseries-kernel_size+1),
                                                                                  n_filter)

Now, create a simple 1d CNN model as:

现在,创建一个简单的1d CNN模型为:

model = Sequential()
model.add(Conv1D(n_filter,kernel_size,
                 input_shape=(len_timeseries,n_feature)))
model.compile(loss="mse",optimizer="adam")

Fit the model and predict the values of X as:

拟合模型并预测X的值为:

model.fit(X,y,nb_epoch=1)
y_pred = model.predict(X)

The dimension of y_pred is (1000, 114, 64) as it should.

y_pred的维度是(1000,114,64)。

Now, I want to reproduce the value of y_pred[irow,0,ilayer]] using weights stored in model.layer. As there is only single layer, len(model.layer)=1. So I extract the weights from the first and the only layer as:

现在,我要使用存储在模型.layer中的权重来复制y_pred[irow,0,ilayer]的值。由于只有一个层,len(model.layer)=1。所以我从第一层和唯一层中提取权重

weight = model.layers[0].get_weights()
print(len(weight))
> 2 
weight0 = np.array(weight[0])
print(weight0.shape)
> (10, 1, 3, 64)
weight1 = np.array(weight[1])
print(weight1.shape)
> (64,)

The weight has length 2 and I assume that the 0th position contain the weights for features and the 1st position contain the bias. As the weight0.shape=(kernel_size,1,n_feature,n_filter), I thought that I can obtain the values of y_pred[irow,0,ilayer] by:

权重为2,我假设第0个位置包含特征的权重,第一个位置包含偏差。当权重的形状=(kernel_size,1,n_feature,n_filter)时,我认为可以通过以下方法获得y_pred[irow,0,ilayer]的值:

ifilter = 0
irow = 0
y_pred_by_hand = weight1[ifilter] + np.sum( weight0[:,0,:,ifilter] * X[irow,:kernel_size,:])
y_pred_by_hand
> 0.5124888777

However, this value is quite different from y_pred[irow,0,ifilter] as:

然而,这个值与y_pred[irow,0,ifilter]是非常不同的:

 y_pred[irow,0,ifilter]
 >0.408206

Please let me know where I got wrong.

请告诉我哪里出错了。

1 个解决方案

#1


0  

You have misunderstood the weights attribute here. What you are looking for is the output attribute of the layer which is the result given by model.predict. This can be obtained by layer.output. Typically a Layer is fed with an input tensor and is acted upon by the weights matrix which depends on the type of layer being used. This computation gives on output tensor which is what you are looking for.

你误解了这里的权重。您要寻找的是这个层的输出属性,这是模型所给出的结果。这可以通过layer.output获得。通常情况下,一个层被输入一个输入张量,并由权重矩阵作用,它依赖于所使用的层的类型。这个计算给出了输出张量,这就是你要找的。

For example consider a simple Dense layer with input tensor A of shape (1,3), an output sigmoid layer emitting a tensor B (1,1) and a weight matrix W is initialized by various techniques. The shape of W is determined based on the input and output shapes. So in this case a Dense layer does A matmul W and the result of this is going to be the prediction B. W's shape thus will determined as (3,1) only which can result in an output shape of (1,1). So what you are looking for is B and you are trying to do is access W.

例如,考虑一个具有输入张量a(1,3)的简单稠密层,一个输出sigmoid层发射一个张量B(1,1)和一个权重矩阵W被各种技术初始化。W的形状是根据输入和输出形状决定的。所以在这种情况下,一个稠密的层会产生一个matmul W,结果就是这个预测b W的形状,因此会被确定为(3,1)只会产生一个(1,1)的输出形状。所以你要找的是B你要做的是访问W。

#1


0  

You have misunderstood the weights attribute here. What you are looking for is the output attribute of the layer which is the result given by model.predict. This can be obtained by layer.output. Typically a Layer is fed with an input tensor and is acted upon by the weights matrix which depends on the type of layer being used. This computation gives on output tensor which is what you are looking for.

你误解了这里的权重。您要寻找的是这个层的输出属性,这是模型所给出的结果。这可以通过layer.output获得。通常情况下,一个层被输入一个输入张量,并由权重矩阵作用,它依赖于所使用的层的类型。这个计算给出了输出张量,这就是你要找的。

For example consider a simple Dense layer with input tensor A of shape (1,3), an output sigmoid layer emitting a tensor B (1,1) and a weight matrix W is initialized by various techniques. The shape of W is determined based on the input and output shapes. So in this case a Dense layer does A matmul W and the result of this is going to be the prediction B. W's shape thus will determined as (3,1) only which can result in an output shape of (1,1). So what you are looking for is B and you are trying to do is access W.

例如,考虑一个具有输入张量a(1,3)的简单稠密层,一个输出sigmoid层发射一个张量B(1,1)和一个权重矩阵W被各种技术初始化。W的形状是根据输入和输出形状决定的。所以在这种情况下,一个稠密的层会产生一个matmul W,结果就是这个预测b W的形状,因此会被确定为(3,1)只会产生一个(1,1)的输出形状。所以你要找的是B你要做的是访问W。