Tensorflow
Contents
11.5. Tensorflow#
TensorFlow is a free and open-source software library for machine learning and artificial intelligence. It can be used across a range of tasks but has a particular focus on training and inference of deep neural networks. wikipedia
11.5.1. Install Tensorflow#
You need ensure that card in computer support Tensorflow before want install it: https://developer.nvidia.com/cuda-gpus
I recommend you should use card RTX at the momment. First at all, you need install tensorflow :
pip install tensorflow
11.5.2. Install Cuda#
You need install cuda and cudnn before work with tensorflow, because tensor need GPU to use for process.
Warning :
Please download version map with version of tensorflow, see at https://www.tensorflow.org/install/source#gpu/
Please download tensorflow map with version python, see at : https://www.tensorflow.org/install/source_windows#tested_build_configurations
To check Cuda version installed in computer, let use command:
nvidia-smi
nvcc --version
When Cuda intalled sucessfully , now you can check tensorflow is supported tensorfllow version and gpu support. In code sample bellow , I will check version of python,pandas,tensorflow and list gpu supported.
import sys
import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
2023-02-22 00:54:40.660623: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-02-22 00:54:40.791897: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.16/x64/lib
2023-02-22 00:54:40.791924: I tensorflow/compiler/xla/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2023-02-22 00:54:41.673014: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.16/x64/lib
2023-02-22 00:54:41.673109: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.16/x64/lib
2023-02-22 00:54:41.673118: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
Tensor Flow Version: 2.11.0
Keras Version: 2.11.0
Python 3.8.16 (default, Jan 11 2023, 00:28:51)
[GCC 11.3.0]
Pandas 1.5.3
Scikit-Learn 1.2.1
GPU is NOT AVAILABLE
2023-02-22 00:54:43.167981: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.16/x64/lib
2023-02-22 00:54:43.168011: W tensorflow/compiler/xla/stream_executor/cuda/cuda_driver.cc:265] failed call to cuInit: UNKNOWN ERROR (303)
2023-02-22 00:54:43.168037: I tensorflow/compiler/xla/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (fv-az566-882): /proc/driver/nvidia/version does not exist
11.5.3. Start with Tensorflow#
import tensorflow as tf
print(tf.__version__)
msg = tf.constant("Hello TesorFlow")
tf.print(msg)
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
result = tf.multiply(x1,x2)
print(result)
#OUT = result
2.11.0
Hello TesorFlow
tf.Tensor([ 5 12 21 32], shape=(4,), dtype=int32)
2023-02-22 00:54:43.178890: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Use GPU inside tensorflow :
tf.debugging.set_log_device_placement(True)
gpus = tf.config.list_logical_devices('GPU')
if gpus:
# Replicate your computation on multiple GPUs
c = []
for gpu in gpus:
with tf.device(gpu.name):
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c.append(tf.matmul(a, b))
with tf.device('/CPU:0'):
matmul_sum = tf.add_n(c)
print(matmul_sum)
Read more discusstion at :
https://forum.dynamobim.com/t/is-python-3-in-dynamo-use-gpu-or-cpu/69619/11
https://medium.com/@medasuryatej/install-tensorflow-gpu-2-0-f4e215438199