Mac(M1芯片)上Tensorflow 2.11.0设置随机张量种子时内核崩溃求助
M1 Mac上TensorFlow设置种子导致内核崩溃的解决办法
检查并安装适配Apple Silicon的TensorFlow版本
M1芯片的Mac不能直接使用通用版TensorFlow,必须安装官方适配的tensorflow-macos和tensorflow-metal加速插件。先卸载原有版本,再重装:pip uninstall tensorflow -y pip install tensorflow-macos tensorflow-metal修正随机种子的设置代码
避免使用已弃用的API,正确的种子设置方式如下:import tensorflow as tf # 设置全局随机种子保证全局可复现 tf.random.set_seed(42) # 生成随机张量时可按需指定操作级种子 rand_tensor = tf.random.normal(shape=(2, 2), seed=123)确认conda环境为arm64架构
M1 Mac的conda环境必须是arm64架构,否则会存在兼容性问题。执行以下命令查看环境架构:conda info若输出的
platform不是osx-arm64,则重新创建arm64环境:conda create -n tf-arm python=3.9 conda activate tf-arm(推荐使用Python 3.8~3.10版本,与当前TensorFlow-macos适配性更佳)
重置Jupyter内核(若使用Jupyter运行代码)
内核崩溃后,需确保Jupyter使用的是适配的conda环境内核:conda activate tf-arm python -m ipykernel install --user --name tf-arm --display-name "TensorFlow (ARM)"重启Jupyter后选择该内核执行代码。
内容的提问来源于stack exchange,提问作者siva82kb
相关产品推荐
相关产品推荐

