Operasi lanjutan

Pendahuluan TensorFlow di Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Ringkasan operasi lanjutan

  • Kita telah membahas operasi dasar di TensorFlow
    • add(), multiply(), matmul(), dan reduce_sum()
  • Di pelajaran ini, kita bahas operasi lanjutan
    • gradient(), reshape(), dan random()
Pendahuluan TensorFlow di Python

Ringkasan operasi lanjutan

Operation Use
gradient() Menghitung kemiringan fungsi pada suatu titik
reshape() Mengubah bentuk tensor (mis. 10x10 menjadi 100x1)
random() Mengisi tensor dengan nilai dari sebaran probabilitas
Pendahuluan TensorFlow di Python

Mencari titik optimum

  • Pada banyak masalah, kita ingin mencari optimum suatu fungsi.

    • Minimum: Nilai terendah dari fungsi loss.
    • Maksimum: Nilai tertinggi dari fungsi objektif.
  • Kita dapat melakukannya dengan gradient().

    • Optimum: Titik dengan gradien = 0.
    • Minimum: Perubahan gradien > 0
    • Maksimum: Perubahan gradien < 0
Pendahuluan TensorFlow di Python

Menghitung gradien

Slide menampilkan plot fungsi y sama dengan x.

Pendahuluan TensorFlow di Python

Menghitung gradien

Slide menampilkan plot fungsi y sama dengan x kuadrat.

Pendahuluan TensorFlow di Python

Gradien di TensorFlow

# Import tensorflow under the alias tf
import tensorflow as tf

# Define x
x = tf.Variable(-1.0)
# Define y within instance of GradientTape
with tf.GradientTape() as tape:
    tape.watch(x)
    y = tf.multiply(x, x)
# Evaluate the gradient of y at x = -1
g = tape.gradient(y, x)
print(g.numpy())
-2.0
Pendahuluan TensorFlow di Python

Gambar sebagai tensor

Slide menunjukkan bagaimana gambar dua kucing direpresentasikan sebagai tensor 2D, lalu diubah bentuknya menjadi vektor 1D.

Pendahuluan TensorFlow di Python

Cara reshape citra grayscale

# Import tensorflow as alias tf
import tensorflow as tf

# Generate grayscale image
gray = tf.random.uniform([2, 2], maxval=255, dtype='int32')

# Reshape grayscale image
gray = tf.reshape(gray, [2*2, 1])

Gambar menunjukkan citra grayscale 2×2 diubah bentuknya menjadi vektor 4×1.

Pendahuluan TensorFlow di Python

Cara reshape citra warna

# Import tensorflow as alias tf
import tensorflow as tf

# Generate color image
color = tf.random.uniform([2, 2, 3], maxval=255, dtype='int32')

# Reshape color image
color = tf.reshape(color, [2*2, 3])

Gambar menunjukkan citra warna 2×2×3 diubah menjadi matriks 4×3.

Pendahuluan TensorFlow di Python

Ayo berlatih!

Pendahuluan TensorFlow di Python

Preparing Video For Download...