Operații de bază

Introducere în TensorFlow în Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Ce este o operație TensorFlow?

Imaginea prezintă operații și tensori într-un graf TensorFlow, desenat cu TensorBoard. Două perechi de matrice sunt combinate prin operația de adunare. Sumele rezultate sunt apoi înmulțite.

Introducere în TensorFlow în Python

Ce este o operație TensorFlow?

Imaginea prezintă operații și tensori într-un graf TensorFlow, desenat cu TensorBoard. Este prezentată o singură operație de adunare.

Introducere în TensorFlow în Python

Ce este o operație TensorFlow?

Imaginea prezintă operații și tensori într-un graf TensorFlow, desenat cu TensorBoard. Sunt prezentate două operații de adunare.

Introducere în TensorFlow în Python

Ce este o operație TensorFlow?

Imaginea prezintă operații și tensori într-un graf TensorFlow, desenat cu TensorBoard. Două perechi de matrice sunt combinate prin operația de adunare. Sumele rezultate sunt apoi înmulțite.

Introducere în TensorFlow în Python

Aplicarea operatorului de adunare

#Import constant and add from tensorflow
from tensorflow import constant, add

# Define 0-dimensional tensors
A0 = constant([1])
B0 = constant([2])
# Define 1-dimensional tensors
A1 = constant([1, 2])
B1 = constant([3, 4])
# Define 2-dimensional tensors
A2 = constant([[1, 2], [3, 4]])
B2 = constant([[5, 6], [7, 8]])
Introducere în TensorFlow în Python

Aplicarea operatorului de adunare

# Perform tensor addition with add()
C0 = add(A0, B0)
C1 = add(A1, B1)
C2 = add(A2, B2)
Introducere în TensorFlow în Python

Adunarea tensorilor

  • Operația add() realizează adunarea element cu element a doi tensori

  • Adunarea element cu element necesită tensori cu aceeași formă:

    • Adunare scalară: $1+2=3$
    • Adunare vectorială: $[1,2]+[3,4]=[4,6]$
    • Adunare matriceală: $\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} + \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix}$
  • Operatorul add() este supraîncărcat

Introducere în TensorFlow în Python

Înmulțirea tensorilor în TensorFlow

  • Înmulțirea element cu element se realizează cu operația multiply()

    • Tensorii înmulțiți trebuie să aibă aceeași formă
    • Ex.: [1,2,3] și [3,4,5] sau [1,2] și [3,4]
  • Înmulțirea matriceală se realizează cu operatorul matmul()

    • Operația matmul(A,B) înmulțește A cu B
    • Numărul de coloane ale lui A trebuie să egaleze numărul de linii ale lui B
Introducere în TensorFlow în Python

Aplicarea operatorilor de înmulțire

# Import operators from tensorflow
from tensorflow import ones, matmul, multiply

# Define tensors
A0 = ones(1)
A31 = ones([3, 1])
A34 = ones([3, 4])
A43 = ones([4, 3])

  • Ce operații sunt valide?
    • multiply(A0, A0), multiply(A31, A31), și multiply(A34, A34)
    • matmul(A43, A34), dar nu matmul(A43, A43)
Introducere în TensorFlow în Python

Suma pe dimensiunile tensorului

  • Operatorul reduce_sum() sumează de-a lungul dimensiunilor unui tensor
    • reduce_sum(A) sumează pe toate dimensiunile lui A
    • reduce_sum(A, i) sumează de-a lungul dimensiunii i
# Import operations from tensorflow
from tensorflow import ones, reduce_sum

# Define a 2x3x4 tensor of ones
A = ones([2, 3, 4])
Introducere în TensorFlow în Python

Suma pe dimensiunile tensorului

# Sum over all dimensions
B = reduce_sum(A)

# Sum over dimensions 0, 1, and 2
B0 = reduce_sum(A, 0)
B1 = reduce_sum(A, 1)
B2 = reduce_sum(A, 2)
Introducere în TensorFlow în Python

Să exersăm!

Introducere în TensorFlow în Python

Preparing Video For Download...