def backward_impl(self, inputs, outputs, prop_down, accum): # inputs: [inputs_fwd_graph] + [inputs_bwd_graph] or # [inputs_fwd_graph] + [outputs_fwd_graph] + [inputs_bwd_graph] axis = self.forward_func.info.args["axis"] # Inputs x0 = inputs[0].data dy = inputs[1].data # Outputs dx0 = outputs[0].data # Grads of inputs g_x0 = inputs[0].grad g_dy = inputs[1].grad # Grads of outputs g_dx0 = outputs[0].grad # Computation if prop_down[1]: maskp = F.greater_equal_scalar(x0, 0.0) maskn = maskp - 1.0 g_dy_p = maskp * g_dx0 g_dy_n = maskn * g_dx0 g_dy_ = F.concatenate(*[g_dy_p, g_dy_n], axis=axis) if accum[1]: g_dy.copy_from(g_dy + g_dy_) else: g_dy.copy_from(g_dy_)
def call(self, input): if self._drop_prob == 0: return input mask = F.rand(shape=(input.shape[0], 1, 1, 1)) mask = F.greater_equal_scalar(mask, self._drop_prob) out = F.mul_scalar(input, 1. / (1 - self._drop_prob)) out = F.mul2(out, mask) return out
def bisection(x0, x1, implicit_function, max_post_itr): for i in range(max_post_itr): xm = (x0 + x1) * 0.5 fm = implicit_function(xm) mp = F.greater_equal_scalar(fm, 0) mn = 1 - mp x0 = mp * xm + mn * x0 # f(x0) > 0 x1 = mn * xm + mp * x1 # f(x1) < 0 return x0, x1
def ray_march(self, camloc, raydir, t0, t1, N, n_chunks, t_argmin=False): # Points computation BR, _ = t0.shape t0 = F.reshape(t0, (BR, 1, 1)) t1 = F.reshape(t1, (BR, 1, 1)) camloc = F.reshape(camloc, (BR, 1, 3)) raydir = F.reshape(raydir, (BR, 1, 3)) step = (t1 - t0) / (N - 1) intervals = F.reshape(F.arange(0, N), (1, N, 1)) ts = t0 + step * intervals points = camloc + ts * raydir points = F.reshape(points, (BR * N, 3)) # SDF computation sdf_points = [] batch = (BR * N) // n_chunks for r in range(0, BR * N, batch): sdf_points.append(self.sdf(points[r:r + batch, :])) sdf_points = F.reshape(F.concatenate(*sdf_points, axis=0), (BR, N, 1)) if n_chunks != 1 else \ F.reshape(sdf_points[0], (BR, N, 1)) # t_argmin computation if t_argmin: idx_min = F.min(sdf_points, axis=1, keepdims=True, only_index=True) t_argmin = F.reshape(F.gather(ts, idx_min, axis=1, batch_dims=1), (BR, 1)) return t_argmin # Intersection check points = F.reshape(points, (BR, N, 3)) sdf_pos = F.greater_equal_scalar(sdf_points[:, :-1, :], 0) sdf_neg = F.less_equal_scalar(sdf_points[:, 1:, :], 0) mask_hit = sdf_pos * sdf_neg decreasing_consts = F.reshape(F.arange(N, 1, -1), (1, N - 1, 1)) vals = mask_hit * decreasing_consts idx_max = F.max(vals, axis=1, only_index=True) points = points[:, :-1, :] x_hit = F.gather(points, idx_max, axis=1, batch_dims=1) x_hit = F.reshape(x_hit, (BR, 3)) mask_hit = F.greater_scalar(F.sum(mask_hit, axis=1), 0) mask_hit = F.reshape(mask_hit, (BR, 1)) x_hit_rm0 = x_hit step = F.reshape(step, (BR, 1)) raydir = F.reshape(raydir, (BR, 3)) x_hit_rm1 = x_hit_rm0 + step * raydir return x_hit_rm0, x_hit_rm1, mask_hit
def hard_tanh_backward(inputs): """ Args: inputs (list of nn.Variable): Incomming grads/inputs to/of the forward function. kwargs (dict of arguments): Dictionary of the corresponding function arguments. Return: list of Variable: Return the gradients wrt inputs of the corresponding function. """ dy = inputs[0] x0 = inputs[1] m0 = F.greater_equal_scalar(x0, -1) m1 = F.less_equal_scalar(x0, 1) m01 = m0 * m1 m01 = no_grad(m01) dx0 = dy * m01 return dx0
def secant(x0, x1, implicit_function, max_post_itr, eps=1e-16): f0 = implicit_function(x0) # > 0 f1 = implicit_function(x1) # < 0 for i in range(max_post_itr): nu = f0 * (x1 - x0) de = f1 - f0 mask0 = F.greater_scalar(F.abs(de), eps) mask1 = 1 - mask0 nu = mask0 * nu + mask1 * 0 de = mask0 * de + mask1 * 1 xm = x0 - nu / de fm = implicit_function(xm) mp = F.greater_equal_scalar(fm, 0) mn = 1 - mp x0 = mp * xm + mn * x0 f0 = mp * fm + mn * f0 x1 = mn * xm + mp * x1 f1 = mn * fm + mp * f1 return x0, x1
def _focal_loss(pred, gt): '''Modified focal loss. Exactly the same as CornerNet. Modified for more stability by using log_sigmoid function Arguments: pred (batch x c x h x w): logit (must be values before sigmoid activation) gt_regr (batch x c x h x w) ''' alpha = 2 beta = 4 pos_inds = F.greater_equal_scalar(gt, 1) neg_inds = 1 - pos_inds neg_weights = F.pow_scalar(1.0 - gt, beta) prob_pred = F.sigmoid(pred) pos_loss = F.log_sigmoid(pred) * F.pow_scalar(1.0 - prob_pred, alpha) * pos_inds pos_loss = F.sum(pos_loss) neg_loss = F.log_sigmoid(-pred) * F.pow_scalar( prob_pred, alpha) * neg_weights * neg_inds neg_loss = F.sum(neg_loss) num_pos = F.maximum_scalar(F.sum(pos_inds), 1) loss = -(1 / num_pos) * (pos_loss + neg_loss) return loss
def projection(x: nn.NdArray, eps: float = 1e-5) -> nn.NdArray: norm = F.pow_scalar(F.sum(x**2, axis=1), val=0.5) return F.where(condition=F.greater_equal_scalar(norm, val=1.), x_true=F.clip_by_norm(x, clip_norm=1 - eps, axis=1), x_false=x)
def vae(x, shape_z, test=False): """ Function for calculate Elbo(evidence lowerbound) loss. This sample is a Bernoulli generator version. Args: x(`~nnabla.Variable`): N-D array shape_z(tuple of int): size of z test : True=train, False=test Returns: ~nnabla.Variable: Elbo loss """ ############################################# # Encoder of 2 fully connected layers # ############################################# # Normalize input xa = x / 256. batch_size = x.shape[0] # 2 fully connected layers, and Elu replaced from original Softplus. h = F.elu(PF.affine(xa, (500, ), name='fc1')) h = F.elu(PF.affine(h, (500, ), name='fc2')) # The outputs are the parameters of Gauss probability density. mu = PF.affine(h, shape_z, name='fc_mu') logvar = PF.affine(h, shape_z, name='fc_logvar') sigma = F.exp(0.5 * logvar) # The prior variable and the reparameterization trick if not test: # training with reparameterization trick epsilon = F.randn(mu=0, sigma=1, shape=(batch_size, ) + shape_z) z = mu + sigma * epsilon else: # test without randomness z = mu ############################################# # Decoder of 2 fully connected layers # ############################################# # 2 fully connected layers, and Elu replaced from original Softplus. h = F.elu(PF.affine(z, (500, ), name='fc3')) h = F.elu(PF.affine(h, (500, ), name='fc4')) # The outputs are the parameters of Bernoulli probabilities for each pixel. prob = PF.affine(h, (1, 28, 28), name='fc5') ############################################# # Elbo components and loss objective # ############################################# # Binarized input xb = F.greater_equal_scalar(xa, 0.5) # E_q(z|x)[log(q(z|x))] # without some constant terms that will canceled after summation of loss logqz = 0.5 * F.sum(1.0 + logvar, axis=1) # E_q(z|x)[log(p(z))] # without some constant terms that will canceled after summation of loss logpz = 0.5 * F.sum(mu * mu + sigma * sigma, axis=1) # E_q(z|x)[log(p(x|z))] logpx = F.sum(F.sigmoid_cross_entropy(prob, xb), axis=(1, 2, 3)) # Vae loss, the negative evidence lowerbound loss = F.mean(logpx + logpz - logqz) return loss