Network outputs from tf.session.run greatly differ from the ones obtained with keras.Model.predict

Viewed 885

I'm trying to use Keras model through Tensorflow session. But results form model.predict and sess.run different. Is there any way to work with Kers model through Tensorflow session?

Tensorflow version: 1.4.0
Keras version: 2.1.1

from sklearn.datasets.samples_generator import make_circles
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np
import tensorflow as tf
from keras import backend as K

sess = tf.Session()
K.tensorflow_backend.set_session(sess)

X, y = make_circles(n_samples=1000,
                    noise=0.1,
                    factor=0.2,
                    random_state=0)

model = Sequential()
model.add(Dense(4, input_shape=(2,), activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(SGD(lr=0.5), 'binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=20)

print("Keras model. First prediction: " + str(model.predict(np.c_[0, 0])))
print("Keras model. Second prediction: " + str(model.predict(np.c_[1.5, 1.5])))

with sess.as_default():

    y_tensor = model.outputs[0]
    x_tensor = model.inputs[0]
    sess.run(tf.global_variables_initializer())

    print("TF model. First prediction: " + str(sess.run(y_tensor, feed_dict={x_tensor: np.c_[0, 0]} )))
    print("TF model. Second prediction: " + str(sess.run(y_tensor, feed_dict={x_tensor: np.c_[1.5, 1.5]} )))
1 Answers

Okay, it's K.set_session(s) and not K.tensorflow_backend.set_session(s).

Second: sess.run(tf.global_variables_initializer()) resets all variables using their respective initializer, including the network weights (they use xavier initializer by default).

So you are:

  1. Training the keras model
  2. Printing the prediction for the keras model
  3. Reseting to random weights
  4. Printing the predictions for the same model

Commenting sess.run(tf.global_variables_initializer()) fixes the problem:

Keras model. First prediction: [[ 0.99195099]]
Keras model. Second prediction: [[ 0.03110269]]
TF model. First prediction: [[ 0.99195099]]
TF model. Second prediction: [[ 0.03110269]]
Related