why keras model param values change when it is accessed in a tensorflow session?

Multi tool use
why keras model param values change when it is accessed in a tensorflow session?
I was having trouble with my transfer learning implementation. I guess I found the root cause but it is not clear to me why it works like that. Here is the explanation...
If I create a model (e.g. resnet50 from keras.applications), and then try to use it in a tensorflow session, weights all of a sudden change.
Here is a simple example:
First import the necessary libraries:
import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.models import Model
Then define the model as following:
model = ResNet50(weights='imagenet')
Now print out parameters from one of the layers as following:
model.get_layer('conv1').get_weights()
The output is long but it starts as following:
[array([[[[ 2.82526277e-02, -1.18737184e-02, 1.51488732e-03, ...,
-1.07003953e-02, -5.27982824e-02, -1.36667420e-03],
[ 5.86827798e-03, 5.04415408e-02, 3.46324709e-03, ...,
1.01423981e-02, 1.39493728e-02, 1.67549420e-02],
[-2.44090753e-03, -4.86173332e-02, 2.69966386e-03, ...,
-3.44439060e-04, 3.48098315e-02, 6.28910400e-03]],
Later in the program, I need to read some data from csv files. I try to read the data by using the dataset API from tensorflow. For this purpose, I create a tensorflow session. And if I want to use the model within the tensorflow session, I see that model parameters change.
Here is a code example:
init_global_var = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_global_var)
print(model.get_layer('conv1').get_weights())
And the output starts as following:
[array([[[[ 3.95432524e-02, -2.38095019e-02, -1.64129660e-02, ...,
-2.83494107e-02, 2.25975104e-02, -1.48569904e-02],
[ 2.40861587e-02, 1.48933977e-02, -4.10864130e-02, ...,
-3.18703875e-02, -9.43836942e-03, 1.18204653e-02],
[ 2.99405716e-02, 1.69009715e-03, -1.43084712e-02, ...,
-2.93575712e-02, 2.70796008e-02, -3.17203328e-02]],
Since I have not trained the model yet, I expect to see the same parameter values but they are not the same!
So the question is: Do I have to create my model within the tensorflow session? Why is my resnet50 model accessible but with different parameter values in the tensorflow session?
save_weights
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
All weights are saved in
save_weights
. It would be better if you can provide the error message and example code to reproduce the error.– Yu-Yang
Jul 5 at 5:22