【问题标题】:TensorFlow Same Model returning different results in Colab and localTensorFlow Same Model 在 Colab 和本地返回不同的结果
【发布时间】:2022-01-09 21:10:47
【问题描述】:

我在 Colab 和本地(终端中的 python3)上运行相同的代码并得到非常不同的结果。

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' 是一个包含输入数据的单行 pandas 数据框
  • './my_saved_model' 是密集序列模型

对于 Colab,我将这些文件复制到本地笔记本目录中。 tf.__version__ 在本地终端为 2.5.0,在 Colab 为 2.7.0。

在 Colab 上,打印输出为 array([[0.00000000e+00]], dtype=float32),这似乎不正确,而在本地终端上,打印输出为 array([[0.447962]], dtype=float32),这似乎是正确的。我尝试了其他输入数据行,每次 Colab 错误地返回 0 或 1,而本地终端正确返回 0 到 1 之间的值。

我不知道为什么会这样,除了 tensorflow 版本不向后兼容的可能性。

这是模型摘要,以防有帮助:

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

【问题讨论】:

    标签: python tensorflow google-colaboratory tensorflow2.0 sequential


    【解决方案1】:

    确认 tensorflow 版本是导致错误的原因。我在 colab 中添加了 pip install tensorflow==2.5.0(即使它是 not advised),一切正常。

    很奇怪,tensorflow 2.x 不支持向后兼容。

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 2019-07-25
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2018-01-31
      • 2014-01-24
      相关资源
      最近更新 更多