Search code examples
pythontensorflowgoogle-colaboratorytensorflow2.0sequential

TensorFlow Same Model returning different results in Colab and local


I'm running the same code on Colab and on local (python3 in terminal) and getting very different results.

import pandas as pd
import tensorflow as tf
model = tf.keras.models.load_model('./my_saved_model')
inputs = pd.read_csv('./inputs.csv', index_col=0)
print(model.predict(inputs))
  • './inputs.csv' is a single-row pandas dataframe containing input data
  • './my_saved_model' is a dense sequential model

For Colab, I copied these files into the local notebook directory. tf.__version__ is 2.5.0 on local terminal and 2.7.0 in Colab.

On Colab, the print output is array([[0.00000000e+00]], dtype=float32), which seems incorrect, and on local terminal, the print output is array([[0.447962]], dtype=float32), which seems correct. I've tried other input data rows, and each time Colab incorrectly returns either 0 or 1 exactly while local terminal returns correctly a value between 0 and 1.

I can't figure out why this is happening, other than the possibility that the tensorflow version is not backward-compatible.

Here's the model summary, in case it helps:

Layer (type)                 Output Shape              Param #
- - -  
normalization_3 (Normalizati (None, 101)               203       
dense_2 (Dense)              (None, 1)                 102  
- - -     
Total params: 305
Trainable params: 102
Non-trainable params: 203

Solution

  • Confirmed that the tensorflow version is what is causing the error. I added pip install tensorflow==2.5.0 to the colab (even though it's not advised), and everything worked as properly.

    Very strange that tensorflow 2.x doesn't support backward compatibility.