def __call__(self, x): """ Parameters ---------- x : chainer.Variable shape(batch_size, channel, x_dim, y_dim) """ batch_size, _, x_dim, y_dim = x.shape xp = self.xp xx_channel = xp.tile(xp.arange(x_dim), (1, y_dim, 1)) yy_channel = xp.tile(xp.arange(y_dim), (1, x_dim, 1)).transpose(0, 2, 1) xx_channel = xp.array(xx_channel, 'f') / (x_dim - 1) yy_channel = xp.array(yy_channel, 'f') / (y_dim - 1) xx_channel = xx_channel * 2 - 1 yy_channel = yy_channel * 2 - 1 xx_channel = xp.tile(xx_channel, (batch_size, 1, 1, 1)).transpose(0, 1, 3, 2) yy_channel = xp.tile(yy_channel, (batch_size, 1, 1, 1)).transpose(0, 1, 3, 2) ret = F.concat([x, xx_channel, yy_channel], axis=1) if self.with_r: rr = F.sqrt( F.square(xx_channel - 0.5) + F.square(yy_channel - 0.5)) ret = F.concat([ret, rr], axis=1) return ret
def decov_loss(tensor, xp=None, axis=1): """ Decov loss as described in https://arxiv.org/pdf/1511.06068.pdf 'Reducing Overfitting In Deep Networks by Decorrelating Representation'. This version implements the loss in the variable format. ARGS: axis: (int, optional) If the tensor is 4-dim, it is reshaped into 2-dim; axis is the first dimension. """ if xp is None: # # get xp module if not provided. xp = chainer.cuda.get_array_module(tensor.data) if tensor.ndim == 4: # # reshape to a 2D matrix. matr = F.reshape(tensor, (tensor.shape[axis], -1)) elif tensor.ndim == 2: matr = tensor # # subtract the mean. centered = F.bias(matr, -F.mean(matr)) # # compute the covariance. cov = F.matmul(centered, F.transpose(centered)) # # compute the frombenius norm. frob_norm = F.sum(F.square(cov)) # # get the norm of diagonal elements. # # in chainer 5.x this should work. # corr_diag_sqr = F.sum(F.square(F.diagonal(cov1))) corr_diag_sqr = F.sum(F.square(cov * xp.eye(cov.shape[0], dtype=cov.dtype))) loss = 0.5 * (frob_norm - corr_diag_sqr) return loss
def test_backward_case1(self): vertices = [[-0.9, -0.9, 2.], [-0.8, 0.8, 1.], [0.8, 0.8, 0.5]] faces = [[0, 1, 2]] renderer = neural_renderer.Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.camera_mode = 'none' vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') vertices, faces = utils.to_minibatch((vertices, faces)) vertices = chainer.Variable(vertices) images = renderer.render_depth(vertices, faces) loss = cf.sum(cf.square(images[0, 15, 20] - 1)) loss.backward() grad = vertices.grad.get() grad2 = np.zeros_like(grad) for i in range(3): for j in range(3): eps = 1e-3 vertices2 = vertices.data.copy() vertices2[i, j] += eps images = renderer.render_depth(vertices2, faces) loss2 = cf.sum(cf.square(images[0, 15, 20] - 1)) grad2[i, j] = ((loss2 - loss) / eps).data.get() chainer.testing.assert_allclose(grad, grad2, atol=1e-3)
def __call__(self): self.renderer.viewing_angle = 30 self.renderer.eye = neural_renderer.get_points_from_angles(2.732, 0, 0) front_image = self.renderer.render_silhouettes(self.vertices, self.faces) loss = cf.sum(cf.square(front_image - self.front_image_ref[None, :, :])) if self.right_image_ref is not None: self.renderer.eye = neural_renderer.get_points_from_angles( 2.732, 0, 90) right_image = self.renderer.render_silhouettes( self.vertices, self.faces) loss = loss + cf.sum( cf.square(right_image - self.right_image_ref[None, :, :])) if self.left_image_ref is not None: self.renderer.eye = neural_renderer.get_points_from_angles( 2.732, 0, 270) left_image = self.renderer.render_silhouettes( self.vertices, self.faces) loss = loss + cf.sum( cf.square(left_image - self.left_image_ref[None, :, :])) if self.top_image_ref is not None: # FIXME: if we set elevetion to 90, it renders nothing... self.renderer.eye = neural_renderer.get_points_from_angles( 2.732, 89.9, 0) top_image = self.renderer.render_silhouettes( self.vertices, self.faces) loss = loss + cf.sum( cf.square(top_image - self.top_image_ref[None, :, :])) self.count = self.count + 1 return loss
def test_backward_case1(self): vertices = [ [-0.9, -0.9, 2.], [-0.8, 0.8, 1.], [0.8, 0.8, 0.5]] faces = [[0, 1, 2]] renderer = neural_renderer.Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.camera_mode = 'none' vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') vertices, faces = utils.to_minibatch((vertices, faces)) vertices = chainer.Variable(vertices) images = renderer.render_depth(vertices, faces) loss = cf.sum(cf.square(images[0, 15, 20] - 1)) loss.backward() grad = vertices.grad.get() grad2 = np.zeros_like(grad) for i in range(3): for j in range(3): eps = 1e-3 vertices2 = vertices.data.copy() vertices2[i, j] += eps images = renderer.render_depth(vertices2, faces) loss2 = cf.sum(cf.square(images[0, 15, 20] - 1)) grad2[i, j] = ((loss2 - loss) / eps).data.get() chainer.testing.assert_allclose(grad, grad2, atol=1e-3)
def _do_one(inputs): t, qvs, nqvs, result, mask, hopeful, terminate = inputs if terminate or mask != 1: return None, 0, 0, 0, 0, 0, 0, True action = int(F.argmax(qvs).data) if action == 1: terminate = True reward = 0 if action == 1: if result == 1: reward = self.r_correct else: reward = self.r_rush if hopeful else self.r_wrong elif t == length - 1 and hopeful: reward = self.r_late qv = F.max(qvalues) nqv = F.max(nqvs).data if action == 1: loss = F.square(reward - qv) else: loss = F.square(reward + 0.5 * nqv - qv) r = 0 if action == 1: r = 10 if result == 1 else -5 r_hope = r if hopeful else 0 correct = int(action and result == 1) rush = 1 if (result != 1 and action and hopeful) else 0 late = 1 if (t == length - 1 and not action and hopeful) else 0 return loss, r, r_hope, action, correct, rush, late, terminate
def get_bbox_side_lengths(self, grids): x0, x1, x2, y0, y1, y2 = self.get_corners(grids) width = F.sqrt(F.square(x1 - x0) + F.square(y1 - y0)) height = F.sqrt(F.square(x2 - x0) + F.square(y2 - y0)) return width, height
def gaussian_kl(params0, params1): (mean0, logstd0), (mean1, logstd1) = params0, params1 assert mean0.shape == logstd0.shape == mean1.shape == logstd1.shape return F.sum(logstd1 - logstd0 + (F.square(F.exp(logstd0)) + F.square(mean0 - mean1)) / (2.0 * F.square(F.exp(logstd1))) - 0.5, axis=1)
def _calc_ssim(img1, img2, window, window_size, channel, data_range, size_average=True): mu1 = gaussian_filter(img1, window, pad=window_size // 2, channel=channel) mu2 = gaussian_filter(img2, window, pad=window_size // 2, channel=channel) mu1_sq = F.square(mu1) mu2_sq = F.square(mu2) mu1_mu2 = mu1 * mu2 sigma1_sq = gaussian_filter( img1 * img1, window, pad=window_size // 2, channel=channel) - mu1_sq sigma2_sq = gaussian_filter( img2 * img2, window, pad=window_size // 2, channel=channel) - mu2_sq sigma12 = gaussian_filter( img1 * img2, window, pad=window_size // 2, channel=channel) - mu1_mu2 C1 = (0.01 * data_range)**2 C2 = (0.03 * data_range)**2 ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)) if size_average: return F.mean(ssim_map) return NotImplementedError()
def _do_both(inputs): t, qvs, nqvs, result, mask, hopeful, terminate = inputs if terminate or mask != 1: return None, 0, 0, 0, 0, 0, 0, True action = int(F.argmax(qvs).data) # if action == 1: # terminate = True reward = [0, 0] if result == 1: reward[1] = self.r_correct else: reward[1] = self.r_rush if hopeful else self.r_wrong if t == length - 1 and hopeful: reward[0] = self.r_late nqv = F.max(nqvs).data loss = F.square(reward[0] + 0.3 * nqv - qvs[0]) loss += F.square(reward[1] - qvs[1]) r = 0 if action == 1: r = 10 if result == 1 else -5 r_hope = r if hopeful else 0 correct = int(action and result == 1) rush = 1 if (result != 1 and action and hopeful) else 0 late = 1 if (t == length - 1 and not action and hopeful) else 0 return loss, r, r_hope, action, correct, rush, late, terminate
def _lossfun(self, entropy, vs_pred, log_probs, vs_pred_old, log_probs_old, advs, vs_teacher): prob_ratio = F.exp(log_probs - log_probs_old) loss_policy = -F.mean( F.minimum( prob_ratio * advs, F.clip(prob_ratio, 1 - self.clip_eps, 1 + self.clip_eps) * advs)) if self.clip_eps_vf is None: loss_value_func = F.mean_squared_error(vs_pred, vs_teacher) else: loss_value_func = F.mean( F.maximum( F.square(vs_pred - vs_teacher), F.square( _elementwise_clip(vs_pred, vs_pred_old - self.clip_eps_vf, vs_pred_old + self.clip_eps_vf) - vs_teacher))) loss_entropy = -F.mean(entropy) self.value_loss_record.append(float(loss_value_func.array)) self.policy_loss_record.append(float(loss_policy.array)) loss = (loss_policy + self.value_func_coef * loss_value_func + self.entropy_coef * loss_entropy) return loss
def __fit_one(self, link, content4_2, style3_2,style4_2): xp = self.xp link.zerograds() layer3_2,layer4_2 = self.model(link.x) if self.keep_color: #trans_layers = self.model(util.gray(link.x)) print "don't keep color!" loss_info = [] loss = Variable(xp.zeros((), dtype=np.float32)) #layer = layers[name] content_loss = self.content_weight * F.mean_squared_error(layer4_2, Variable(content4_2)) loss_info.append(('content_', float(content_loss.data))) loss += content_loss style_patch, style_patch_norm = style3_2 near,size,size2 = util.nearest_neighbor_patch(layer3_2, style_patch, style_patch_norm) style_loss = self.style_weight * (F.sum(F.square(layer3_2))*size2/size-2*F.sum(near)/size) loss_info.append(('style_', float(style_loss.data))) loss+=style_loss style_patch, style_patch_norm = style4_2 near,size,size2 = util.nearest_neighbor_patch(layer4_2, style_patch, style_patch_norm) style_loss = self.style_weight *1.5* (F.sum(F.square(layer4_2))*size2/size-2*F.sum(near)/size) loss_info.append(('style_', float(style_loss.data))) loss+= style_loss tv_loss = self.tv_weight * util.total_variation(link.x) loss_info.append(('tv', float(tv_loss.data))) loss+=tv_loss loss.backward() self.optimizer.update() return loss_info
def forward(self, x, stage): ''' for alpha in [0, 1), and 2*k+2 + alpha < self.max_stage (-1 <= k <= ...): stage 0 + alpha : p <- block[0] <- in[0] * 1 stage 2*k+1 + alpha : p <- ... <- block[k] <- (up <- in[k]) * (1 - alpha) .................... <- (block[k+1] <- in[k+1]) * (alpha) stage 2*k+2 + alpha : p <- ............... <- (block[k+1] <- in[k+1]) * 1 over flow stages continues. ''' stage = min(stage, self.max_stage - 1e-8) alpha = stage - math.floor(stage) stage = math.floor(stage) h = x if stage % 2 == 0: k = (stage - 2) // 2 h = F.leaky_relu(self.ins[k + 1](h)) for i in reversed(range(0, (k + 1) + 1)): # k+1 .. 0 h = self.blocks[i](h) else: k = (stage - 1) // 2 h_0 = F.leaky_relu(self.ins[k](downscale2x(h))) h_1 = self.blocks[k + 1](F.leaky_relu(self.ins[k + 1](x))) assert 0. <= alpha < 1. h = (1.0 - alpha) * h_0 + alpha * h_1 for i in reversed(range(0, k + 1)): # k .. 0 h = self.blocks[i](h) inv_norm = F.rsqrt( F.square(h[:, -9:-6]) + F.square(h[:, -6:-3]) + 1e-8) camera_param = F.concat( [h[:, -9:-6] * inv_norm, h[:, -6:-3] * inv_norm, h[:, -3:]]) return h[:, :-9], camera_param
def calc_2d_normal(x1, x2, mu1, mu2, s1, s2, rho): norm1 = F.broadcast_to(x1, mu1.shape) - mu1 norm2 = F.broadcast_to(x2, mu2.shape) - mu2 s1s2 = s1 * s2 z = F.square(norm1 / s1) + F.square(norm2 / s2) - 2 * rho * norm1 * norm2 / s1s2 neg_rho = 1 - F.square(rho) return F.exp(-z / (2 * neg_rho)) / (2 * np.pi * s1s2 * F.sqrt(neg_rho))
def color_adjust(x, args): if args.iter % args.save_intervel == 0: save_result(x, args) args.iter += 1 # Input for VGG x_vgg = np.asarray(np.reshape(x, args.shape), dtype=np.float32) x_vgg_var = Variable(chainer.dataset.concat_examples([x_vgg], args.gpu)) # Poisson loss poisson_loss = F.sum( F.square(args.content_laplace - F.convolution_2d(x_vgg_var, W=args.W_laplace, pad=1)) * args.inverse_border_mask_var) # tv loss tv_loss = total_variation(x_vgg_var) # Content loss content_loss = F.sum( F.square((args.bg_var - x_vgg_var) * args.inverse_mask_var)) loss = args.poisson_weight * poisson_loss + args.tv_weight * tv_loss + args.content_weight * content_loss # Backward loss.backward() # Transfer loss & diff from GPU to CPU loss = cuda.to_cpu(loss.data) dx = np.squeeze(cuda.to_cpu(x_vgg_var.grad)) return loss, np.asarray(dx.flatten(), dtype=np.float64)
def compute_tv_loss(images, masks): # s1 = cf.absolute(images[:, :, 1:, :-1] - images[:, :, :-1, :-1]) # s2 = cf.absolute(images[:, :, :-1, 1:] - images[:, :, :-1, :-1]) s1 = cf.square(images[:, :, 1:, :-1] - images[:, :, :-1, :-1]) s2 = cf.square(images[:, :, :-1, 1:] - images[:, :, :-1, :-1]) masks = cf.broadcast_to(masks[:, None, :-1, :-1], s1.shape) masks = masks.data == 1 return cf.sum(masks * (s1 + s2))
def __call__(self, x, mask): y = self.fwd(x) sum_0 = F.sum(y) prod_0 = F.prod(y) loss = (F.sum(F.square((y * mask) - x)) + F.square(sum_0 - 45.)) #F.log(F.square(prod_0 - 362880.) + 1e-8)) return loss
def loss_dis(self, dis, y_fake, y_real): batchsize = len(y_fake) L1 = F.sum(F.square(y_real - 1.)) # F.sum(F.softplus(-y_real)) / batchsize L2 = F.sum(F.square(y_fake)) # F.sum(F.softplus(y_fake)) / batchsize loss = (L1 + L2) / batchsize chainer.report({'loss': loss}, dis) return loss
def _margin_loss(self, x, t, m_plus=0.9, m_minus=0.1, l=0.5): """ """ batchsize = x.shape[0] t_discreted = self.xp.zeros(x.shape, dtype="float32") t_discreted[self.xp.arange(batchsize), t] = 1. L_present = t_discreted * F.square(F.relu(m_plus - x)) L_absent = (1. - t_discreted) * F.square(F.relu(x - m_minus)) L = L_present + l * L_absent return F.mean(F.sum(L, axis=-1))
def _gaussian_kl_divergence(self, p, q): p_mean = p[0][:Z_DIM] p_logstd = p[0][Z_DIM:] p_var = F.square(F.exp(p_logstd)) q_mean = q[0][:Z_DIM] q_logstd = q[0][Z_DIM:] q_var = F.square(F.exp(q_logstd)) kl = (F.log(q_var / p_var) + (p_var + F.square(p_mean - q_mean)) / q_var - 1) * 0.5 return F.sum(kl)
def __call__(self, d_x_gen, d_x_real=None): bs_d_x_gen = d_x_gen.shape[0] if d_x_real is not None: bs_d_x_real = d_x_real.shape[0] loss = F.sum(F.square(d_x_real - 1)) / bs_d_x_real /2 \ + F.sum(F.square(d_x_gen)) / bs_d_x_gen / 2 return loss else: loss = F.sum(F.square(d_x_gen - 1)) / bs_d_x_gen / 2 return loss
def forward(self, z): camera_param = self.net(z) # normalize to cos**2+sin**2=1 inv_norm = F.rsqrt( F.square(camera_param[:, :3]) + F.square(camera_param[:, 3:6]) + 1e-8) camera_param = F.concat([ camera_param[:, :3] * inv_norm, camera_param[:, 3:6] * inv_norm, camera_param[:, 6:] ]) return camera_param
def __call__(self, d_x_gen, d_x_real=None): bs_d_x_gen = d_x_gen.shape[0] if d_x_real is not None: bs_d_x_real = d_x_real.shape[0] loss = F.sum(F.square(d_x_real - 1)) / bs_d_x_real /2 \ + F.sum(F.square(d_x_gen)) / bs_d_x_gen / 2 return loss else: loss = F.sum(F.square(d_x_gen - 1)) / bs_d_x_gen / 2 return loss
def obtain_loss(self, lambda_val, P1, W1, X, P2, W2, Y, A, R, alpha, beta): loss = 0 loss += lambda_val * F.sum( F.square(F.matmul(P1, F.transpose(self.u.W)) - F.matmul(W1, X))) loss += lambda_val * F.sum( F.square(F.matmul(P2, F.transpose(self.v.W)) - F.matmul(W2, Y))) loss += alpha * F.sum( F.square(A * (R - F.matmul(self.u.W, F.transpose(self.v.W))))) loss += beta * ( (F.sum(F.square(self.u.W)) + F.sum(F.square(self.v.W)))) return loss
def edge_length_loss(vertices, faces): assert vertices.ndim == 3 assert faces.ndim == 2 v0 = vertices[:, faces[:, 0], :] # [bs, nf, 3] v1 = vertices[:, faces[:, 1], :] v2 = vertices[:, faces[:, 2], :] l01 = cf.mean(cf.square(v0 - v1)) l12 = cf.mean(cf.square(v1 - v2)) l20 = cf.mean(cf.square(v2 - v0)) return l01 + l12 + l20
def chainer_2d_normal(self, x1, x2, mu1, mu2, s1, s2, rho): norm1 = x1 - mu1 norm2 = x2 - mu2 s1s2 = s1 * s2 + 1e-15 eps = 1e-15 z = F.square(norm1 / (s1 + eps)) + F.square( norm2 / (s2 + eps)) - 2 * (rho * norm1 * norm2) / s1s2 neg_rho = 1 - F.square(rho) + 1e-15 result = F.exp(-z / (2 * neg_rho)) denom = (2 * np.pi) * s1s2 * F.sqrt(neg_rho) + 1e-15 result /= denom return result
def get_loss(scene, googlenet, images, masks, lambda_length, without_mask): features = extract_feature(googlenet, images) if without_mask: loss = -cf.sum(cf.square(features)) loss /= features.size else: scale = masks.shape[2] / features.shape[2] masks = cf.average_pooling_2d(masks[:, None, :, :], scale, scale)[:, 0, :, :] loss = -cf.sum(cf.square(features * cf.broadcast_to(masks[:, None, :, :], features.shape))) loss /= features.shape[0] * features.shape[1] * cf.sum(masks) for mesh in scene.mesh_list.values(): loss += lambda_length * mesh.get_var_line_length_loss() return loss
def loss_f_dict(c, c_char, c_feature, c_mask, q, q_char, q_feature, q_mask, c_has_gloss, c_gloss, q_has_gloss, q_gloss, target): """ # rec_loss = 0 start_scores, end_scores = self.forward(c, c_char, c_feature, c_mask, q, q_char, q_feature, q_mask) # start_losses = F.bernoulli_nll(start_scores, target[:, 0, 0].astype(self.xp.float32)) # end_losses = F.bernoulli_nll(end_scores, target[:, 0, 1].astype(self.xp.float32)) # start_losses = F.bernoulli_nll(target[:, 0, 0].astype(self.xp.float32), start_scores) # end_losses = F.bernoulli_nll(target[:, 0, 1].astype(self.xp.float32), end_scores) start_losses = self.neg_loglikelihood_fun(target[:, 0, 0], start_scores) end_losses = self.neg_loglikelihood_fun(target[:, 0, 1], end_scores, c_mask) rec_loss = start_losses + end_losses """ rl_loss = 0 start_scores, end_scores = self.forward(c, c_char, c_feature, c_mask, q, q_char, q_feature, q_mask, c_has_gloss, c_gloss, q_has_gloss, q_gloss) mle_loss = self.mle_loss_func(c, c_char, c_feature, c_mask, q, q_char, q_feature, q_mask, target, start_scores, end_scores) # if self.args.lambda_param != 1: if self.args.fine_tune: rl_loss = self.rl_loss_func(c, c_char, c_feature, c_mask, q, q_char, q_feature, q_mask, target, start_scores, end_scores) self.rec_loss = mle_loss # self.loss = self.args.lambda_param * mle_loss + (1 - self.args.lambda_param) * rl_loss if self.args.fine_tune: self.loss = mle_loss / (2 * F.square(self.sigma_a[0])) \ + rl_loss / (2 * F.square(self.sigma_b[0])) \ + F.log(F.square(self.sigma_a[0])) \ + F.log(F.square(self.sigma_b[0])) else: self.loss = mle_loss """ # computational graph if (self.args.dot_file is not None) and os.path.exists(self.args.dot_file) is False: g = CG.build_computational_graph((self.rec_loss,)) with open(self.args.dot_file, 'w') as f: f.write(g.dump()) """ chainer.report( {'mle_loss': mle_loss, 'rl_loss': rl_loss, 'loss': self.loss}, observer=self) return self.loss
def compute_double_q_learning_loss(self, l_obs, l_act, l_rew, l_next_obs, l_done): """ :param l_obs: A chainer variable holding a list of observations. Should be of shape N * |S|. :param l_act: A chainer variable holding a list of actions. Should be of shape N. :param l_rew: A chainer variable holding a list of rewards. Should be of shape N. :param l_next_obs: A chainer variable holding a list of observations at the next time step. Should be of shape N * |S|. :param l_done: A chainer variable holding a list of binary values (indicating whether episode ended after this time step). Should be of shape N. :return: A chainer variable holding a scalar loss. """ # Hint: You may want to make use of the following fields: self._discount, self._q, self._qt # Hint2: Q-function can be called by self._q.forward(argument) # Hint3: You might also find https://docs.chainer.org/en/stable/reference/generated/chainer.functions.select_item.html useful # loss = C.Variable(np.array([0.])) # TODO: replace this line l_rew = F.cast(l_rew, np.float32) q_future = self._q.forward(l_next_obs) qt_future = self._qt.forward(l_next_obs) future_rew = l_rew + self._discount * F.select_item( qt_future, F.argmax(q_future, axis=1)) target = F.select_item(F.stack([future_rew, l_rew], axis=1), F.cast(l_done, np.int32)) y = F.select_item(self._q.forward(l_obs), l_act) return F.mean(F.square(y - target))
def compute_double_q_learning_loss(self, l_obs, l_act, l_rew, l_next_obs, l_done): """ :param l_obs: A chainer variable holding a list of observations. Should be of shape N * |S|. :param l_act: A chainer variable holding a list of actions. Should be of shape N. :param l_rew: A chainer variable holding a list of rewards. Should be of shape N. :param l_next_obs: A chainer variable holding a list of observations at the next time step. Should be of shape N * |S|. :param l_done: A chainer variable holding a list of binary values (indicating whether episode ended after this time step). Should be of shape N. :return: A chainer variable holding a scalar loss. """ # Hint: You may want to make use of the following fields: self._discount, self._q, self._qt # Hint2: Q-function can be called by self._q.forward(argument) # Hint3: You might also find # https://docs.chainer.org/en/stable/reference/generated/chainer.functions.select_item.html # useful # loss = C.Variable(np.array([0.])) # compute target q value named y q_next = self._qt.forward(l_next_obs) a_next = F.argmax(self._q.forward(l_next_obs), axis=1) q_act_next = F.select_item(q_next, a_next) y = l_rew + self._discount * q_act_next * (1 - l_done) # compute mean square loss function q = self._q.forward(l_obs) q_act = F.select_item(q, l_act) loss = F.mean(F.square(q_act - y)) return loss
def lf(frames): mu, ln_var = self.encode(frames) z = F.gaussian(mu, ln_var) frames_flat = F.reshape( frames, (-1, frames.shape[1] * frames.shape[2] * frames.shape[3])) variational_flat = F.reshape( self.decode(z), (-1, frames.shape[1] * frames.shape[2] * frames.shape[3])) rec_loss = F.sum(F.square(frames_flat - variational_flat), axis=1) # l2 reconstruction loss rec_loss = F.mean(rec_loss) kl_loss = F.sum(F.gaussian_kl_divergence(mu, ln_var, reduce="no"), axis=1) if self._cpu: kl_tolerance = np.asarray(self.kl_tolerance * self.n_latent).astype(np.float32) else: kl_tolerance = cp.asarray(self.kl_tolerance * self.n_latent).astype(cp.float32) kl_loss = F.maximum(kl_loss, F.broadcast_to(kl_tolerance, kl_loss.shape)) kl_loss = F.mean(kl_loss) loss = rec_loss + kl_loss chainer.report({'loss': loss}, observer=self) chainer.report({'kl_loss': kl_loss}, observer=self) chainer.report({'rec_loss': rec_loss}, observer=self) return loss
def compute_weighted_value_loss(y, t, weights, clip_delta=True, batch_accumulator='mean'): """Compute a loss for value prediction problem. Args: y (Variable or ndarray): Predicted values. t (Variable or ndarray): Target values. weights (ndarray): Weights for y, t. clip_delta (bool): Use the Huber loss function if set True. batch_accumulator (str): 'mean' will devide loss by batchsize Returns: (Variable) scalar loss """ assert batch_accumulator in ('mean', 'sum') y = F.reshape(y, (-1, 1)) t = F.reshape(t, (-1, 1)) if clip_delta: losses = F.huber_loss(y, t, delta=1.0) else: losses = F.square(y - t) / 2 losses = F.reshape(losses, (-1, )) loss_sum = F.sum(losses * weights) if batch_accumulator == 'mean': loss = loss_sum / y.shape[0] elif batch_accumulator == 'sum': loss = loss_sum return loss
def f_loss_grad(x): set_flat_params(self, x) self.cleargrads() values = self.compute_baselines(obs) loss = F.mean(F.square(values - targets)) loss.backward() flat_grad = get_flat_grad(self) return loss.data.astype(np.float64), flat_grad.astype(np.float64)
def _smooth_l1_loss(x, t, in_weight, sigma): sigma2 = sigma ** 2 diff = in_weight * (x - t) abs_diff = F.absolute(diff) flag = (abs_diff.array < (1. / sigma2)).astype(np.float32) y = (flag * (sigma2 / 2.) * F.square(diff) + (1 - flag) * (abs_diff - 0.5 / sigma2)) return F.sum(y)
def logli(self, a): a = F.cast(a, np.float32) # transform back to standard normal zs = (a - self.means) * F.exp(-self.log_stds) # density of standard normal: f(z) = (2*pi*det|Σ|)^(-n/2) * exp(-|x|^2/2) # the return value should be log f(z) return - F.sum(self.log_stds, axis=-1) - \ 0.5 * F.sum(F.square(zs), axis=-1) - \ 0.5 * self.means.shape[-1] * np.log(2 * np.pi)
def __call__(self, h): if len(h.shape) != 4: return 0 # (b, c, h, w) -> (b, h, w, c) -> (b, h*w, c) h = F.transpose(h, (0, 2, 3, 1)) shape = h.shape b, n, c = shape[0], shape[1]*shape[2], shape[3] h = F.reshape(h, (b, n, c)) s = 0 xp = cuda.get_array_module(h.data) I_ = xp.identity(n) I_ = Variable(to_device(I_, device)) for h_ in h: s += F.sum(F.square(F.linear(h_, h_) - I_)) l = s / (b * n * c) return l
def kl_div(self, other): """ Given the distribution parameters of two diagonal multivariate Gaussians, compute their KL divergence (vectorized) Reference: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence#Kullback.E2.80.93Leibler_divergence_for_multivariate_normal_distributions In general, for two n-dimensional distributions, we have D_KL(N1||N2) = 1/2 ( tr(Σ_2^{-1}Σ_1) + (μ_2 - μ_1)^T Σ_2^{-1} (μ_2 - μ_1) - n + ln(det(Σ_2) / det(Σ_1)) ) Here, Σ_1 and Σ_2 are diagonal. Hence this equation can be simplified. In terms of the parameters of this method, - ln(det(Σ_2) / det(Σ_1)) = sum(2 * (log_stds_2 - log_stds_1), axis=-1) - (μ_2 - μ_1)^T Σ_2^{-1} (μ_2 - μ_1) = sum((means_1 - means_2)^2 / vars_2, axis=-1) - tr(Σ_2^{-1}Σ_1) = sum(vars_1 / vars_2, axis=-1) Where - vars_1 = exp(2 * log_stds_1) - vars_2 = exp(2 * log_stds_2) Combined together, we have D_KL(N1||N2) = 1/2 ( tr(Σ_2^{-1}Σ_1) + (μ_2 - μ_1)^T Σ_2^{-1} (μ_2 - μ_1) - n + ln(det(Σ_2) / det(Σ_1)) ) = sum(1/2 * ((vars_1 - vars_2) / vars_2 + (means_1 - means_2)^2 / vars_2 + 2 * (log_stds_2 - log_stds_1)), axis=-1) = sum( ((means_1 - means_2)^2 + vars_1 - vars_2) / (2 * vars_2) + (log_stds_2 - log_stds_1)), axis=-1) :param means_1: List of mean parameters of the first distribution :param log_stds_1: List of log standard deviation parameters of the first distribution :param means_2: List of mean parameters of the second distribution :param log_stds_2: List of log standard deviation parameters of the second distribution :return: An array of KL divergences. """ vars = F.exp(2 * self.log_stds) other_vars = F.exp(2 * other.log_stds) return F.sum((F.square(self.means - other.means) + vars - other_vars) / (2 * other_vars + 1e-8) + other.log_stds - self.log_stds, axis=-1)
def __call__(self): self.renderer.eye = neural_renderer.get_points_from_angles(2.732, 0, np.random.uniform(0, 360)) image = self.renderer.render(self.vertices, self.faces, cf.tanh(self.textures)) loss = cf.sum(cf.square(image - self.image_ref.transpose((2, 0, 1))[None, :, :, :])) return loss
def __call__(self): image = self.renderer.render_silhouettes(self.vertices, self.faces) loss = cf.sum(cf.square(image - self.image_ref[None, :, :])) return loss