我正在用两个不同的超参数设置在Keras中训练一个imagenet预训练的VGG-16模型。我更愿意看看两组模型权重之间是否存在线性关系。为了简单起见,我使用了两个VGG-16模型来检查我的方法是否有效。我已经计算了每个模型的层权重,并将它们存储为一个一维数组,每个数组的形状为(1,512)。我希望测量本研究中使用的这两个模型权重之间的Pearson相关系数https://arxiv.org/pdf/1910.08475v2.pdf。我的代码如下:
import os
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import *
from tensorflow.keras import applications
from tensorflow.keras.models import Model
from scipy.stats import pearsonr
from numpy import array
model_input = (224,224,3)
model1 = applications.VGG16(include_top=False,
weights='imagenet',
input_shape=model_input)
model1.summary()
n_layers1 = len(model1.get_weights())
for layer1 in range(n_layers1):
layer_weights1 = array([model1.get_weights()[layer1]]) # shape [1 x 512]
model2 = applications.VGG16(include_top=False,
weights='imagenet',
input_shape=model_input)
model2.summary()
n_layers2 = len(model2.get_weights())
for layer2 in range(n_layers2):
layer_weights2 = array([model2.get_weights()[layer2]]) # shape [1 x 512]
# calculate Pearson's correlation
corr, _ = pearsonr(layer_weights1, layer_weights2)
print('Pearsons correlation: %.3f' % corr)
我得到以下错误:
File "C:UsersAppDataLocalTemp2/ipykernel_16136/1139308701.py", line 1, in <module>
corr, _ = pearsonr(layer_weights1, layer_weights2)
File "C:UsersAppDataLocalContinuumanaconda3envstf2.7libsite-packagesscipystatsstats.py", line 4016, in pearsonr
raise ValueError('x and y must have length at least 2.')
ValueError: x and y must have length at least 2.
我的方法正确吗?
我找到了解决问题的方法。这是由于计算Pearson相关系数的语法导致的形状不匹配误差。修改后的代码如下:
import os
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import *
from tensorflow.keras import applications
from tensorflow.keras.models import Model
from scipy.stats import pearsonr
from numpy import array
import numpy as np
model_input = (224,224,3)
model1 = applications.VGG16(include_top=False,
weights='imagenet',
input_shape=model_input)
model1.summary()
n_layers1 = len(model1.get_weights())
for layer1 in range(n_layers1):
layer_weights1 = array([model1.get_weights()[layer1]])
model2 = applications.VGG16(include_top=False,
weights='imagenet',
input_shape=model_input)
model2.summary()
n_layers2 = len(model2.get_weights())
for layer2 in range(n_layers2):
layer_weights2 = array([model2.get_weights()[layer2]])
# calculate Pearson's correlation
x1 = layer_weights1.transpose()
x2 = layer_weights1.transpose()
x11 = x1.reshape(-1)
x22 = x2.reshape(-1)
corr, sig = pearsonr(x11, x22)
print('Pearsons correlation: %.4f' % corr)