Python implements the steepest descent method

The examples in this article share the specific code of python to achieve the fastest descent method for your reference. The specific content is as follows

Code:

from sympy import*import numpy as np
def backtracking_line_search(f,df,x,x_k,p_k,alpha0):
 rho=0.5
 c=10**-4
 alpha=alpha0
 replacements1=zip(x,x_k)
 replacements2=zip(x,x_k+alpha*p_k)
 f_k=f.subs(replacements1)
 df_p=np.dot([df_.subs(replacements1)for df_ in df],p_k)while f.subs(replacements2) f_k+c*alpha*df_p:
 alpha=rho*alpha
 replacements2 =zip(x, x_k +alpha * p_k)return alpha
def stepest_line_search(f,x,x0,alpha0):
 df =[diff(f, x_)for x_ in x]
 x_k=x0
 alpha=alpha0
 replacements=zip(x,x_k)
 len_df =sqrt(np.sum([df_.subs(replacements)**2for df_ in df]))while len_df 1e-6:
 p_k=-1*np.array([df_.subs(replacements)for df_ in df])
 alpha =backtracking_line_search(f, df, x, x_k, p_k, alpha)
 x_k=x_k+alpha*p_k
 replacements =zip(x, x_k)
 len_df=np.sum([df_.subs(replacements)**2for df_ in df])return x_k
if __name__=="__main__":init_printing(use_unicode=True)
 x1 =symbols("x1")
 x2 =symbols("x2")
 x = np.array([x1, x2])
 f =100*(x2 - x1 **2)**2+(1- x1)**2
 ans=stepest_line_search(f, x, np.array([1.2,1]),1)
 print "the minimal value in point:",ans

analysis:

This uses backtracking line search to find alpha.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements the steepest descent method
Python implements gradient descent method
Python implements the brick-and-mortar game
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements the actual banking system
How Python implements the timer function
Python implements the aircraft war project
End the method of running python
Python implements the sum of fractional sequences
The specific method of python instantiation object
The specific method of python import library
python3 simply implements the combined design pattern
2.1 The Python Interpreter (python interpreter)
Python implements the shuffling of the cards in Doudizhu
Python implements the source code of the snake game
Python magic method topic
Python implements Super Mario
Python implements tic-tac-toe game
Python3.7 debugging example method
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python function-dictionary get() method
Python implements minesweeper game
Python implements scanning tools
Consolidate the Python foundation (2)
Python error handling method
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python defines a function method
Consolidate the foundation of Python (4)
Python implements simple tank battle
Consolidate the foundation of Python(7)
Python implements udp chat window
Python TCP packet injection method
python guess the word game
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Ubuntu install the latest Python 3.
Python drawing ring graph method
Python implements parking management system
Python implements digital bomb game
Python tracking except information method
Python implements TCP file transfer
Python realizes the guessing game
The difference between Python extensions
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Consolidate the foundation of Python(5)
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function