背景:在使用Jupyter过程中引入tensorflow内核似乎挂掉的问题

我的设备:MacBook air M1;macos 13.2.1

方法一:

一、打开终端面,重新创建一个tensorflow环境:

conda create -n tf tensorflow
conda activate tf

二、由于新创建的虚拟环境没有jupyter,需要重新安装。
这里有两种方法:
1、使用pip安装

pip install jupyter notebook

2、直接食用anaconda安装,home-notebook-install:

tensorflow内核挂掉_tensorflow

注意:上方的环境是否是你想要安装Jupyter的环境。
3、进入Jupyter,输入测试代码:

import tensorflow as tf
import os
import warnings
warnings.filterwarnings('ignore') # 防止出现警告
os.environ["CUDA_VISIBLE_DEVICES"]="0"
tf.compat.v1.disable_eager_execution()
hello=tf.constant('Hello,TensorFlow')
config=tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9
sess=tf.compat.v1.Session(config=config)
print(sess.run(hello))

输出为:

b'Hello,TensorFlow'

表明tensorflow安装成功,如果没有运行成功,那么就换第二种方法:

方法二:

1. conda activate tf          ##替换成自己的虚拟环境名
2. conda install ipykernel    ##安装ipykernel
3. sudo python -m ipykernel install --name tf  ##在ipykernel中安装当前环境
4. conda deactivate

tensorflow内核挂掉_python_02


执行命令完毕之后,进入Jupyter中,更换自己的内核:Kernel–change kernel–tf(你自己创建的名称)

最后执行以下代码:

import tensorflow as tf
import os
import warnings
warnings.filterwarnings('ignore') # 防止出现警告
os.environ["CUDA_VISIBLE_DEVICES"]="0"
tf.compat.v1.disable_eager_execution()
hello=tf.constant('Hello,TensorFlow')
config=tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9
sess=tf.compat.v1.Session(config=config)
print(sess.run(hello))

输出为:

b'Hello,TensorFlow'

以上是我的解决方案,可以作为参考!!!