Advertisement
pqnysekackaya

Untitled

May 20th, 2022
942
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. def func(x):
  4.  
  5.     return (x[0] + x[1] - 1)**2 + (x[0] - x[1] - 2)**2
  6.  
  7. def gradient(x):
  8.  
  9.     return np.array([4 * x[0] - 6, 4 * x[1] + 2])
  10.  
  11. def gradient_descent(initialization, step_size, iterations):
  12.  
  13.     x = initialization
  14.     for i in range(iterations):
  15.         x = x[0] + step_size*gradient(x)
  16.     return x
  17.  
  18. print(gradient_descent(np.array([0, 0]), 0.1, 5))
  19. print(gradient_descent(np.array([0, 0]), 0.1, 100))
Advertisement
RAW Paste Data Copied
Advertisement