2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M1 MacにTensorflowをインストールするときにいつもググってしまうのでもうググらないために残しておくメモ

Posted at

M1 MacにTensorflowをインストール

すでに沢山の類似記事があると思いますが、
自分の環境用に備忘録として残しておきます。

Command Line Tools for Xcode のインストール

インストールしたらバージョンを確認

$ xcode-select --install
$ xcode-select -v

仮想環境を整える

[Miniforge] https://github.com/conda-forge/miniforge からMiniforge3-MacOSX-arm64をダウンロードする
以下を実行するとMiniforgeがインストールされる

bash /(Miniforge3-MacOSX-arm64.shのあるディレクトリ)/Miniforge3-MacOSX-arm64.sh

その後、Tensorflowを他のQiitaの記事を参考にしてインストールしてみたのですが、どうにも動かないのでM1Mac用に最適化されたTensorflow-metalを配布してくれてる[サイト]https://developer.apple.com/metal/tensorflow-plugin/ を参考にインストール
サイトの指示通りにインストールする

python3 -m venv ~/venv-metal
source ~/venv-metal/bin/activate
python -m pip install -U pip
python -m pip install tensorflow
python -m pip install tensorflow-metal

サイトにある以下のテストコードを実行して問題なく動いているか確認した

import tensorflow as tf

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=False)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

結果:

(venv-metal) (base) ****@MacBook-Air ML_rust % python3 test_TF.py 
2024-01-07 15:36:21.721201: I metal_plugin/src/device/metal_device.cc:1154] Metal device set to: Apple M1
2024-01-07 15:36:21.721223: I metal_plugin/src/device/metal_device.cc:296] systemMemory: 16.00 GB
2024-01-07 15:36:21.721228: I metal_plugin/src/device/metal_device.cc:313] maxCacheSize: 5.33 GB
2024-01-07 15:36:21.721270: 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-07 15:36:21.721302: 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>)
Epoch 1/5
2024-01-07 15:36:24.654776: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
782/782 [==============================] - 490s 612ms/step - loss: 4.6229 - accuracy: 0.0816 
Epoch 2/5
782/782 [==============================] - 112s 143ms/step - loss: 4.1233 - accuracy: 0.1355
Epoch 3/5
782/782 [==============================] - 109s 140ms/step - loss: 3.7688 - accuracy: 0.1725
Epoch 4/5
782/782 [==============================] - 109s 140ms/step - loss: 4.3786 - accuracy: 0.1044
Epoch 5/5
782/782 [==============================] - 109s 139ms/step - loss: 4.1251 - accuracy: 0.1058

無事終了した
ここまで青息吐息だったので後で記事を整えます。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?