@Gaiussheh
2018-10-26T20:05:24.000000Z
字数 4991
阅读 475
Ronchi
We are trying to use a numerical method to find this, which requires aproximate value of at every point
Using discrete approximation, Regularizers can be replaced with
phi
is the above, the area is a 231*141
rectangle, therefore phi
is a 231*141
matrix
#Regx
def Regy(phi):
sum=0
phiplusone=phi[1:231,:]
phiplusone=np.insert(phiplusone,230,np.zeros(141),axis=0)
phimunusone=phi[0:230,:]
phimunusone=np.insert(phimunusone,0,np.zeros(141),axis=0)
result=(phimunusone-2*phi+phiplusone)**2
sum=np.sum(result)
return sum
#Regy
def Regx(phi):
sum=0
phiplusone=phi[:,1:141]
phiplusone=np.insert(phiplusone,140,np.zeros(231),axis=1)
phimunusone=phi[:,0:140]
phimunusone=np.insert(phimunusone,0,np.zeros(231),axis=1)
result=(phimunusone-2*phi+phiplusone)**2
sum=np.sum(result)
return sum
x
as the main variable of thefunction, therefore
#Total Cost
def TotalCost(x):
phi=np.zeros((231,141))
for i in range(0,231):
for j in range(0,141):
phi[i][j]=x[141*i+j] # put the x value into phi
sum=0
sum+=(Regx(phi)+Regy(phi))
return sum
def derivative(x)
phi=np.zeros((231,141))
for i in range(0,231):
for j in range(0,141):
phi[i][j]=x[141*i+j]
matrix=np.zeros((231,141)) # matrix containing derivatives to phi
result=np.zeros((231*141)) # final result
phiplusone=phi[:,1:141]
phiplusone=np.insert(phiplusone,140,np.zeros(231),axis=1)
phiplustwo=phiplusone[:,1:141]
phiplustwo=np.insert(phiplustwo,140,np.zeros(231),axis=1)
phimunusone=phi[:,0:140]
phimunusone=np.insert(phimunusone,0,np.zeros(231),axis=1)
phimunustwo=phimunusone[:,0:140]
phimunustwo=np.insert(phimunustwo,0,np.zeros(231),axis=1)
matrix+=(2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
#This is to calculate the direvities of the regulizer
phiplusone=phi[1:231,:]
phiplusone=np.insert(phiplusone,230,np.zeros(141),axis=0)
phiplustwo=phiplusone[1:231,:]
phiplustwo=np.insert(phiplustwo,230,np.zeros(141),axis=0)
phimunusone=phi[0:230,:]
phimunusone=np.insert(phimunusone,0,np.zeros(141),axis=0)
phimunustwo=phimunusone[0:230,:]
phimunustwo=np.insert(phimunustwo,0,np.zeros(141),axis=0)
matrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
#This is to calculate the direvities of the regulizer
for i in range(0,231):
for j in range(0,141):
result[141*i+j]=matrix[i][j]
return result
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
x=0*np.random.rand((231*141)) # the main variable of the total cost function, 231*141 indecates phi
x[426]=0.0001 # set one value tobe nonzero
x,min,info=scipy.optimize.fmin_l_bfgs_b(TotalCost,x,fprime=derivative,iprint=100) # bfgs method
phi=np.ones((231,141)) # regain phis
for i in range(0,231):
for j in range(0,141):
phi[i][j]=x[131*i+j]
plt.imshow(phi) # show the result
plt.colorbar()
plt.axis()
plt.show()
print(np.sqrt(np.sum(d**2)/(231*141)))
x0=np.random.rand((231*141+4*(end-start)))
diff=np.zeros((231,141))
difference=(derivative(x0))/(scipy.optimize.approx_fprime(x0,TotalCost,0.000001))-1
for i in range(0,231):
for j in range(0,141):
diff[i][j]=difference[141*i+j]
diff[diff>1]=1
diff[diff<-1]=1
plt.imshow(diff)
plt.colorbar()
plt.axis()
plt.show()