Ga naar inhoud

TensorFlow-installatie

In dit artikel

TensorFlow is een open-sourceplatform voor machine learning en het trainen van diepe neurale netwerken, ontwikkeld door Google. Met TensorFlow kunt u complexe neurale netwerken maken, trainen en gebruiken via een gebruiksvriendelijke API in Python.

Dit platform ondersteunt zowel traditionele machine learning-algoritmen, zoals lineaire en logistische regressie, als meer geavanceerde architecturen, waaronder convolutionele neurale netwerken (Convolutional Neural Networks, CNN) en recurrente neurale netwerken (Recurrent Neural Networks, RNN).

Informatie

Als u TensorFlow niet handmatig wilt installeren, bestel dan een server met een kant-en-klare image uit de marketplace — daarop zijn NVIDIA-stuurprogramma's en TensorFlow al geïnstalleerd. Zie voor details het artikel TensorFlow.

TensorFlow-installatie op Linux

Deze instructie is geschikt voor Ubuntu 24.04 (Python 3.12) en Ubuntu 22.04 (Python 3.10). De commando's zijn voor beide versies hetzelfde; alleen de standaard Python-versie van het systeem verschilt.

Opmerking

Als u van plan bent GPU-versnelling te gebruiken, installeer dan NVIDIA-stuurprogramma's volgens deze instructie. U hoeft de CUDA Toolkit en cuDNN niet apart te installeren — bij de installatie van het pakket tensorflow[and-cuda] worden de benodigde CUDA- en cuDNN-bibliotheken automatisch in de virtuele omgeving geïnstalleerd.

  1. Installeer Python, de module voor virtuele omgevingen en pip:

    sudo apt update
    sudo apt install python3 python3-venv python3-pip
    

    Gebruik de Python-versie die standaard in het systeem is geïnstalleerd (3.12 in Ubuntu 24.04, 3.10 in Ubuntu 22.04). Controleer voordat u een andere versie installeert op de officiële website of TensorFlow deze ondersteunt.

    Opmerking

    Vanaf Ubuntu 24.04 en Debian 12 is het niet toegestaan om pakketten met pip in de systeem-Python te installeren (de fout externally-managed-environment). Daarom wordt TensorFlow in een virtuele omgeving geïnstalleerd.

  2. Maak een virtuele omgeving voor Python:

    python3 -m venv venv
    
  3. Activeer de virtuele omgeving:

    source venv/bin/activate
    

    Na succesvolle activatie bevat de prompt de naam van de virtuele omgeving tussen haakjes:

    (venv) user@49069:~$
    

    Opmerking

    U kunt zoveel virtuele omgevingen maken als u wilt en verschillende bibliotheken installeren (ook gelijktijdig, maar dit kan soms conflicten veroorzaken).

  4. Werk pip bij en installeer de TensorFlow-bibliotheek met GPU-ondersteuning:

    pip install --upgrade pip
    pip install 'tensorflow[and-cuda]'
    

    Als u geen GPU gebruikt, volstaat het om het pakket zonder extra afhankelijkheden te installeren: pip install tensorflow.

    Opmerking

    Voorheen was het voor het gebruik van TensorFlow met een GPU nodig om TensorRT handmatig te installeren en de paden naar de CUDA- en cuDNN-bibliotheken toe te voegen aan de variabele LD_LIBRARY_PATH. Dit is in de huidige TensorFlow-versies niet meer nodig, en ondersteuning voor TensorRT is vanaf versie 2.18 uit de standaard TensorFlow-builds verwijderd.

  5. Verlaat de virtuele omgeving:

    deactivate
    

TensorFlow uitvoeren

Om met TensorFlow te werken, activeert u eenvoudigweg de virtuele omgeving vanuit de hoofdmap van uw home-map:

source venv/bin/activate

Daarna kunt u de Python-console starten of uw eigen scripts uitvoeren.

TensorFlow-installatie verifiëren

Om de functionaliteit en GPU-ondersteuning van de bibliotheek te verifiëren, start het volgende Python-programma in de console:

import tensorflow as tf
print(tf.reduce_sum(tf.random.normal([1000, 1000])))
print(tf.config.list_physical_devices('GPU'))

Bij succesvolle installatie ontvangt u een uitvoer die er ongeveer als volgt uitziet, waarbij uw GPU-gebruik wordt weergegeven (het voorbeeld is verkregen op Ubuntu 22.04 met Python 3.10; uw versies, servicemeldingen en GPU-model zullen verschillen):

(venv) user1@49069:~$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf

2024-04-22 23:39:54.472502: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
>>> print(tf.reduce_sum(tf.random.normal([1000, 1000])))

2024-04-22 23:39:55.810888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22282 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 4090, pci bus id: 0000:07:00.0, compute capability: 8.9
tf.Tensor(332.5041, shape=(), dtype=float32)
>>> print(tf.config.list_physical_devices('GPU'))

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>>

U kunt bovendien het test-Pythonscript voor het trainen van een neuronaal netwerk 2c_nn_mnist_customtrain.py downloaden en uitvoeren vanuit deze GitHub-repository.

Na voltooiing van de uitvoering zou u een uitvoer moeten ontvangen die er ongeveer als volgt uitziet (de servicemeldingen van TensorFlow zijn afhankelijk van de versie):

(venv) user1@49069:~$ python ./2c_nn_mnist_customtrain.py
2024-04-23 10:35:25.832754: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-04-23 10:35:28.404381: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22282 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 4090, pci bus id: 0000:07:00.0, compute capability: 8.9
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1713861338.172126   21279 service.cc:145] XLA service 0x7f36a3bd2ec0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1713861338.172187   21279 service.cc:153]   StreamExecutor device (0): NVIDIA GeForce RTX 4090, Compute Capability 8.9
2024-04-23 10:35:38.180648: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-04-23 10:35:38.207205: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:465] Loaded cuDNN version 8902
I0000 00:00:1713861338.355523   21279 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9022 time: 14.9 test-acc: 0.932 (error: 6.75%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9474 time: 4.74 test-acc: 0.953 (error: 4.71%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9628 time: 4.83 test-acc: 0.963 (error: 3.67%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9734 time: 4.86 test-acc: 0.968 (error: 3.25%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9798 time: 4.9 test-acc: 0.97 (error: 3.04%)