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

导入from tensorflow.keras.utils import to_categorical时内核崩溃求助

解决方案:TensorFlow/Keras导入导致Jupyter内核崩溃(M2 Mac)

1. 修复版本兼容性冲突

你的TensorFlow 2.12.0与独立安装的Keras 2.10.0版本不匹配——TensorFlow 2.12.x自带的Keras版本应为2.12.x,独立Keras会和TF内置的Keras产生冲突,引发内核崩溃:

  • 卸载独立Keras:pip uninstall keras -y
  • 重新安装对应版本的TensorFlow(强制覆盖,确保自带匹配的Keras):pip install tensorflow==2.12.0 --force-reinstall

2. 适配Apple Silicon的TensorFlow安装

M2芯片需要针对Apple Silicon优化的TensorFlow版本,普通pip包可能存在底层适配问题:

  • 先卸载现有TensorFlow:pip uninstall tensorflow -y
  • 通过conda+pip安装适配版本:
    conda install -c apple tensorflow-deps
    pip install tensorflow-macos==2.12.0
    pip install tensorflow-metal==0.8.0
    
    (tensorflow-metal是Apple官方的GPU加速插件,能解决部分Apple Silicon上的内核崩溃问题)

3. 确保Jupyter内核与环境一致

可能Jupyter使用的内核并非你安装TensorFlow的Anaconda环境:

  • 在目标Anaconda环境中安装ipykernel:pip install ipykernel
  • 将当前环境注册为Jupyter内核:python -m ipykernel install --user --name=你的环境名 --display-name="Python (你的环境名)"
  • 重启Jupyter,选择刚添加的内核运行代码

4. 清理缓存并重启环境

  • 关闭所有Jupyter窗口,删除环境下的__pycache__文件夹,或运行命令批量清理:find . -name "__pycache__" -type d -exec rm -r {} +
  • 重启终端和Anaconda环境,重新打开Jupyter测试导入

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 05:22:06