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

edn

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?

edn

Seemingly, this question is a replicate of another question in Tensorflow. Check this question out instead for the answer:

Integrating Keras model into TensorFlow

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Where is the tensorflow session in Keras

From Dev

Where is the tensorflow session in Keras

From Dev

Integrating Keras model into TensorFlow

From Dev

Different predictions when running Keras model in TensorFlow Lite

From Dev

Why is my JSF session always recreated? (when accessed with double slash in the URL)

From Dev

session becomes empty when accessed in another function

From Dev

Loading Tensorflow model in different session

From Dev

Using PARAM values with Session State Server (SQL Server Model) for IIS App

From Dev

tensorflow/keras training model keyerror

From Dev

Selectively Optimizing a Keras Model with Tensorflow

From Dev

Why and when does this char * change values?

From Dev

Why and when does this char * change values?

From Dev

MVC Model property not updated when accessed in javascript

From Dev

Why don't change $_SESSION variables (PHP) when use AJAX?

From Dev

why wont ng-keypress change ng-model values

From Dev

graph session empty error in Keras with Tensorflow backend

From Dev

sails.js Use session param in model

From Dev

sails.js Use session param in model

From Dev

Populate TextBoxFor values when DropDownList on change from model

From Dev

"WARNING:tensorflow:Your input ran out of data" Error appearing when training Keras Model

From Java

How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API?

From Dev

How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API?

From Dev

Why Session is not accessible in Rails model

From Java

Multilayer Perceptron (MLP) Keras tensorflow model

From Dev

Opening Keras model with embedding layer in Tensorflow in Golang

From Dev

Learning Keras model by using Distributed Tensorflow

From Dev

Tensorflow——keras model.save() raise NotImplementedError

From Dev

Java Tensorflow + Keras Equivalent of model.predict()

From Dev

My selectbox becomes disabled when accessed through session

Related Related

  1. 1

    Where is the tensorflow session in Keras

  2. 2

    Where is the tensorflow session in Keras

  3. 3

    Integrating Keras model into TensorFlow

  4. 4

    Different predictions when running Keras model in TensorFlow Lite

  5. 5

    Why is my JSF session always recreated? (when accessed with double slash in the URL)

  6. 6

    session becomes empty when accessed in another function

  7. 7

    Loading Tensorflow model in different session

  8. 8

    Using PARAM values with Session State Server (SQL Server Model) for IIS App

  9. 9

    tensorflow/keras training model keyerror

  10. 10

    Selectively Optimizing a Keras Model with Tensorflow

  11. 11

    Why and when does this char * change values?

  12. 12

    Why and when does this char * change values?

  13. 13

    MVC Model property not updated when accessed in javascript

  14. 14

    Why don't change $_SESSION variables (PHP) when use AJAX?

  15. 15

    why wont ng-keypress change ng-model values

  16. 16

    graph session empty error in Keras with Tensorflow backend

  17. 17

    sails.js Use session param in model

  18. 18

    sails.js Use session param in model

  19. 19

    Populate TextBoxFor values when DropDownList on change from model

  20. 20

    "WARNING:tensorflow:Your input ran out of data" Error appearing when training Keras Model

  21. 21

    How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API?

  22. 22

    How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API?

  23. 23

    Why Session is not accessible in Rails model

  24. 24

    Multilayer Perceptron (MLP) Keras tensorflow model

  25. 25

    Opening Keras model with embedding layer in Tensorflow in Golang

  26. 26

    Learning Keras model by using Distributed Tensorflow

  27. 27

    Tensorflow——keras model.save() raise NotImplementedError

  28. 28

    Java Tensorflow + Keras Equivalent of model.predict()

  29. 29

    My selectbox becomes disabled when accessed through session

HotTag

Archive