Constantes et variables

Introduction à TensorFlow en Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Qu'est-ce que TensorFlow ?

  • Bibliothèque libre pour le calcul numérique sur des graphes
    • Développée par l'équipe Google Brain
  • API de bas et de haut niveau
    • Addition, multiplication, différenciation
    • Modèles de Machine Learning
  • Changements majeurs dans TensorFlow 2.0
    • Exécution immédiate par défaut
    • Création de modèles avec Keras et Estimators
Introduction à TensorFlow en Python

Qu'est-ce qu'un tenseur ?

  • Généralisation des vecteurs et des matrices
  • Ensemble de nombres
  • Forme (shape) précise
Introduction à TensorFlow en Python

Qu'est-ce qu'un tenseur ?

L'image montre une tranche de pain, une tranche de pain divisée en 9 morceaux et un pain entier. Source : Public Domain Vectors

Introduction à TensorFlow en Python

Définir des tenseurs dans TensorFlow

import tensorflow as tf

# 0D Tensor
d0 = tf.ones((1,))
# 1D Tensor
d1 = tf.ones((2,))
# 2D Tensor
d2 = tf.ones((2, 2))
# 3D Tensor
d3 = tf.ones((2, 2, 2))
Introduction à TensorFlow en Python

Définir des tenseurs dans TensorFlow

# Print the 3D tensor
print(d3.numpy())
[[[1. 1.]
  [1. 1.]]

 [[1. 1.]
  [1. 1.]]]
Introduction à TensorFlow en Python

Définir des constantes dans TensorFlow

  • Une constante est la catégorie de tenseur la plus simple
    • Non entraînable
    • Peut avoir toute dimension
from tensorflow import constant

# Define a 2x3 constant.
a = constant(3, shape=[2, 3])
# Define a 2x2 constant.
b = constant([1, 2, 3, 4], shape=[2, 2])
Introduction à TensorFlow en Python

Fonctions pratiques pour définir des constantes

Opération Exemple
tf.constant() constant([1, 2, 3])
tf.zeros() zeros([2, 2])
tf.zeros_like() zeros_like(input_tensor)
tf.ones() ones([2, 2])
tf.ones_like() ones_like(input_tensor)
tf.fill() fill([3, 3], 7)
Introduction à TensorFlow en Python

Définir et initialiser des variables

import tensorflow as tf

# Define a variable
a0 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.float32)
a1 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.int16)
# Define a constant
b = tf.constant(2, tf.float32)
# Compute their product
c0 = tf.multiply(a0, b)
c1 = a0*b
Introduction à TensorFlow en Python

Passons à la pratique !

Introduction à TensorFlow en Python

Preparing Video For Download...