Skip to content

Instantly share code, notes, and snippets.

@tsutarou10
Created September 3, 2017 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsutarou10/95e8e2b50b4912e217db87a3e19f200d to your computer and use it in GitHub Desktop.
Save tsutarou10/95e8e2b50b4912e217db87a3e19f200d to your computer and use it in GitHub Desktop.
勾配法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
def function(x):
return x ** 2 - 4 * x + 4
def diff(x): #数値微分
dx = 1e-11
return (function(x+dx) - function(x)) / dx
'''
initX : 初期値
learningRate : 学習率
'''
def gradient(initX,learningRate):
x = initX
dx = 1e-11
dist = np.array([])
iter = 0
while True:
dist = np.append(dist,x)
if -1 * dx < diff(x) < dx:
break
x = x - learningRate * diff(x)
iter += 1
return dist,iter
if __name__ == '__main__':
'''
x^2 - 4x + 4のグラフの描写
'''
x = np.arange(-4,8,0.1)
y = function(x)
dist,iter = gradient(5,0.1)
plt.plot(x,y,label = 'f(x) num of iter = ' + str(iter))
y = function(dist)
plt.plot(dist,y)
print 'min = ' + str(dist[len(dist) - 1]) #最小値を出力
plt.legend()
plt.show()
plt.savefig('gradient_method.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment