Untitled

From Chocolate Earthworm, 1 Year ago, written in Plain Text, viewed 199 times.
URL http://codebin.org/view/1c65631d Embed
Download Paste or View Raw
  1. import numpy as np
  2.  
  3. def func(x):
  4.     return (x[0] + x[1] - 1)**2 + (x[0] - x[1] - 2)**2
  5.  
  6. def gradient(x):
  7.     return np.array([4 * x[0] - 6, 4 * x[1] + 2])
  8.  
  9. def gradient_descent(initialization, step_size, iterations):
  10.     x = initialization + step_size * (-gradient(initialization))
  11.     for i in range(iterations):
  12.         x = x + step_size * (-gradient(x))
  13.     return x
  14.  
  15.  
  16. print(gradient_descent(np.array([0, 0]), 0.1, 5))
  17. print(gradient_descent(np.array([0, 0]), 0.1, 100))

Reply to "Untitled"

Here you can reply to the paste above