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

MacBook Air M1环境下Anaconda Jupyter Notebook中TensorFlow与Keras安装使用问题

解决M1 Mac上TensorFlow导入及Jupyter内核崩溃问题

问题根源

  • M1为arm64架构,默认conda渠道的TensorFlow适配性不足,且conda与pip混用会引发依赖冲突
  • Jupyter未关联到你创建的tf conda环境,导致环境不一致

分步解决步骤

1. 清理混乱的现有环境

先删除之前的conda环境,避免残留依赖干扰:

conda remove -n tf --all -y

如果base环境里也用pip装过TensorFlow/Keras,一并卸载:

pip uninstall tensorflow keras -y

2. 创建M1适配的TensorFlow环境

使用conda-forge渠道安装arm64版本的TensorFlow,推荐搭配Python3.9(兼容性最优):

conda create -n tf -c conda-forge tensorflow python=3.9 -y

3. 将conda环境关联到Jupyter内核

激活tf环境后,安装ipykernel并添加专属内核:

conda activate tf
pip install ipykernel
python -m ipykernel install --user --name tf --display-name "TensorFlow (M1)"

4. 启动Jupyter并选择正确内核

在终端激活tf环境后启动Notebook:

conda activate tf
jupyter notebook

打开Notebook后,通过右上角Kernel -> Change Kernel选择TensorFlow (M1)内核。

5. 验证导入

在Notebook中运行以下代码测试:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM

print("TensorFlow & Keras导入成功!")

新手额外提示(针对LSTM股价预测)

  • 无需单独安装Keras,tf.keras是TensorFlow内置的官方实现,单独安装会导致版本冲突
  • LSTM对数据尺度敏感,处理股价数据前务必做归一化(例如用sklearn.preprocessing.MinMaxScaler)
  • 搭建堆叠LSTM时,除最后一层外,前面的LSTM层需要设置return_sequences=True,确保序列数据能传递给下一层

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 13:10:29