def add(self, r_hat): xp = cuda.get_array_module(r_hat) weights = self.alpha**(xp.arange(r_hat.shape[0], 0, -1.0) - 1.0) summation = (1 - self.alpha) * F.sum(as_variable(weights) * r_hat, axis=0) # Compute exponential moving variance batch = (1 - self.alpha) * F.cumsum( as_variable(weights) * r_hat, axis=0) + (self.alpha**r_hat.shape[0]) * self.biased_mean batch = batch.data / (1 - self.alpha**( self.n + 1.0 + xp.arange(0.0, r_hat.shape[0], 1.0))) batch_s = xp.roll(batch, 1, axis=0) batch_s[0] = self.mean diff = r_hat - batch_s diff = self.alpha * (diff**2) var = (1 - self.alpha) * F.sum(as_variable(weights) * diff, axis=0) self.var = (self.alpha**r_hat.shape[0]) * self.var + var.data # Compute exponential moving average self.biased_mean = (self.alpha** r_hat.shape[0]) * self.biased_mean + summation.data self.n += r_hat.shape[0] self.bias_correction_factor *= self.alpha**r_hat.shape[0] self.mean = self.biased_mean / (1 - self.bias_correction_factor) return self.mean
def check_forward(self, x_data, axis): xp = backend.get_array_module(x_data) x = chainer.Variable(x_data) y = functions.cumsum(x, axis=axis) self.assertEqual(y.data.dtype, self.dtype) y_expect = xp.asarray(numpy.cumsum(self.x, axis=axis)) testing.assert_allclose(y_expect, y.data, **self.check_forward_options)
def check_forward(self, x_data, axis): xp = cuda.get_array_module(x_data) x = chainer.Variable(x_data) y = functions.cumsum(x, axis=axis) self.assertEqual(y.data.dtype, self.dtype) y_expect = xp.asarray(numpy.cumsum(self.x, axis=axis)) testing.assert_allclose(y_expect, y.data, **self.check_forward_options)
def observe_until_click(c): """ Generates a vector of observed documents (=1) and unobserved documents (=0) under the assumption that the user observed all documents up to and including the last clicked one. :param c: The click vectors :type c: chainer.Variable :return: The observation vectors :rtype: chainer.Variable """ return F.fliplr(F.clip(F.cumsum(F.fliplr(c), axis=1), 0.0, 1.0))
def report_scores(self, a, y, nr_docs, dtype, prefix=''): cv = self.click_model(a, y, nr_docs) cv = as_variable(cv.data.astype(dtype)) xp = cuda.get_array_module(a) """:type : numpy""" one_to_n = xp.arange(0.0, cv.shape[1], 1.0, dtype=cv.dtype) + 1.0 one_to_n = one_to_n.reshape((1, one_to_n.shape[0])) one_to_n = xp.broadcast_to(one_to_n, cv.shape) ctr = F.cumsum(cv, axis=1) / one_to_n for i in range(0, 10): index = min(i, ctr.shape[1] - 1) report({f"{prefix}ctr@{i + 1}": F.mean(ctr[:, index])}, self) report({f"{prefix}ctr": F.mean(ctr[:, -1])}, self)
def forward(self, novel_img_frustrum): frustrum_feats_depth = F.concat( [self.depth_coords, novel_img_frustrum], axis=1) occlusion_prep = self.occlusion(frustrum_feats_depth) # print(occlusion_prep.array) b, c, d, h, w = occlusion_prep.shape cumsum = F.concat([ self.xp.zeros((b, c, 1, h, w), "float32"), F.clip(F.cumsum(occlusion_prep, axis=2), 0, 1) ], axis=2) # print(cumsum[0, 0, :, 0, 0]) frustum_weights = cumsum[:, :, 1:] - cumsum[:, :, :-1] depth_map = F.sum(self.depth_coords * frustum_weights, axis=2) # -0.5 ~ 0.5 # print(depth_map.array.mean()) return frustum_weights, depth_map
def check_type_error(self, x): with self.assertRaises(type_check.InvalidType): functions.cumsum(x, self.axis)
def f(x): y = functions.cumsum(x, axis) return y * y
def check_backward(self, x_data, axis, y_grad): gradient_check.check_backward( lambda x: functions.cumsum(x, axis), x_data, y_grad, dtype=numpy.float64, **self.check_backward_options)
def forward(self, inputs, device): x, = inputs return functions.cumsum(x, axis=self.axis),
def test_invalid_type_axis(self): with self.assertRaises(TypeError): functions.cumsum(self.x, [0]) with self.assertRaises(TypeError): functions.cumsum(self.x, (0, ))
def f(x): return functions.cumsum(x, axis)
def test_invalid_type_axis(self): with self.assertRaises(TypeError): functions.cumsum(self.x, [0]) with self.assertRaises(TypeError): functions.cumsum(self.x, (0,))