要在 TensorFlow 中使用神经结构学习 (NSL),您可以通过以下几种方式设置环境:
- 学习和使用 NSL 的最简单方式无需安装:使用 Google Colaboratory 直接在浏览器中运行 NSL 教程。
- 要在本地计算机上使用 NSL,请使用 Python 的
pip软件包管理器安装 NSL 软件包。 - 如果您的计算机配置比较独特,请从源代码构建 NSL。
注:NSL 要求使用 TensorFlow 1.15 或更高版本。NSL 还支持除 v2.1(其中包含与 NSL 不兼容的错误)之外的 TensorFlow 2.x 版本。
使用 pip 安装 Neural Structured Learning
1. 安装 Python 开发环境。
在 Ubuntu 上:
sudo apt updatesudo apt install python3-dev python3-pip # Python 3sudo pip3 install --upgrade virtualenv # system-wide install
在 macOS 上:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"export PATH="/usr/local/bin:/usr/local/sbin:$PATH"brew updatebrew install python # Python 3sudo pip3 install --upgrade virtualenv # system-wide install
2. 创建虚拟环境。
virtualenv --python python3 "./venv"source "./venv/bin/activate"pip install --upgrade pip
注:要退出虚拟环境,请运行 deactivate。
3. 安装 TensorFlow
CPU 支持:
pip install 'tensorflow>=1.15.0'GPU 支持:
pip install 'tensorflow-gpu>=1.15.0'4. 安装 Neural Structured Learning pip 软件包。
pip install --upgrade neural_structured_learning5.(可选)测试 Neural Structured Learning。
python -c "import neural_structured_learning as nsl"成功:Neural Structured Learning 现已完成安装。
构建 Neural Structured Learning pip 软件包
1. 安装 Python 开发环境。
在 Ubuntu 上:
sudo apt updatesudo apt install python3-dev python3-pip # Python 3sudo pip3 install --upgrade virtualenv # system-wide install
在 macOS 上:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"export PATH="/usr/local/bin:/usr/local/sbin:$PATH"brew updatebrew install python # Python 3sudo pip3 install --upgrade virtualenv # system-wide install
2. 安装 Bazel。
安装 Bazel,即用于编译 Neural Structured Learning 的构建工具。
3. 克隆 Neural Structured Learning 仓库。
git clone https://github.com/tensorflow/neural-structured-learning.git4. 创建虚拟环境。
virtualenv --python python3 "./venv"source "./venv/bin/activate"pip install --upgrade pip
注:要退出虚拟环境,请运行 deactivate。
5. 安装 Tensorflow
注意,NSL 要求使用 TensorFlow 1.15 或更高版本。NSL 还支持 TensorFlow 2.0。
CPU 支持:
pip install 'tensorflow>=1.15.0'GPU 支持:
pip install 'tensorflow-gpu>=1.15.0'6. 安装 Neural Structured Learning 依赖项。
cd neural-structured-learningpip install --requirement neural_structured_learning/requirements.txt
7.(可选)对 Neural Structured Learning 进行单元测试。
bazel test //neural_structured_learning/...8. 构建 pip 软件包。
python setup.py bdist_wheel --universal --dist-dir="./wheel"9. 安装 pip 软件包。
pip install --upgrade ./wheel/neural_structured_learning*.whl10. 测试 Neural Structured Learning。
python -c "import neural_structured_learning as nsl"成功:Neural Structured Learning 软件包已完成构建。