TensorFlow 筹备 JupyterLab 交互式笔记本环境,不便咱们边写代码、边做笔记。

根底环境

以下是本文的根底环境,不详述装置过程了。

Ubuntu

  • Ubuntu 18.04.5 LTS (Bionic Beaver)

    • ubuntu-18.04.5-desktop-amd64.iso

CUDA

  • CUDA 11.2.2

    • cuda_11.2.2_460.32.03_linux.run
  • cuDNN 8.1.1

    • libcudnn8_8.1.1.33-1+cuda11.2_amd64.deb
    • libcudnn8-dev_8.1.1.33-1+cuda11.2_amd64.deb
    • libcudnn8-samples_8.1.1.33-1+cuda11.2_amd64.deb

Anaconda

  • Anaconda Python 3.8

    • Anaconda3-2020.11-Linux-x86_64.sh
conda activate base

装置 JupyterLab

Anaconda 环境里已有,如下查看版本:

jupyter --version

不然,如下进行装置:

conda install -c conda-forge jupyterlab

装置 TensorFlow

创立虚拟环境 tf,再 pip 装置 TensorFlow:

# create virtual environmentconda create -n tf python=3.8 -yconda activate tf# install tensorflowpip install --upgrade pippip install tensorflow

测试:

$ python - <<EOFimport tensorflow as tfprint(tf.__version__, tf.test.is_built_with_gpu_support())print(tf.config.list_physical_devices('GPU'))EOF
2021-04-01 11:18:17.719061: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.02.4.1 True2021-04-01 11:18:18.437590: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set2021-04-01 11:18:18.437998: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.12021-04-01 11:18:18.458471: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero2021-04-01 11:18:18.458996: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:pciBusID: 0000:01:00.0 name: GeForce RTX 2060 computeCapability: 7.5coreClock: 1.35GHz coreCount: 30 deviceMemorySize: 5.79GiB deviceMemoryBandwidth: 245.91GiB/s2021-04-01 11:18:18.459034: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.02021-04-01 11:18:18.461332: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.112021-04-01 11:18:18.461362: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.112021-04-01 11:18:18.462072: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.102021-04-01 11:18:18.462200: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.102021-04-01 11:18:18.462745: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusolver.so.102021-04-01 11:18:18.463241: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.112021-04-01 11:18:18.463353: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.82021-04-01 11:18:18.463415: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero2021-04-01 11:18:18.463854: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero2021-04-01 11:18:18.464170: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

Solution: Could not load dynamic library 'libcusolver.so.10'

cd /usr/local/cuda/lib64sudo ln -sf libcusolver.so.11 libcusolver.so.10

装置 IPython kernel

在虚拟环境 tf 里,装置 ipykernel 与 Jupyter 交互。

# install ipykernel (conda new environment)conda activate tfconda install ipykernel -ypython -m ipykernel install --user --name tf --display-name "Python TF"# run JupyterLab (conda base environment with JupyterLab)conda activate basejupyter lab

<!--
jupyter kernelspec list
jupyter kernelspec remove tf
-->

另一种形式,可用 nb_conda 扩大,其于笔记里会激活 Conda 环境:

# install ipykernel (conda new environment)conda activate tfconda install ipykernel -y# install nb_conda (conda base environment with JupyterLab)conda activate baseconda install nb_conda -y# run JupyterLabjupyter lab

最初,拜访 http://localhost:8888/ :

参考

  • Install TensorFlow 2

    • Build from source
    • GPU support
  • Install TensorFlow - Anaconda

    • anaconda / packages / tensorflow
  • Installing the IPython kernel
GoCoding 集体实际的教训分享,可关注公众号!