You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Keras中‘np.object将被定义为对应NumPy标量’的报错

问题解决:NumPy与TensorFlow/Keras版本不兼容导致np.object报错

问题重现

运行以下代码时触发报错:

from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(64, activation="relu"),
    layers.Dense(10, activation="softmax")
])

报错核心信息:

AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. 
The aliases was originally deprecated in NumPy 1.20;

问题原因

你的TensorFlow(或其内置的Keras)版本过旧,不兼容较新版本的NumPy。NumPy 1.20+移除了np.object这个别名,但旧版TensorFlow的代码里仍在使用该写法,导致导入时出错。

解决方案

有两种可行的修复方式:

方式一:降级NumPy到兼容版本

安装与当前TensorFlow版本匹配的旧版NumPy(推荐1.19.x系列),执行命令:

pip install numpy==1.19.5

方式二:升级TensorFlow到兼容新版本NumPy的版本

旧版TensorFlow在后续更新中修复了该兼容问题,直接升级TensorFlow即可:

pip install --upgrade tensorflow

升级后无需单独安装Keras,因为TensorFlow已内置兼容的Keras版本。

验证修复

执行以下命令确认环境正常:

python3 -c 'import tensorflow as tf; print(tf.__version__); print(tf.keras.__version__)'

若能成功输出版本号,说明环境修复完成,再运行原notebook代码即可正常执行。

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:33:32