1. 程式人生 > 其它 >macbook pro m1安裝tensorflow

macbook pro m1安裝tensorflow

一、安裝Miniforge3

這裡下載ARM版Miniforge3,如下圖所示

完成後開啟下載目錄終端,執行shell指令碼來安裝。輸入如下命令
bash Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate

二、安裝TensorFlow依賴項

conda install -c apple tensorflow-deps

三、 安裝基礎TensorFlow

python -m pip install tensorflow-macos

四、安裝tensorflow-metal外掛

python -m pip install tensorflow-metal

測試

import tensorflow as tf
# 載入MNIST資料集
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 搭建模型
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
# 訓練並驗證模型
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test,  y_test, verbose=2)

結果:

參考

https://www.1024sou.com/article/445622.html
https://www.cyberlight.xyz/passage/tensorflow-new-apple-m1
https://developer.apple.com/metal/tensorflow-plugin/