目录

安装 TensorFlow

sudo conda create --name tensorflow python
conda activate tensorflow

# 不指定环境(-n),默认安装到base环境
sudo conda install -c apple -n tensorflow tensorflow-deps
pip install tensorflow-macos
pip install tensorflow-metal
sudo conda install notebook -y

pip install numpy  --upgrade
pip install pandas  --upgrade
pip install matplotlib  --upgrade
pip install scikit-learn  --upgrade
pip install scipy  --upgrade
pip install plotly  --upgrade

验证

import sys
import tensorflow.keras
import tensorflow as tf
import platform

print(f"Python Platform: {platform.platform()}")
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print(f"Python {sys.version}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
Python Platform: macOS-13.2-arm64-arm-64bit
Tensor Flow Version: 2.11.0
Keras Version: 2.11.0
Python 3.10.9 (main, Jan 11 2023, 09:18:18) [Clang 14.0.6 ]
GPU is available

查看可用的设备

import tensorflow as tf
tf.config.experimental.get_visible_devices()
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
 PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

模型训练(Cifar100)

import tensorflow as tf
from tensorflow.keras.optimizers.legacy import Adam

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=Adam(), loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
Metal device set to: Apple M2 Max
Epoch 1/5
782/782 [==============================] - 49s 52ms/step - loss: 4.7070 - accuracy: 0.0810
Epoch 2/5
782/782 [==============================] - 38s 49ms/step - loss: 4.4909 - accuracy: 0.0892
Epoch 3/5
782/782 [==============================] - 38s 49ms/step - loss: 3.9776 - accuracy: 0.1300
Epoch 4/5
782/782 [==============================] - 39s 49ms/step - loss: 3.7069 - accuracy: 0.1676
Epoch 5/5
782/782 [==============================] - 39s 49ms/step - loss: 3.6723 - accuracy: 0.1756

切换设备

  • CPU
    with tf.device('/device:CPU:0'):
      model.fit(x_train, y_train, epochs=5, batch_size=64)
    
  • GPU
    with tf.device('/device:GPU:0'):
      model.fit(x_train, y_train, epochs=5, batch_size=64)
    

测试结果

使用的 Cifar100 数据集,模型网络 ResNet50,执行 1 次 Epoch。

设备 batch_size(32) batch_size(64) batch_size(128)
CPU and GPU CPU times: user 1min 26s, sys: 29.5s, total: 1min 55s
Wall time: 1min 28s
CPU times: user 50.4s, sys: 16.9s, total: 1min 7s
Wall time: 51.8s
CPU times: user 32.6s, sys: 9.19s, total: 41.8s
Wall time: 36.5s
GPU CPU times: user 1min 29s, sys: 29.2s, total: 1min 58s
Wall time: 1min 30s
CPU times: user 55.3s, sys: 16.5s, total: 1min 11s
Wall time: 56.4s
CPU times: user 35.6s, sys: 9.37s, total: 45s
Wall time: 36.9s
CPU CPU times: user 19min 16s, sys: 9min 55s, total: 29min 11s
Wall time: 8min 26s
CPU times: user 17min 18s, sys: 5min 43s, total: 23min 1s
Wall time: 6min 31s
CPU times: user 17min 13s, sys: 5min 20s, total: 22min 34s
Wall time: 5min 51s

参考资料