Tensorflow 示例在 colab 上运行良好,但在 jupyter lab 上 运行 时给出 nan

Tensorflow example works fine on colab, but gives nan while running on jupyter lab

使用 colab 上的 tensorflow 示例教程后:基本回归:预测燃油效率,可在此处找到:https://www.tensorflow.org/tutorials/keras/regression
我一直在尝试通过 anaconda 在 jupyter notebook 上 运行 这个例子,以便能够 运行 它离线。
人们可以在 link 上找到的代码在 google colab 上运行良好,但是当我尝试在 jupyter notebook 上 运行 它时,我得到了一个 nan 的向量。

  1. 运行 在 google colab 上的产量:
    Running the example on google colab
  2. 关于 jupyter 的 运行 产量:
    Running the example on jupyter lab

构建时序模型的代码:[问题出处]

horsepower_model = tf.keras.Sequential([
    horsepower_normalizer,
    layers.Dense(units=1)
])

horsepower_model.summary()

其余代码可以在我上面附上的link中找到:
https://www.tensorflow.org/tutorials/keras/regression

模型要做的事情:
该模型从数据集中获取 2 个数组:MPG 和 Horsepower,并且即将使用单变量线性回归以根据 Horsepower 预测 MPG。
因此,模型引入了一个形状为 1 的 Dense 层,从这里可以看出:

layers.Dense(units=1)

模型的输出应该是基于马力值的 MPG 值。
然后我们将能够将它与我们从数据集中获得的真实 MPG 的值进行比较。

提前致谢。

编辑:我上传我的笔记本:
https://www.udrop.com/Jk3/Horsepower.ipynb

我看到您在其中使用了不同的模型。 在 Colab 中,您使用的模型只有 5 个参数 而在 Notebook 中,您使用的是具有 944 参数的密集模型。

先在笔记本上试试运行参数少的模型 要么 在两者上尝试 运行 相同的模型。(也许复杂模型没有完全训练。)

编辑 1: 我创建了一个 jupyter notebook 并尝试了代码。 在我的系统中它运行良好,请在您的系统中尝试 运行 这个 jupyter notebook。 Jupyter Notebook .如果有效,那么您的代码中可能存在语法错误。

编辑 2: 更新张量流:

pip install --ignore-installed --upgrade tensorflow

使用此命令或适用于您的版本的任何其他命令。

在每个马力值周围加上一组额外的括号解决了我的问题:

horsepower = np.array([[h] for h in train_features['Horsepower']])