def actor_optimizer(self): action = K.placeholder(shape=[None, self.action_size]) advantages = K.placeholder(shape=[ None, ]) #advatages -> *multi-step* policy = self.actor.output action_prob = K.sum(action * policy, axis=1) cross_entropy = K.log(action_prob + 1e-10) * advantages cross_entropy = -K.mean(cross_entropy) # add (-entropy) to loss function, for enthusiastic search minus_entropy = K.sum(policy * K.log(policy + 1e-10), axis=1) minus_entropy = K.mean(minus_entropy) # optimizing loss minimizes cross_entropy, maximizes entropy loss = cross_entropy #+ 0.01 * minus_entropy optimizer = Adam(lr=self.actor_lr) updates = optimizer.get_updates(loss, self.actor.trainable_weights) train = K.function([self.actor.input, action, advantages], [loss], updates=updates) return train
def SID(y_true, y_pred): y_true = K.switch( K.min(y_true) < 0, y_true - K.min(y_true) + K.epsilon(), y_true + K.epsilon()) y_pred = K.switch( K.min(y_pred) < 0, y_pred - K.min(y_pred) + K.epsilon(), y_pred + K.epsilon()) p_n = y_true / K.sum(y_true, axis=1, keepdims=True) q_n = y_pred / K.sum(y_pred, axis=1, keepdims=True) return (K.sum(p_n * K.log(p_n / q_n)) + K.sum(q_n * K.log(q_n / p_n)))
def hans(self, x, lambda1): # x = tf.nn.l2_normalize(x, axis=[1, 2]) C = [] for i in range(6): B = x[i, :, :, :] for j in range(4): x = tf.tile(tf.expand_dims(B[:, :, j], 2), [1, 1, 4]) C.append(K.sum(tf.multiply(x, B) / tf.to_float(tf.size(B)))) return K.sum(C) * lambda1
def MSE_KL(y_true, y_pred): # y_true=y_true[:,-162:] y_true = K.switch( K.min(y_true) < 0, y_true - K.min(y_true) + K.epsilon(), y_true + K.epsilon()) y_pred = K.switch( K.min(y_pred) < 0, y_pred - K.min(y_pred) + K.epsilon(), y_pred + K.epsilon()) p_n = y_true / K.max(y_true, axis=1, keepdims=True) q_n = y_pred / K.max(y_pred, axis=1, keepdims=True) return K.mean(K.square(y_true - y_pred), axis=-1) + 0.5 * (K.sum(p_n * K.log(p_n / q_n)) + K.sum( (1.001 - p_n) * K.log((1.01 - p_n) / (1.001 - q_n))))
def compile_saliency_function(model, activation_layer=layer_name): input_img = model.input layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) layer_output = layer_dict[activation_layer].output max_output = k.max(layer_output, axis=3) saliency = k.gradients(k.sum(max_output), input_img)[0] return k.function([input_img, k.learning_phase()], [saliency])
def _get_coords_for_joint(joint_idx, parent_idx, child_angle_idx, coords): if parent_idx is None: # joint_idx should be 0 coords[joint_idx] = K.zeros(base_shape[:-2] + [3, 1]) parent_bone = K.constant( np.concatenate([ np.ones(base_shape), np.zeros(base_shape), np.zeros(base_shape) ], axis=-2)) else: parent_bone = coords[parent_idx] - coords[joint_idx] parent_bone_norm = K.sqrt( K.sum(K.square(parent_bone), axis=-2, keepdims=True) + K.epsilon()) parent_bone = parent_bone / parent_bone_norm for child_idx in body_graph[joint_idx]: child_bone = tf.matmul(rotmat_list[child_angle_idx], parent_bone) child_bone_idx = bone_idcs[(joint_idx, child_idx)] child_bone = child_bone * K.reshape( bone_len_list[child_bone_idx], (child_bone.shape[0], 1, 1, 1)) coords[child_idx] = child_bone + coords[joint_idx] child_angle_idx += 1 for child_idx in body_graph[joint_idx]: child_angle_idx, coords = _get_coords_for_joint( child_idx, joint_idx, child_angle_idx, coords) return child_angle_idx, coords
def _get_bone_len(arg): bone_list = tf.unstack(arg[:, :, 0, :], axis=1) bones = [ bone_list[j] - bone_list[i] for i, j in zip(members_from, members_to) ] bones = K.stack(bones, axis=1) return K.sqrt(K.sum(K.square(bones), axis=-1) + K.epsilon())
def add_loss(model, W): inputs = model.inputs[0] abnormal = model.inputs[1] # abnormal = K.print_tensor(abnormal, message='abnormal = ') outputs = model.outputs[0] z_mean = model.get_layer('z_mean').output z_log_var = model.get_layer('z_log_var').output beta = K.sum(1.0 - abnormal, axis=-1, keepdims=True) / W # beta = K.print_tensor(beta, message='beta = ') reconstruction_loss = mean_squared_error(inputs, outputs) reconstruction_loss *= W kl_loss = 1 + z_log_var - beta * K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(reconstruction_loss + kl_loss) model.add_loss(vae_loss)
def tsne(P, activations): # d = K.shape(activations)[1] v = d - 1. eps = K.variable( 10e-15 ) # needs to be at least 10e-8 to get anything after Q /= K.sum(Q) sum_act = K.sum(K.square(activations), axis=1) Q = K.reshape(sum_act, [-1, 1]) + -2 * K.dot(activations, K.transpose(activations)) Q = (sum_act + Q) / v Q = K.pow(1 + Q, -(v + 1) / 2) Q *= K.variable(1 - np.eye(n)) Q /= K.sum(Q) Q = K.maximum(Q, eps) C = K.log((P + eps) / (Q + eps)) C = K.sum(P * C) return C
def style_loss(style, gen): assert K.ndim(style) == 3 assert K.ndim(gen) == 3 S = gram_matrix(style) G = gram_matrix(gen) channels = 3 size = img_h * img_w # Euclidean distance of the gram matrices multiplied by the constant return K.sum(K.square(S - G)) / (4. * (channels**2) * (size**2))
def _get_avg_bone_len(arg): bone_list = tf.unstack(arg[:, :, 0, :], axis=1) bones = [ bone_list[j] - bone_list[i] for i, j in zip(members_from, members_to) ] bones = K.expand_dims(K.stack(bones, axis=1), axis=2) bone_len = K.sqrt( K.sum(K.square(bones), axis=-1, keepdims=True) + K.epsilon()) return K.mean(bone_len, axis=1, keepdims=True)
def total_variation_loss(x): assert K.ndim(x) == 4 if K.image_data_format() == 'channels_first': a = K.square(x[:, :, :img_h - 1, :img_w - 1] - x[:, :, 1:, :img_w - 1]) b = K.square(x[:, :, :img_h - 1, :img_w - 1] - x[:, :, :img_h - 1, 1:]) else: # Move the image pixel by pixel, and calculate the variance a = K.square(x[:, :img_h - 1, :img_w - 1, :] - x[:, 1:, :img_w - 1, :]) b = K.square(x[:, :img_h - 1, :img_w - 1, :] - x[:, :img_h - 1, 1:, :]) return K.sum(K.pow(a + b, 1.25))
def call(self, x, mask=None): # return K.softmax(3*x) x *= K.cast(x >= 0., K.floatx()) # x = K.abs(x) # x = x - K.min(K.flatten(x)) x = K.transpose(x) x_normalized = x / (K.sum(x, axis=0) + K.epsilon()) x = K.transpose(x_normalized) # x *= K.cast(x/K.max(x) >= 0.3, K.floatx()) # x = 4.0*x/(K.max(K.flatten(x))+K.epsilon()) # x=K.softmax(x) return x
def edm(x, y=None): with K.name_scope('edm'): y = x if y is None else y x = K.expand_dims(x, axis=1) y = K.expand_dims(y, axis=2) return K.sqrt(K.sum(K.square(x - y), axis=-1) + K.epsilon())
def __call__(self, p): p *= K.cast(p >= 0., K.floatx()) return p / (K.epsilon() + K.sqrt(K.sum(K.square(p), axis=self.axis, keepdims=True)))
def edm_loss(y_true, y_pred): return K.mean(K.sum(K.square(edm(y_true) - edm(y_pred)), axis=[1, 2]))
def content_loss(content, gen): assert K.ndim(content) == 3 assert K.ndim(gen) == 3 # Euclidean distance return K.sum(K.square(gen - content))