def cost_virtual_adversarial_training(self,x,t,test=False,unchain_clean_y=True):
     cost_fitness,y = self.cost_fitness_with_y(x,t,test=test)
     py = self.nn.py_given_y(y)
     xvadv,ptb = self.get_virtual_adversarial_examples_for_py(x,py,test=test)
     py_given_xvadv = self.nn.py_given_x(xvadv,test,False)
     cost_fitness_vadv = categorical_kl_divergence(py,py_given_xvadv,unchain_py=unchain_clean_y)
     return cost_fitness, self.lamb*cost_fitness_vadv
 def get_virtual_adversarial_examples_for_py(self,x,py,test=False):
     xp = cupy.get_array_module(*x.data)
     d = xp.random.normal(size=x.data.shape)
     for i in xrange(self.num_power_iteration):
         input_gradient_keeper = InputGradientKeeper()
         d = as_mat(d)
         x_xi_d = x + self.xi*normalize_axis1(d).reshape(x.data.shape)
         x_xi_d_ = input_gradient_keeper(x_xi_d)
         py_given_x_xi_d = self.nn.py_given_x(x_xi_d_,test,False)
         kl = categorical_kl_divergence(py,py_given_x_xi_d,unchain_py=True)
         kl.backward()
         d = input_gradient_keeper.gx
     if (self.norm_constraint_type == 'L2'):
         ptb = perturbation_with_L2_norm_constraint(d,self.epsilon)
     else:
         ptb = perturbation_with_max_norm_constraint(d,self.epsilon)
     xvadv = x + ptb.reshape(x.data.shape)
     return xvadv,ptb