我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用keras.backend.stop_gradient()。
def symbolic_fgs(x, grad, eps=0.3, clipping=True): """ FGSM attack. """ # signed gradient normed_grad = K.sign(grad) # Multiply by constant epsilon scaled_grad = eps * normed_grad # Add perturbation to original example to obtain adversarial example adv_x = K.stop_gradient(x + scaled_grad) if clipping: adv_x = K.clip(adv_x, 0, 1) return adv_x
def call(self, x): s, s_hat = x # Compute the variables defined in the class comment S2 = K.sum(s) S1 = s_hat[0, 1] N = s_hat[0, 0] # Compute the unbiased weights a2 = (S1 + S2) / N / s # Compute the biased weights and the scaling factor t a1 = K.pow(a2, self.k) sT = K.transpose(s) t = K.dot(sT, a2) / K.dot(sT, a1) return K.stop_gradient([a1 * t])[0]
def round_through(x): '''Element-wise rounding to the closest integer with full gradient propagation. A trick from [Sergey Ioffe](http://stackoverflow.com/a/36480182) ''' rounded = K.round(x) return x + K.stop_gradient(rounded - x)
def _mean_abs(x, axis=None, keepdims=False): return K.stop_gradient(K.mean(K.abs(x), axis=axis, keepdims=keepdims))
def ternarize(W, H=1): '''The weights' ternarization function, # References: - [Recurrent Neural Networks with Limited Numerical Precision](http://arxiv.org/abs/1608.06902) - [Ternary Weight Networks](http://arxiv.org/abs/1605.04711) ''' Wt = _ternarize(W, H) return W + K.stop_gradient(Wt - W)
def ternarize_dot(x, W): '''For RNN (maybe Dense or Conv too). Refer to 'Recurrent Neural Networks with Limited Numerical Precision' Section 3.1 ''' Wt = _ternarize(W) return K.dot(x, W) + K.stop_gradient(K.dot(x, Wt - W))
def call(self, x): return K.stop_gradient(x[1])
def call(self, x): s, x1 = x a = x1[:, :1] s_hat = x1[:, 1:2] # Rescale the weights, making sure we mostly scale down a_hat = a * K.clip(s_hat / s, self.min_decrease, self.max_increase) # Scale again so that the reported loss is comparable to the other ones t = 1 #sT = K.transpose(s) #t = K.dot(sT, a) / K.dot(sT, a_hat) return K.stop_gradient([a_hat * t])[0]
def call(self, inputs, training=None): def noised(): stddev = K.stop_gradient(K.sqrt(K.clip(self.factor * K.abs(inputs), self.epsilon, None))) return inputs + K.random_normal(shape=K.shape(inputs), mean=0.0, stddev=stddev) return K.in_train_phase(noised, inputs, training=training)
def create_policy_loss(policy_t, value_t, n_actions): """ Policy loss :param policy_t: policy tensor from prediction part :param value_t: value tensor from prediction part :param n_actions: count of actions in space :param entropy_beta: entropy loss scaling factor :return: action_t, advantage_t, policy_loss_t """ action_t = Input(batch_shape=(None, 1), name='action', dtype='int32') reward_t = Input(batch_shape=(None, 1), name="reward") def policy_loss_func(args): p_t, v_t, act_t, rew_t = args log_p_t = tf.nn.log_softmax(p_t) oh_t = K.one_hot(act_t, n_actions) oh_t = K.squeeze(oh_t, 1) p_oh_t = K.sum(log_p_t * oh_t, axis=-1, keepdims=True) adv_t = (rew_t - K.stop_gradient(v_t)) tf.summary.scalar("advantage_mean", K.mean(adv_t)) tf.summary.scalar("advantage_rms", K.sqrt(K.mean(K.square(adv_t)))) res_t = -adv_t * p_oh_t tf.summary.scalar("loss_policy_mean", K.mean(res_t)) tf.summary.scalar("loss_policy_rms", K.sqrt(K.mean(K.square(res_t)))) return res_t loss_args = [policy_t, value_t, action_t, reward_t] policy_loss_t = Lambda(policy_loss_func, output_shape=(1,), name='policy_loss')(loss_args) tf.summary.scalar("value_mean", K.mean(value_t)) tf.summary.scalar("reward_mean", K.mean(reward_t)) return action_t, reward_t, policy_loss_t
def style_loss(style, combination, mask_path=None, nb_channels=None): assert K.ndim(style) == 3 assert K.ndim(combination) == 3 if content_mask_path is not None: content_mask = K.variable(load_mask(content_mask_path, nb_channels)) combination = combination * K.stop_gradient(content_mask) del content_mask if mask_path is not None: style_mask = K.variable(load_mask(mask_path, nb_channels)) style = style * K.stop_gradient(style_mask) if content_mask_path is None: combination = combination * K.stop_gradient(style_mask) del style_mask S = gram_matrix(style) C = gram_matrix(combination) channels = 3 size = img_width * img_height return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2)) # an auxiliary loss function # designed to maintain the "content" of the # base image in the generated image
def mcmc_chain(self, x, nb_gibbs_steps): xi = x for i in range(nb_gibbs_steps): xi, xi_pre, xi_sigm = self.gibbs_xhx(xi) x_rec, x_rec_pre, x_rec_sigm = xi, xi_pre, xi_sigm x_rec = K.stop_gradient(x_rec) return x_rec, x_rec_pre, x_rec_sigm
def build(self): model = self.net.model pi_model = self.net.pi_model q_model = self.net.q_model target_model = self.net.target_model target_pi_model = self.net.target_pi_model target_q_model = self.net.target_q_model self.states = tf.placeholder(tf.float32, shape=(None, self.in_dim), name='states') self.actions = tf.placeholder(tf.float32, shape=[None, self.action_dim], name='actions') self.rewards = tf.placeholder(tf.float32, shape=[None], name='rewards') self.next_states = tf.placeholder(tf.float32, shape=[None, self.in_dim], name='next_states') # terminal contain only 0 or 1 it will work as masking #self.terminals = tf.placeholder(tf.bool, shape=[None], name='terminals') self.ys = tf.placeholder(tf.float32, shape=[None]) #y = tf.where(self.terminals, self.rewards, self.rewards + self.gamma * K.stop_gradient(K.sum(target_q_model(Concatenate()([target_model(self.next_states), # target_pi_model(self.next_states)])), axis=-1))) self.target_q = K.sum(target_q_model(Concatenate()([target_model(self.states), target_pi_model(self.states)])), axis=-1) self.q = K.sum(q_model(Concatenate()([model(self.states), self.actions])), axis=-1) self.q_loss = K.mean(K.square(self.ys-self.q)) self.mu = pi_model(self.states) self.pi_loss = - K.mean(q_model(Concatenate()([model(self.states), self.mu]))) self.q_updater = self.q_optimizer.minimize(self.q_loss, var_list=self.net.var_q) self.pi_updater = self.pi_opimizer.minimize(self.pi_loss, var_list=self.net.var_pi) self.soft_updater = [K.update(t_p, t_p*(1-self.tau)+p*self.tau) for p, t_p in zip(self.net.var_all, self.net.var_target_all)] self.sync = [K.update(t_p, p) for p, t_p in zip(self.net.var_all, self.net.var_target_all)] self.sess.run(tf.global_variables_initializer()) self.built = True
def build(self): model = self.net.model mu_model = self.net.mu_model log_std_model = self.net.log_std_model q_model = self.net.q_model target_model = self.net.target_model target_mu_model = self.net.target_mu_model target_log_std_model = self.net.target_log_std_model target_q_model = self.net.target_q_model self.states = tf.placeholder(tf.float32, shape=(None, self.in_dim), name='states') self.actions = tf.placeholder(tf.float32, shape=[None, self.action_dim], name='actions') self.rewards = tf.placeholder(tf.float32, shape=[None], name='rewards') self.next_states = tf.placeholder(tf.float32, shape=[None, self.in_dim], name='next_states') self.ys = tf.placeholder(tf.float32, shape=[None]) # There are other implementations about how can we take aciton. # Taking next action version or using only mu version or searching action which maximize Q. target_mu = target_mu_model(self.states) target_log_std = target_log_std_model(self.states) target_action = target_mu + K.random_normal(K.shape(target_mu), dtype=tf.float32) * K.exp(target_log_std) self.target_q = K.sum(target_q_model(Concatenate()([target_model(self.states), target_action])), axis=-1) self.q = K.sum(q_model(Concatenate()([model(self.states), self.actions])), axis=-1) self.q_loss = K.mean(K.square(self.ys-self.q)) self.mu = mu_model(self.states) self.log_std = log_std_model(self.states) self.eta = (self.actions - self.mu) / K.exp(self.log_std) inferred_action = self.mu + K.stop_gradient(self.eta) * K.exp(self.log_std) self.pi_loss = - K.mean(q_model(Concatenate()([model(self.states), inferred_action]))) self.q_updater = self.q_optimizer.minimize(self.q_loss, var_list=self.net.var_q) self.pi_updater = self.pi_opimizer.minimize(self.pi_loss, var_list=self.net.var_pi) self.soft_updater = [K.update(t_p, t_p*(1-self.tau)+p*self.tau) for p, t_p in zip(self.net.var_all, self.net.var_target_all)] self.sync = [K.update(t_p, p) for p, t_p in zip(self.net.var_all, self.net.var_target_all)] self.sess.run(tf.global_variables_initializer()) self.built = True
def call(self, inputs): _, kernel_b = xnorize(self.kernel, self.H) _, inputs_b = xnorize(inputs) outputs = K.conv2d(inputs_b, kernel_b, strides=self.strides, padding=self.padding, data_format=self.data_format, dilation_rate=self.dilation_rate) # calculate Wa and xa # kernel_a mask = K.reshape(self.kernel, (-1, self.filters)) # self.nb_row * self.nb_col * channels, filters kernel_a = K.stop_gradient(K.mean(K.abs(mask), axis=0)) # filters # inputs_a if self.data_format == 'channels_first': channel_axis = 1 else: channel_axis = -1 mask = K.mean(K.abs(inputs), axis=channel_axis, keepdims=True) ones = K.ones(self.kernel_size + (1, 1)) inputs_a = K.conv2d(mask, ones, strides=self.strides, padding=self.padding, data_format=self.data_format, dilation_rate=self.dilation_rate) # nb_sample, 1, new_nb_row, new_nb_col if self.data_format == 'channels_first': outputs = outputs * K.stop_gradient(inputs_a) * K.expand_dims(K.expand_dims(K.expand_dims(kernel_a, 0), -1), -1) else: outputs = outputs * K.stop_gradient(inputs_a) * K.expand_dims(K.expand_dims(K.expand_dims(kernel_a, 0), 0), 0) if self.use_bias: outputs = K.bias_add( outputs, self.bias, data_format=self.data_format) if self.activation is not None: return self.activation(outputs) return outputs # Aliases