tf.keras.Model.predict and call return different result

tf.keras.Model.predict and call return different result

import tensorflow as tf
import numpy as np
tf.set
ipts = tf.keras.Input([2])
x = tf.keras.layers.Dense(10)(ipts)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(3)(x)
model = tf.keras.Model(ipts, x)
model.summary()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
y_train = model(tf.ones((2,2)),training=True)
y_test = model(tf.ones((2,2)),training=False)
sess.run(y_train)
sess.run(y_test)
model.predict(np.array([[1.,1.],[1.,1.]]))

sess.run(y_test) should be the same with model.predict(np.array([[1.,1.],[1.,1.]])) , but the fact is that they are different. Why

1 answer

  • answered 2019-04-15 06:56 Anna Krogager

    You need to register the session as your Keras session with K.set_session(sess). Then sess.run(y_test) gives the same result as model.predict(np.array([[1.,1.],[1.,1.]])).