インストール

Pip によるインストール

以下を実行して TensorFlow デシジョン フォレストをインストールします。

# Install TensorFlow Decision Forests.
pip3 install tensorflow_decision_forests --upgrade

次に、次を実行してインストールを確認します。

# Check the version of TensorFlow Decision Forests.
python3 -c "import tensorflow_decision_forests as tfdf; print('Found TF-DF v' + tfdf.__version__)"

ソースからビルドする

Linux

設定

要件

  • バゼル >= 3.7.2
  • Python >= 3
  • ギット
  • Python パッケージ: numpy tensorflow pandas

依存関係を手動でインストールする代わりに、 TensorFlow Build dockerを使用できます。このオプションを選択した場合は、Docker をインストールします。

編集

次のように TensorFlow デシジョン フォレストをダウンロードします。

# Download the source code of TF-DF.
git clone https://github.com/tensorflow/decision-forests.git
cd decision-forests

オプション: TensorFlow デシジョン フォレストはYggdrasil デシジョン フォレストに依存します。 Yggdrasil コードを編集する場合は、Yggdrasil github のクローンを作成し、 third_party/yggdrasil_decision_forests/workspace.bzlでパスを適宜変更します。

オプション: docker オプションを使用する場合は、 start_compile_docker.shスクリプトを実行し、次のステップに進みます。 docker オプションを使用したくない場合は、次のステップに直接進みます。

# Optional: Install and start the build docker.
./tools/start_compile_docker.sh

次のコマンドを使用して、TF-DF の単体テストをコンパイルして実行します。 test_bazel.shpython3.8およびマシン上のデフォルトのコンパイラ用に構成されていることに注意してください。この構成を変更するには、ファイルを直接編集します。

# Build and test TF-DF.
./tools/test_bazel.sh

次のコマンドを使用して pip パッケージを作成し、テストします。 python3.8 を、使用する Python のバージョンに置き換えます。 test_bazel.shスクリプトと同じバージョンの Python を使用する必要はないことに注意してください。

構成がmanylinux2014と互換性がある場合、 manylinux2014と互換性のある pip パッケージが生成されます。

構成が manylinux2014 と互換性がない場合、 manylinux2014と互換性のない pip パッケージが生成され、最終チェックは失敗します。自分のマシンで TF-DF を使用したい場合でも問題ありません。ビルドを manylinux2014 と互換性のあるものにする簡単な方法は、上記の Docker を使用することです。

# Build and test a Pip package.
./tools/build_pip_package.sh python3.9

このコマンドは、TF-DF pip パッケージをインストールし、 examples/minimal.pyの例を実行します。 Pip パッケージはdist/ディレクトリにあります。

互換性のある他のバージョンの Python 用の Pip パッケージを作成する場合は、次を実行します。

# Install the other versions of python (assume only python3.9 is installed; this is the case in the build docker).
sudo apt-get update && sudo apt-get install python3.9 python3-pip

# Create the Pip package for the other version of python
./tools/build_pip_package.sh python3.9

あるいは、次のコマンドを実行して、pyenv を使用して互換性のあるすべての Python バージョンの pip パッケージを作成することもできます。詳細についてはtools/build_pip_package.shのヘッダーを参照してください。

# Build and test all the Pip package using Pyenv.
./tools/build_pip_package.sh ALL_VERSIONS

マックOS

設定

要件

  • XCodeコマンドラインツール
  • バゼル(バゼリスク推奨)
  • Python >= 3.9
  • ギット
  • Pyenv (複数の Python バージョンを使用して Pip パッケージを構築するため)

構築/パッケージング (Apple CPU)

Apple CPU を搭載した MacOS マシンをお持ちの場合は、次の手順でビルドできます。

  1. 3 つのリポジトリのクローンを作成し、パスを調整します。

    git clone https://github.com/tensorflow/decision-forests.git
    git clone https://github.com/google/yggdrasil-decision-forests.git
    git clone --branch boost-1.75.0 https://github.com/boostorg/boost.git
    (cd boost && git submodule update --init --checkout --force)
    # Adjust path TF-DF --> YDF
    perl -0777 -i.original -pe 's/    http_archive\(\n        name = "ydf",\n        urls = \["https:\/\/github.com\/google\/yggdrasil-decision-forests\/archive\/refs\/heads\/main.zip"\],\n        strip_prefix = "yggdrasil-decision-forests-main",\n    \)/    native.local_repository\(\n        name = "ydf",\n        path = "..\/yggdrasil-decision-forests",\n    \)/igs' decision-forests/third_party/yggdrasil_decision_forests/workspace.bzl
    # Adjust path YDF --> Boost
    perl -0777 -i.original -pe 's/    new_git_repository\(\n        name = "org_boost",\n        branch = branch,\n        build_file_content = build_file_content,\n        init_submodules = True,\n        recursive_init_submodules = True,\n        remote = "https:\/\/github.com\/boostorg\/boost",\n    \)/    native.new_local_repository\(\n        name = "org_boost",\n        path = "..\/boost",\n        build_file_content = build_file_content,\n    \)/igs' yggdrasil-decision-forests/third_party/boost/workspace.bzl
    

    Tensorflow コミット ハッシュは MacOS ビルドでは壊れている場合があるため、test_bazel.sh スクリプトを手動で調整して修正する必要がある場合があります。

  2. (オプション) 新しい仮想環境を作成し、アクティブ化します。

    python3 -m venv venv
    source venv/source/activate
    
  3. Apple CPU の TensorFlow 依存関係を調整する

    perl -0777 -i.original -pe 's/tensorflow~=/tensorflow-macos~=/igs' decision-forests/configure/setup.py
    
  4. 使用して実行する Python バージョンを決定します。

    cd decision-forests
    # This will compile with the latest Tensorflow version in the tensorflow-macos repository.
    RUN_TESTS=1 PY_VERSION=3.9 TF_VERSION=mac-arm64 ./tools/test_bazel.sh
    
  5. Pip パッケージをビルドする

    # First, we deactivate our virtualenv, since the Pip script uses a different one.
    deactivate
    # Build the packages.
    ./tools/build_pip_package.sh ALL_VERSIONS_MAC_ARM64
    
  6. パッケージはdecision-forests/dist/にあります。

Intel CPU のクロスコンパイル

Apple CPU を搭載した MacOS マシンをお持ちの場合は、次のように Intel CPU を搭載した MacOS マシン用の TF-DF をクロスコンパイルします。

  1. Apple CPU のガイドのステップ 1 ~ 3 および 5 に従い、ステップ 4 はスキップします。ビルド ディレクトリをクリーンアップするbazel --bazelrc=tensorflow_bazelrc clean --expungeを実行する必要がある場合があります。

  2. 使用して実行する Python バージョンを決定します。

    cd decision-forests
    # This will compile with the latest Tensorflow version in the tensorflow-macos repository.
    RUN_TESTS=0 PY_VERSION=3.9 TF_VERSION=mac-intel-crosscompile ./tools/test_bazel.sh
    
  3. Pip パッケージをビルドする

    # First, we deactivate our virtualenv, since the Pip script uses a different one.
    deactivate
    # Build the packages.
    ./tools/build_pip_package.sh ALL_VERSIONS_MAC_INTEL_CROSSCOMPILE
    
  4. パッケージはdecision-forests/dist/にあります。

最後のメモ

TF-DF のコンパイルは、TensorFlow Pip パッケージTensorFlow Bazel の依存関係に依存します。 TensorFlow のごく一部のみがコンパイルされます。単一の強力なワークステーションで TF-DF をコンパイルするには、最大 10 分かかります。