Константи та змінні

Вступ до TensorFlow на Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Що таке TensorFlow?

  • Відкрита бібліотека для чисельних обчислень на графах
    • Розроблена командою Google Brain
  • API низького та високого рівнів
    • Додавання, множення, диференціювання
    • Моделі машинного навчання
  • Важливі зміни в TensorFlow 2.0
    • За замовчуванням eager execution
    • Побудова моделей із Keras та Estimators
Вступ до TensorFlow на Python

Що таке тензор?

  • Узагальнення векторів і матриць
  • Набір чисел
  • Певна форма
Вступ до TensorFlow на Python

Що таке тензор?

Зображено скибку хліба, скибку, поділену на 9 частин, і цілу хлібину. Джерело: Public Domain Vectors

Вступ до TensorFlow на Python

Визначення тензорів у 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))
Вступ до TensorFlow на Python

Визначення тензорів у TensorFlow

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

 [[1. 1.]
  [1. 1.]]]
Вступ до TensorFlow на Python

Визначення констант у TensorFlow

  • Константа — найпростіший тип тензора
    • Не тренується
    • Може мати будь-яку розмірність
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])
Вступ до TensorFlow на Python

Зручні функції для створення констант

Операція Приклад
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)
Вступ до TensorFlow на Python

Визначення та ініціалізація змінних

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
Вступ до TensorFlow на Python

Давайте потренуємось!

Вступ до TensorFlow на Python

Preparing Video For Download...