def test_nested_select(self): def beales(x, y): e1 = 1.5 - x + x * y e2 = 2.25 - x + x * (y**2) e3 = 2.625 - x + x * (y**3) return {'e1': e1, 'e2': e2, 'e3': e3} x1 = ch.zeros(10) y1 = ch.zeros(10) # With a single select this worked minimize(beales(x1, y1), x0=[x1[1:4], y1], method='dogleg', options={'disp': False}) x2 = ch.zeros(10) y2 = ch.zeros(10) # But this used to raise `AttributeError: 'Select' object has no attribute 'x'` minimize(beales(x2, y2), x0=[x2[1:8][:3], y2], method='dogleg', options={'disp': False}) np.testing.assert_array_equal(x1, x2) np.testing.assert_array_equal(y1, y2)
def test_nested_select(self): def beales(x, y): e1 = 1.5 - x + x*y e2 = 2.25 - x + x*(y**2) e3 = 2.625 - x + x*(y**3) return {'e1': e1, 'e2': e2, 'e3': e3} x1 = ch.zeros(10) y1 = ch.zeros(10) # With a single select this worked minimize(beales(x1, y1), x0=[x1[1:4], y1], method='dogleg', options={'disp': False}) x2 = ch.zeros(10) y2 = ch.zeros(10) # But this used to raise `AttributeError: 'Select' object has no attribute 'x'` minimize(beales(x2, y2), x0=[x2[1:8][:3], y2], method='dogleg', options={'disp': False}) np.testing.assert_array_equal(x1, x2) np.testing.assert_array_equal(y1, y2)
def moment(a, moment=1, axis=0): if moment == 1: # By definition the first moment about the mean is 0. shape = list(a.shape) del shape[axis] if shape: # return an actual array of the appropriate shape return ch.zeros(shape, dtype=float) else: # the input was 1D, so return a scalar instead of a rank-0 array return np.float64(0.0) else: mn = ch.expand_dims(a.mean(axis=axis), axis) s = ch.power((a - mn), moment) return s.mean(axis=axis)
def moment(a, moment=1, axis=0): if moment == 1: # By definition the first moment about the mean is 0. shape = list(a.shape) del shape[axis] if shape: # return an actual array of the appropriate shape return ch.zeros(shape, dtype=float) else: # the input was 1D, so return a scalar instead of a rank-0 array return np.float64(0.0) else: mn = ch.expand_dims(a.mean(axis=axis), axis) s = ch.power((a-mn), moment) return s.mean(axis=axis)
def test_sum_and_mean(self): for fn in [ch.sum, ch.mean]: data = ch.zeros((3,4,7,2)) dsum = fn(data, axis=2) dr = dsum.dr_wrt(data) diff = ch.random.randn(data.size).reshape(data.shape) pred = dr.dot(diff.r.ravel()) gt = fn(diff, axis=2) #print pred #print gt #print pred.ravel() - gt.r.ravel() self.assertTrue(1e-15 > np.max(np.abs(gt.r.ravel() - pred))) # test caching dr0 = gt.dr_wrt(diff) diff[:] = np.random.randn(diff.size).reshape(diff.shape) self.assertTrue(gt.dr_wrt(diff) is dr0) # changing values shouldn't force recompute gt.axis=1 self.assertTrue(gt.dr_wrt(diff) is not dr0)