Gradient descent

Introduction to Deep Learning in Python

Dan Becker

Data Scientist and contributor to Keras and TensorFlow libraries

Gradient descent

ch2_2.003.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.004.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.005.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.006.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.008.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.009.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.010.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.011.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.012.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.013.png

Introduction to Deep Learning in Python

Gradient descent

ch2_2.014.png

Introduction to Deep Learning in Python

Gradient descent

  • ถ้า slope เป็นบวก:
    • การเคลื่อนที่ตรงข้าม slope หมายถึงเลื่อนไปทางค่าที่น้อยลง
    • ลบ slope ออกจากค่าปัจจุบัน
    • ก้าวที่ใหญ่เกินไปอาจทำให้เส้นทางคลาดเคลื่อนได้
  • วิธีแก้: learning rate
    • อัปเดตแต่ละน้ำหนักโดยลบด้วย learning rate * slope
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.022.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.028.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.029.png

  • Slope ของฟังก์ชัน mean-squared loss เทียบกับค่าที่พยากรณ์:
    • 2 (ค่าที่พยากรณ์ - ค่าจริง) = 2 Error
    • 2 * -4
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.033.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.035.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.037.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.038.png

  • การคำนวณ slope ของน้ำหนัก ต้องคูณค่าต่อไปนี้:
    • Slope ของฟังก์ชัน loss เทียบกับค่าที่โหนดที่รับข้อมูลเข้า
    • ค่าของโหนดที่ป้อนเข้าสู่น้ำหนักนั้น
    • Slope ของฟังก์ชัน activation เทียบกับค่าที่ป้อนเข้า
Introduction to Deep Learning in Python

ตัวอย่างการคำนวณ slope

ch2_2.044.png

  • 2 * -4 * 3
  • -24
  • ถ้า learning rate เท่ากับ 0.01 น้ำหนักใหม่จะเป็น
  • 2 - 0.01(-24) = 2.24
Introduction to Deep Learning in Python

เครือข่ายที่มีสองอินพุตส่งผลต่อการพยากรณ์

ch2_2.045.png

Introduction to Deep Learning in Python

โค้ดสำหรับคำนวณ slope และอัปเดตน้ำหนัก

import numpy as np
weights = np.array([1, 2])
input_data = np.array([3, 4])
target = 6
learning_rate = 0.01
preds = (weights * input_data).sum()
error = preds - target

print(error)
5
Introduction to Deep Learning in Python

โค้ดสำหรับคำนวณ slope และอัปเดตน้ำหนัก

gradient = 2 * input_data * error

gradient
array([30, 40])
weights_updated = weights - learning_rate * gradient
preds_updated = (weights_updated * input_data).sum()
error_updated = preds_updated - target

print(error_updated)
2.5
Introduction to Deep Learning in Python

มาฝึกกันเถอะ!

Introduction to Deep Learning in Python

Preparing Video For Download...