[关闭]
@Gaiussheh 2018-10-26T20:05:24.000000Z 字数 4991 阅读 475

Minimizing the Regularizer

Ronchi


BACKGROUND

CODES

  1. #Regx
  2. def Regy(phi):
  3. sum=0
  4. phiplusone=phi[1:231,:]
  5. phiplusone=np.insert(phiplusone,230,np.zeros(141),axis=0)
  6. phimunusone=phi[0:230,:]
  7. phimunusone=np.insert(phimunusone,0,np.zeros(141),axis=0)
  8. result=(phimunusone-2*phi+phiplusone)**2
  9. sum=np.sum(result)
  10. return sum
  11. #Regy
  12. def Regx(phi):
  13. sum=0
  14. phiplusone=phi[:,1:141]
  15. phiplusone=np.insert(phiplusone,140,np.zeros(231),axis=1)
  16. phimunusone=phi[:,0:140]
  17. phimunusone=np.insert(phimunusone,0,np.zeros(231),axis=1)
  18. result=(phimunusone-2*phi+phiplusone)**2
  19. sum=np.sum(result)
  20. return sum
  1. #Total Cost
  2. def TotalCost(x):
  3. phi=np.zeros((231,141))
  4. for i in range(0,231):
  5. for j in range(0,141):
  6. phi[i][j]=x[141*i+j] # put the x value into phi
  7. sum=0
  8. sum+=(Regx(phi)+Regy(phi))
  9. return sum
  1. def derivative(x)
  2. phi=np.zeros((231,141))
  3. for i in range(0,231):
  4. for j in range(0,141):
  5. phi[i][j]=x[141*i+j]
  6. matrix=np.zeros((231,141)) # matrix containing derivatives to phi
  7. result=np.zeros((231*141)) # final result
  8. phiplusone=phi[:,1:141]
  9. phiplusone=np.insert(phiplusone,140,np.zeros(231),axis=1)
  10. phiplustwo=phiplusone[:,1:141]
  11. phiplustwo=np.insert(phiplustwo,140,np.zeros(231),axis=1)
  12. phimunusone=phi[:,0:140]
  13. phimunusone=np.insert(phimunusone,0,np.zeros(231),axis=1)
  14. phimunustwo=phimunusone[:,0:140]
  15. phimunustwo=np.insert(phimunustwo,0,np.zeros(231),axis=1)
  16. matrix+=(2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
  17. #This is to calculate the direvities of the regulizer
  18. phiplusone=phi[1:231,:]
  19. phiplusone=np.insert(phiplusone,230,np.zeros(141),axis=0)
  20. phiplustwo=phiplusone[1:231,:]
  21. phiplustwo=np.insert(phiplustwo,230,np.zeros(141),axis=0)
  22. phimunusone=phi[0:230,:]
  23. phimunusone=np.insert(phimunusone,0,np.zeros(141),axis=0)
  24. phimunustwo=phimunusone[0:230,:]
  25. phimunustwo=np.insert(phimunustwo,0,np.zeros(141),axis=0)
  26. matrix+=2*(phi-2*phiplusone+phiplustwo)-4*(phimunusone-2*phi+phiplusone)+2*(phimunustwo-2*phimunusone+phi)
  27. #This is to calculate the direvities of the regulizer
  28. for i in range(0,231):
  29. for j in range(0,141):
  30. result[141*i+j]=matrix[i][j]
  31. return result
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import optimize
  4. x=0*np.random.rand((231*141)) # the main variable of the total cost function, 231*141 indecates phi
  5. x[426]=0.0001 # set one value tobe nonzero
  6. x,min,info=scipy.optimize.fmin_l_bfgs_b(TotalCost,x,fprime=derivative,iprint=100) # bfgs method
  7. phi=np.ones((231,141)) # regain phis
  8. for i in range(0,231):
  9. for j in range(0,141):
  10. phi[i][j]=x[131*i+j]
  11. plt.imshow(phi) # show the result
  12. plt.colorbar()
  13. plt.axis()
  14. plt.show()
  15. print(np.sqrt(np.sum(d**2)/(231*141)))
  1. x0=np.random.rand((231*141+4*(end-start)))
  2. diff=np.zeros((231,141))
  3. difference=(derivative(x0))/(scipy.optimize.approx_fprime(x0,TotalCost,0.000001))-1
  4. for i in range(0,231):
  5. for j in range(0,141):
  6. diff[i][j]=difference[141*i+j]
  7. diff[diff>1]=1
  8. diff[diff<-1]=1
  9. plt.imshow(diff)
  10. plt.colorbar()
  11. plt.axis()
  12. plt.show()

PROBLEM:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注