def test_norm2(self): shape = [3, 4] x_ = randn(shape) x = tf.constant(x_) with self.test_session(): self.assertAllClose( np.linalg.norm(x_.ravel())**2, util.norm2(x).eval())
def add(self, next_f): """Inform the solver of a new function value.""" next_f = _n.reshape(_n.array(next_f, copy=True), (self.n, 1)) # Update the Jacobian, if applicable if not self.first_step: self.__update_jacobian(self.next_x, next_f) # Check if improvement is found if (_util.norm2(next_f) < self.skip_treshold*_util.norm2(self.F) or self.first_step): self.x = self.next_x self.F = next_f self.last_reset = False F = next_f else: new_H = _n.identity(self.n) / self.initial_scale if self.last_reset: warnings.warn("%% Broyden reset II", BroydenWarning) # also last step failed... self.next_x = self.next_x - next_f self.F = next_f else: warnings.warn("%% Broyden reset", BroydenWarning) self.next_x = self.x - self.F self.last_reset = True self.H = new_H self.iteration += 1 return if self.first_step: self.first_norm = self.residual_norm() self.first_step = False step = _n.dot(self.H, F) factor = min(1., self.max_step*_util.norm2(self.x)/_util.norm2(step)) step *= factor self.next_x = self.x - step self.iteration += 1
def norm2_matrix(mat): for k in mat: mat[k] = norm2(mat[k]) return mat.to_sparse(0)
def normeuclid(vec1, vec2): n1 = norm2(vec1) n2 = norm2(vec2) return euclid(n1, n2)
def cosine(vec1, vec2): return norm2(vec1).dot(norm2(vec2))