Skip to content

Commit

Permalink
Automate the epsilon picking based on percentile of model values for …
Browse files Browse the repository at this point in the history
…DEFAULT mode. Fix example.

Fix bug with Maps using array of values
  • Loading branch information
fourndo committed Jun 6, 2016
1 parent 382b31b commit ef12a36
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
39 changes: 26 additions & 13 deletions SimPEG/Directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ def endIter(self):
class Update_IRLS(InversionDirective):

eps_min = None
eps_p = None
eps_q = None
eps = None
norms = [2.,2.,2.,2.]
factor = None
gamma = None
Expand All @@ -263,6 +262,7 @@ class Update_IRLS(InversionDirective):
f_old = None
f_min_change = 1e-2
beta_tol = 5e-2
prctile = 95

# Solving parameter for IRLS (mode:2)
IRLSiter = 0
Expand Down Expand Up @@ -297,9 +297,22 @@ def endIter(self):
print "Convergence with smooth l2-norm regularization: Start IRLS steps..."

self.mode = 2
print self.eps_p, self.eps_q, self.norms
self.reg.eps_p = self.eps_p
self.reg.eps_q = self.eps_q

# Either use the supplied epsilon, or fix base on distribution of
# model values
if getattr(self, 'reg.eps', None) is None:
self.reg.eps_p = np.percentile(np.abs(self.invProb.curModel),self.prctile)
else:
self.reg.eps_p = self.eps[0]

if getattr(self, 'reg.eps', None) is None:
self.reg.eps_q = np.percentile(np.abs(self.reg.regmesh.cellDiffxStencil*(self.reg.mapping * self.invProb.curModel)),self.prctile)
else:
self.reg.eps_q = self.eps[1]

print "L[p qx qy qz]-norm : " + str(self.reg.norms)
print "eps_p: " + str(self.reg.eps_p) + " eps_q: " + str(self.reg.eps_q)

self.reg.norms = self.norms
self.coolingFactor = 1.
self.coolingRate = 1
Expand Down Expand Up @@ -343,14 +356,14 @@ def endIter(self):
else:
self.f_old = phim_new

# Cool the threshold parameter if required
if getattr(self, 'factor', None) is not None:
eps = self.reg.eps / self.factor

if getattr(self, 'eps_min', None) is not None:
self.reg.eps = np.max([self.eps_min,eps])
else:
self.reg.eps = eps
# # Cool the threshold parameter if required
# if getattr(self, 'factor', None) is not None:
# eps = self.reg.eps / self.factor
#
# if getattr(self, 'eps_min', None) is not None:
# self.reg.eps = np.max([self.eps_min,eps])
# else:
# self.reg.eps = eps

# Get phi_m at the end of current iteration
self.phi_m_last = self.invProb.phi_m_last
Expand Down
36 changes: 7 additions & 29 deletions SimPEG/Examples/Inversion_IRLS.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,33 @@ def run(N=100, plotIt=True):
survey = Survey.LinearSurvey()
survey.pair(prob)
survey.dobs = prob.fields(mtrue) + std_noise * np.random.randn(nk)
#survey.makeSyntheticData(mtrue, std=std_noise)

wd = np.ones(nk) * std_noise

#print survey.std[0]
#M = prob.mesh
# Distance weighting
wr = np.sum(prob.G**2.,axis=0)**0.5
wr = ( wr/np.max(wr) )

# reg = Regularization.Simple(mesh)
# reg.mref = mref
# reg.cell_weights = wr
#
dmis = DataMisfit.l2_DataMisfit(survey)
dmis.Wd = 1./wd
#
# opt = Optimization.ProjectedGNCG(maxIter=20,lower=-2.,upper=2., maxIterCG= 10, tolCG = 1e-4)
# invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
# invProb.curModel = m0
#
# beta = Directives.BetaSchedule(coolingFactor=2, coolingRate=1)
# target = Directives.TargetMisfit()
#

betaest = Directives.BetaEstimate_ByEig()
# inv = Inversion.BaseInversion(invProb, directiveList=[beta, betaest, target])
#
#
# mrec = inv.run(m0)
# ml2 = mrec
# print "Final misfit:" + str(invProb.dmisfit.eval(mrec))
#
# # Switch regularization to sparse
# phim = invProb.phi_m_last
# phid = invProb.phi_d

reg = Regularization.Sparse(mesh)
reg.mref = mref
reg.cell_weights = wr

reg.mref = np.zeros(mesh.nC)
eps_p = 5e-2
eps_q = 5e-2
norms = [0., 0., 2., 2.]


opt = Optimization.ProjectedGNCG(maxIter=100 ,lower=-2.,upper=2., maxIterLS = 20, maxIterCG= 10, tolCG = 1e-3)
invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
update_Jacobi = Directives.Update_lin_PreCond()
IRLS = Directives.Update_IRLS( norms=norms, eps_p=eps_p, eps_q=eps_q)

# Set the IRLS directive, penalize the lowest 25 percentile of model values
# Start with an l2-l2, then switch to lp-norms
norms = [0., 0., 2., 2.]
IRLS = Directives.Update_IRLS( norms=norms, prctile = 25, maxIRLSiter = 15, minGNiter=3)

inv = Inversion.BaseInversion(invProb, directiveList=[IRLS,betaest,update_Jacobi])

Expand Down
4 changes: 3 additions & 1 deletion SimPEG/Maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ def __init__(self, mesh, indActive, valInactive, nC=None):
if Utils.isScalar(valInactive):
self.valInactive = np.ones(self.nC)*float(valInactive)
else:
self.valInactive = valInactive.copy()
self.valInactive = np.ones(self.nC)
self.valInactive[self.indInactive] = valInactive.copy()

self.valInactive[self.indActive] = 0

inds = np.nonzero(self.indActive)[0]
Expand Down

0 comments on commit ef12a36

Please sign in to comment.