你参考其他人的文章,安装好M1版本的TensorFlow环境后,使用notebook出现了一些问题。
import tensorflow as tf
import os
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'
2024-01-08 16:05:50.503566: I metal_plugin/src/device/metal_device.cc:1154] Metal device set to: Apple M1 Max
2024-01-08 16:05:50.503598: I metal_plugin/src/device/metal_device.cc:296] systemMemory: 64.00 GB
2024-01-08 16:05:50.503603: I metal_plugin/src/device/metal_device.cc:313] maxCacheSize: 24.00 GB
2024-01-08 16:05:50.503654: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2024-01-08 16:05:50.503691: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
2024-01-08 16:05:50.505344: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
核心信息为:
2024-01-08 16:05:50.503566: I metal_plugin/src/device/metal_device.cc:1154] Metal device set to: Apple M1 Max
2024-01-08 16:05:50.503654: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2024-01-08 16:05:50.503691: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: )
2024-01-08 16:05:50.505344: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
出现则表示安装成功了。
同样,你可以直接在终端中执行以下命令:
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
出现以下内容,表示成功:
但是,如果使用TensorFlow过程中,notebook弹框显示:
Kernel Restarting
The kernel for Untitled.ipynb appears to have died. It will restart automatically.
这确实是个问题!
最后正确解决的办法是重新装TensorFlow。
其实,你在网上找教程,可能找到的是旧版本的安装教程!
所以导致你安装错误了,而目前我最新的版本是2.15.0,正确的安装方式请查看我的另外一篇文章:MacOS安装Miniforge、Tensorflow、Jupyter Lab等(2024年最新)