LoginSignup
22
37

More than 1 year has passed since last update.

M1 Macで機械学習やPythonはMiniforgeを使う

Last updated at Posted at 2021-12-18

現時点(2021年12月)で、M1 Macで機械学習用のPython環境構築は、Miniforgeを使うのが一番の簡単なので手順をメモしました。

Homebrewをインストール

こちらから指示に従ってHomebrewをインストール。

Miniforgeをインストール

Miniforgeをインストール。Miniforgeのパッケージ管理システムである「conda」を使えるようにします。

# Miniforgeをダウンロード
% wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh

# インストール
% bash ./Miniforge3-MacOSX-arm64.sh
# ここでいっぱいEnterキーを押して、"yes"をタイプしてEnterキー

# Miniforgeのcondaを起動
% source ~/miniforge3/bin/activate

Tensorflowをインストール

機械学習で使ういろいろなものをここでインストールします。まずは、mlという環境を作ります。

% conda create -name ml python=3.9
% conda activate ml

最初に、Tensorflowをインストールします。

% conda install -c apple tensorflow-deps -y
% python -m pip install tensorflow-macos
% python -m pip install tensorflow-metal

うまくいけば、以下のプログラムを実行すると、バージョン情報が表示されます。

test-tf.py
import tensorflow as tf
print(tf.__version__)

Transformer をインストール

次に、自然言語処理に役立つ Transformer をインストールします。

% conda install -c huggingface transformers -y

その他、機械学習でよく使うパッケージをインストールします。

% conda install -c conda-forge scikit-learn -y
% conda install -c conda-forge pandas -y
% conda install -c conda-forge jupyterlab -y
% conda install -c pytorch pytorch torchvision -y

うまくうけば、以下のプログラムが動きます。

test-transformers.py
import transformers as tr
print(tr.__version__)

音声関連のライブラリ

その他必要に応じてインストール。
諸々の機械学習関連のライブラリのコンパイル環境など。

% conda install numpy decorator attrs cython
% conda install llvmdev
% conda install cmake
% conda install tornado psutil xgboost cloudpickle pytest
% conda install -c conda-forge librosa -y

Condaの使い方

ターミナルを立ち上げたあと、以下のようにして、condaの環境を開始します。

% source ~/miniforge3/bin/activate
% conda activate ml

環境一覧の確認:

% conda env list

Jupyter notebookを起動する場合、以下のコマンドを実行します。

% jupyter notebook

スクリーンショット 2021-12-18 12.58.34.png

二度目移行の始め方

一度設定してしまえば、後は、以下のようにしてminiforgeのconda環境を始めます。

# Miniforgeのcondaを起動
% source ~/miniforge3/bin/activate
% conda activate ml

参考

22
37
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
22
37