Exemplo n.º 1
0
 def evaluate(self, opt, grads, infos, objVar):
     """
   Approximates gradient based on evaluated points.
   @ In, opt, dict, current opt point (normalized)
   @ In, grads, list(dict), evaluated neighbor points
   @ In, infos, list(dict), info about evaluated neighbor points
   @ In, objVar, string, objective variable
   @ Out, magnitude, float, magnitude of gradient
   @ Out, direction, dict, versor (unit vector) for gradient direction
   @ Out, foundInf, bool, if True then infinity calculations were used
 """
     gradient = {}
     for g, pt in enumerate(grads):
         info = infos[g]
         delta = info['delta']
         activeVar = info['optVar']
         lossDiff = np.atleast_1d(
             mathUtils.diffWithInfinites(pt[objVar], opt[objVar]))
         grad = lossDiff / delta
         gradient[activeVar] = grad
     # obtain the magnitude and versor of the gradient to return
     magnitude, direction, foundInf = mathUtils.calculateMagnitudeAndVersor(
         list(gradient.values()))
     direction = dict((var, float(direction[v]))
                      for v, var in enumerate(gradient.keys()))
     return magnitude, direction, foundInf
Exemplo n.º 2
0
Arquivo: SPSA.py Projeto: HCBINL/raven
 def evaluate(self, opt, grads, infos, objVar):
   """
     Approximates gradient based on evaluated points.
     @ In, opt, dict, current opt point (normalized)
     @ In, grads, list(dict), evaluated neighbor points
     @ In, infos, list(dict), info about evaluated neighbor points
     @ In, objVar, string, objective variable
     @ Out, magnitude, float, magnitude of gradient
     @ Out, direction, dict, versor (unit vector) for gradient direction
   """
   gradient = {}
   lossDiff = np.atleast_1d(mathUtils.diffWithInfinites(grads[0][objVar], opt[objVar]))
   for var in self._optVars:
     # don't assume delta is unchanged; calculate it here
     delta = grads[0][var] - opt[var]
     gradient[var] = lossDiff / delta
   magnitude, direction, foundInf = mathUtils.calculateMagnitudeAndVersor(list(gradient.values()))
   direction = dict((var, float(direction[v])) for v, var in enumerate(gradient.keys()))
   return magnitude, direction, foundInf