0

I am training an ImageNet-pretrained VGG-16 model in Keras with two different hyperparameter settings. I prefer to see if there exists a linear relationship between the two sets of model weights. For simplicity, I have taken two VGG-16 models just to check if my approach works. I have computed the layer weights for each model and stored them as a 1-dimensional array, each having a shape of (1, 512). I wish to measure the Pearson Correlation coefficient between these two model weights as used in this study https://arxiv.org/pdf/1910.08475v2.pdf. My code is as shown below:

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)

I am getting the following error:

 File "C:\Users\AppData\Local\Temp\2/ipykernel_16136/1139308701.py", line 1, in <module>
    corr, _ = pearsonr(layer_weights1, layer_weights2)

  File "C:\Users\AppData\Local\Continuum\anaconda3\envs\tf2.7\lib\site-packages\scipy\stats\stats.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.

Is my approach correct?

1 Answer 1

0

I found a solution to my problem. It is the shape mismatch error due to the syntax for the computation of the Pearson correlation coefficient. The revised code is as shown below:

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.