- 前言
- 系統(tǒng)要求
- 當(dāng)前不支持
- Xcode
- Command Line Tools
- Homebrew
- Miniforge
- 創(chuàng)建虛擬環(huán)境
- 安裝 Tensorflow dependencies
- 安裝 Tensorflow
- 安裝 metal plugin
- 安裝必須的包
- 測試
- JupyterLab
- VSCode
- 延伸閱讀
- 參考
前言
幾天前护桦,見到 https://github.com/apple/tensorflow_macos 已經(jīng) Archived,并在 README 中看到了 TensorFlow v2.5 原生支持了 M1。
You can now leverage Apple’s tensorflow-metal PluggableDevice in TensorFlow v2.5 for accelerated training on Mac GPUs directly with Metal. Learn more here.
本文作為Apple Silicon Mac M1 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow, JupyterLab, VSCode)的更新篇,為大家詳細(xì)介紹如何安裝最新支持 GPU 加速版本的 TensorFlow。
系統(tǒng)要求
- macOS 12.0+
當(dāng)前不支持
- 多 GPU 支持
- 英特爾 GPU 的加速
- V1 TensorFlow 網(wǎng)絡(luò)
Xcode
從 App Store 安裝 Xcode销斟。
Command Line Tools
從 Apple Developer 下載安裝 Xcode Command Line Tools 或者執(zhí)行以下命令。
catchzeng@m1 ~ % xcode-select --install
Homebrew
catchzeng@m1 ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Miniforge
Anaconda 無法在 M1 上運(yùn)行, Miniforge 是用來替代它的。
從 https://github.com/conda-forge/miniforge 下載 Miniforge3-MacOSX-arm64
馁害。
執(zhí)行以下命令,安裝 Miniforge
catchzeng@m1 ~ % bash Miniforge3-MacOSX-arm64.sh
重啟終端并檢查 Python 安裝情況以故。
(base) catchzeng@m1 ~ % which python
/Users/catchzeng/miniforge3/bin/python
(base) catchzeng@m1 ~ % which pip
/Users/catchzeng/miniforge3/bin/pip
創(chuàng)建虛擬環(huán)境
創(chuàng)建一個(gè) conda 創(chuàng)建虛擬環(huán)境蜗细,這里使用 python 3.9.5
(TensorFlow 2.5 需要)。
(base) catchzeng@m1 ~ % conda create -n tensorflow python=3.9.5
(base) catchzeng@m1 ~ % conda activate tensorflow
(tensorflow) catchzeng@m1 ~ %
安裝 Tensorflow dependencies
首次安裝
(tensorflow) catchzeng@m1 ~ % conda install -c apple tensorflow-deps
注:tensorflow-deps 的版本是基于 TensorFlow 的怒详,因此可以根據(jù)自己的需求指定版本安裝:
v2.5
(tensorflow) catchzeng@m1 ~ % conda install -c apple tensorflow-deps==2.5.0
v2.6
(tensorflow) catchzeng@m1 ~ % conda install -c apple tensorflow-deps==2.6.0
升級(jí)安裝
如果之前已經(jīng)安裝了 v2.5
炉媒,想要更新 v2.6
的,可以執(zhí)行以下命令安裝昆烁。
# 卸載已安裝的 tensorflow-macos 和 tensorflow-metal
(tensorflow) catchzeng@m1 ~ % python -m pip uninstall tensorflow-macos
(tensorflow) catchzeng@m1 ~ % python -m pip uninstall tensorflow-metal
# 升級(jí) tensorflow-deps
(tensorflow) catchzeng@m1 ~ % conda install -c apple tensorflow-deps --force-reinstall
# 后者指向特定的 conda 環(huán)境
(tensorflow) catchzeng@m1 ~ % conda install -c apple tensorflow-deps --force-reinstall -n tensorflow
安裝 Tensorflow
(tensorflow) catchzeng@m1 ~ % python -m pip install tensorflow-macos
安裝 metal plugin
(tensorflow) catchzeng@m1 ~ % python -m pip install tensorflow-metal
安裝必須的包
(tensorflow) catchzeng@m1 ~ % brew install libjpeg
(tensorflow) catchzeng@m1 ~ % conda install -y pandas matplotlib scikit-learn jupyterlab
注意: libjpeg 是 matplotlib 需要依賴的庫吊骤。
測試
TensorFlow
(tensorflow) catchzeng@m1 ~ % python
Python 3.9.5 | packaged by conda-forge | (default, Oct 19 2021, 17:32:20)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Init Plugin
Init Graph Optimizer
Init Kernel
>>> print(tf.__version__)
2.6.0
>>>
JupyterLab
(tensorflow) catchzeng@m1 ~ % jupyter lab
from tensorflow.keras import layers
from tensorflow.keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
test_loss, test_acc = model.evaluate(test_images, test_labels)
test_acc
打開活動(dòng)監(jiān)視器,可以看到 Python 正在使用 GPU 資源静尼。
VSCode
安裝 Python 支持
選擇虛擬環(huán)境并信任 notebook
運(yùn)行 notebook
延伸閱讀
- Ubuntu 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow GPU, JupyterLab, VSCode)
- Mac 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow, JupyterLab, VSCode)
- Win10 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow GPU, JupyterLab, VSCode)
- Apple Silicon Mac M1 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow, JupyterLab, VSCode)