Python keras.backend.log() Examples
The following are 30
code examples of keras.backend.log().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example #1
Source File: cartpole_a3c.py From reinforcement-learning with MIT License | 6 votes |
def actor_optimizer(self): action = K.placeholder(shape=(None, self.action_size)) advantages = K.placeholder(shape=(None, )) policy = self.actor.output good_prob = K.sum(action * policy, axis=1) eligibility = K.log(good_prob + 1e-10) * K.stop_gradient(advantages) loss = -K.sum(eligibility) entropy = K.sum(policy * K.log(policy + 1e-10), axis=1) actor_loss = loss + 0.01*entropy optimizer = Adam(lr=self.actor_lr) updates = optimizer.get_updates(self.actor.trainable_weights, [], actor_loss) train = K.function([self.actor.input, action, advantages], [], updates=updates) return train # make loss function for Value approximation
Example #2
Source File: training.py From aiexamples with Apache License 2.0 | 6 votes |
def softmax_loss(y_true, y_pred): """Compute cross entropy loss aka softmax loss. # Arguments y_true: Ground truth targets, tensor of shape (?, num_boxes, num_classes). y_pred: Predicted logits, tensor of shape (?, num_boxes, num_classes). # Returns softmax_loss: Softmax loss, tensor of shape (?, num_boxes). """ eps = K.epsilon() y_pred = K.clip(y_pred, eps, 1. - eps) softmax_loss = -tf.reduce_sum(y_true * tf.log(y_pred), axis=-1) return softmax_loss
Example #3
Source File: losses.py From icassp19 with MIT License | 6 votes |
def crossentropy_reed_wrap(_beta): def crossentropy_reed_core(y_true, y_pred): """ This loss function is proposed in: Reed et al. "Training Deep Neural Networks on Noisy Labels with Bootstrapping", 2014 :param y_true: :param y_pred: :return: """ # hyper param print(_beta) y_pred = K.clip(y_pred, K.epsilon(), 1) # (1) dynamically update the targets based on the current state of the model: bootstrapped target tensor # use predicted class proba directly to generate regression targets y_true_update = _beta * y_true + (1 - _beta) * y_pred # (2) compute loss as always _loss = -K.sum(y_true_update * K.log(y_pred), axis=-1) return _loss return crossentropy_reed_core
Example #4
Source File: training.py From aiexamples with Apache License 2.0 | 6 votes |
def focal_loss(y_true, y_pred, gamma=2, alpha=0.25): """Compute focal loss. # Arguments y_true: Ground truth targets, tensor of shape (?, num_boxes, num_classes). y_pred: Predicted logits, tensor of shape (?, num_boxes, num_classes). # Returns focal_loss: Focal loss, tensor of shape (?, num_boxes). # References https://arxiv.org/abs/1708.02002 """ #y_pred /= K.sum(y_pred, axis=-1, keepdims=True) eps = K.epsilon() y_pred = K.clip(y_pred, eps, 1. - eps) pt = tf.where(tf.equal(y_true, 1), y_pred, 1 - y_pred) focal_loss = -tf.reduce_sum(alpha * K.pow(1. - pt, gamma) * K.log(pt), axis=-1) return focal_loss
Example #5
Source File: trainDL.py From 4Dsurvival with GNU General Public License v3.0 | 6 votes |
def sort4minibatches(xvals, evals, tvals, batchsize): ntot = len(xvals) indices = np.arange(ntot) np.random.shuffle(indices) start_idx=0 esall = [] for end_idx in list(range(batchsize, batchsize*(ntot//batchsize)+1, batchsize))+[ntot]: excerpt = indices[start_idx:end_idx] sort_idx = np.argsort(tvals[excerpt])[::-1] es = excerpt[sort_idx] esall += list(es) start_idx = end_idx return (xvals[esall], evals[esall], tvals[esall], esall) #Define Cox PH partial likelihood function loss. #Arguments: E (censoring status), risk (risk [log hazard ratio] predicted by network) for batch of input subjects #As defined, this function requires that all subjects in input batch must be sorted in descending order of survival/censoring time (i.e. arguments E and risk will be in this order)
Example #6
Source File: loss.py From maskrcnn with MIT License | 6 votes |
def rpn_offsets_loss(gt_offsets, gt_fg, pred_offsets): """RPNのオフセット回帰の損失関数 positive(gt_fg > 0)データのみ評価対象とする gt_offsets: 正解オフセット [N, R, 4] 3軸目は領域提案とアンカーのオフセット(中心、幅、高さ)。 (tx, ty, th, tw) gt_fg: 正解データの前景/背景 [N, R] pred_offsets: 予測値 [N, R, 4]. """ pos_idx = tf.where(gt_fg > 0) gt_offsets = tf.gather_nd(gt_offsets, pos_idx) pred_offsets = tf.gather_nd(pred_offsets, pos_idx) # FasterRCNNの論文上は、RPNのオフセット回帰には係数10を乗ずることでオブジェクト分類損失とのバランスを取ることになっている。 # が、rpnの損失の全損失に占める割合が高すぎるようなら係数調整 p = 1. loss = p * offsets_loss(gt_offsets, pred_offsets) loss = log.tfprint(loss, "rpn_offsets_loss") return loss
Example #7
Source File: wtte.py From wtte-rnn with MIT License | 6 votes |
def __init__(self, kind, reduce_loss=True, clip_prob=1e-6, regularize=False, location=None, growth=None): self.kind = kind self.reduce_loss = reduce_loss self.clip_prob = clip_prob if regularize == True or location is not None or growth is not None: raise DeprecationWarning('Directly penalizing beta has been found \ to be unneccessary when using bounded activation \ and clipping of log-likelihood.\ Use this method instead.')
Example #8
Source File: wtte.py From wtte-rnn with MIT License | 6 votes |
def loss_function(self, y_true, y_pred): y, u, a, b = _keras_split(y_true, y_pred) if self.kind == 'discrete': loglikelihoods = loglik_discrete(y, u, a, b) elif self.kind == 'continuous': loglikelihoods = loglik_continuous(y, u, a, b) if self.clip_prob is not None: loglikelihoods = K.clip(loglikelihoods, log(self.clip_prob), log(1 - self.clip_prob)) if self.reduce_loss: loss = -1.0 * K.mean(loglikelihoods, axis=-1) else: loss = -loglikelihoods return loss # For backwards-compatibility
Example #9
Source File: loss.py From maskrcnn with MIT License | 6 votes |
def sparse_categorical_crossentropy(gt_ids, pred_one_hot_post_softmax): """ K.sparse_categorical_crossentropyだと結果がNaNになる。。。 0割り算が発生しているかも。 https://qiita.com/4Ui_iUrz1/items/35a8089ab0ebc98061c1 対策として、微少値を用いてlog(0)にならないよう調整した本関数を作成。 """ gt_ids = log.tfprint(gt_ids, "cross:gt_ids:") pred_one_hot_post_softmax = log.tfprint(pred_one_hot_post_softmax, "cross:pred_one_hot_post_softmax:") gt_one_hot = K.one_hot(gt_ids, K.shape(pred_one_hot_post_softmax)[-1]) gt_one_hot = log.tfprint(gt_one_hot, "cross:gt_one_hot:") epsilon = K.epsilon() # 1e-07 loss = -K.sum( gt_one_hot * K.log( tf.clip_by_value(pred_one_hot_post_softmax, epsilon, 1 - epsilon)), axis=-1) loss = log.tfprint(loss, "cross:loss:") return loss
Example #10
Source File: reinforce_agent.py From 2019-OSS-Summer-RL with MIT License | 6 votes |
def optimizer(self): action = K.placeholder(shape=[None, 5]) discounted_rewards = K.placeholder(shape=[None, ]) # 크로스 엔트로피 오류함수 계산 action_prob = K.sum(action * self.model.output, axis=1) cross_entropy = K.log(action_prob) * discounted_rewards loss = -K.sum(cross_entropy) # 정책신경망을 업데이트하는 훈련함수 생성 optimizer = Adam(lr=self.learning_rate) updates = optimizer.get_updates(self.model.trainable_weights,[], loss) train = K.function([self.model.input, action, discounted_rewards], [], updates=updates) return train # 정책신경망으로 행동 선택
Example #11
Source File: cartpole_a3c.py From reinforcement-learning with MIT License | 6 votes |
def build_model(self): state = Input(batch_shape=(None, self.state_size)) shared = Dense(self.hidden1, input_dim=self.state_size, activation='relu', kernel_initializer='glorot_uniform')(state) actor_hidden = Dense(self.hidden2, activation='relu', kernel_initializer='glorot_uniform')(shared) action_prob = Dense(self.action_size, activation='softmax', kernel_initializer='glorot_uniform')(actor_hidden) value_hidden = Dense(self.hidden2, activation='relu', kernel_initializer='he_uniform')(shared) state_value = Dense(1, activation='linear', kernel_initializer='he_uniform')(value_hidden) actor = Model(inputs=state, outputs=action_prob) critic = Model(inputs=state, outputs=state_value) actor._make_predict_function() critic._make_predict_function() actor.summary() critic.summary() return actor, critic # make loss function for Policy Gradient # [log(action probability) * advantages] will be input for the back prop # we add entropy of action probability to loss
Example #12
Source File: reinforce_agent.py From reinforcement-learning with MIT License | 6 votes |
def optimizer(self): action = K.placeholder(shape=[None, 5]) discounted_rewards = K.placeholder(shape=[None, ]) # Calculate cross entropy error function action_prob = K.sum(action * self.model.output, axis=1) cross_entropy = K.log(action_prob) * discounted_rewards loss = -K.sum(cross_entropy) # create training function optimizer = Adam(lr=self.learning_rate) updates = optimizer.get_updates(self.model.trainable_weights, [], loss) train = K.function([self.model.input, action, discounted_rewards], [], updates=updates) return train # get action from policy network
Example #13
Source File: breakout_a3c.py From reinforcement-learning with MIT License | 6 votes |
def actor_optimizer(self): action = K.placeholder(shape=[None, self.action_size]) advantages = K.placeholder(shape=[None, ]) policy = self.actor.output good_prob = K.sum(action * policy, axis=1) eligibility = K.log(good_prob + 1e-10) * advantages actor_loss = -K.sum(eligibility) entropy = K.sum(policy * K.log(policy + 1e-10), axis=1) entropy = K.sum(entropy) loss = actor_loss + 0.01*entropy optimizer = RMSprop(lr=self.actor_lr, rho=0.99, epsilon=0.01) updates = optimizer.get_updates(self.actor.trainable_weights, [], loss) train = K.function([self.actor.input, action, advantages], [loss], updates=updates) return train # make loss function for Value approximation
Example #14
Source File: breakout_a3c.py From reinforcement-learning with MIT License | 6 votes |
def build_model(self): input = Input(shape=self.state_size) conv = Conv2D(16, (8, 8), strides=(4, 4), activation='relu')(input) conv = Conv2D(32, (4, 4), strides=(2, 2), activation='relu')(conv) conv = Flatten()(conv) fc = Dense(256, activation='relu')(conv) policy = Dense(self.action_size, activation='softmax')(fc) value = Dense(1, activation='linear')(fc) actor = Model(inputs=input, outputs=policy) critic = Model(inputs=input, outputs=value) actor._make_predict_function() critic._make_predict_function() actor.summary() critic.summary() return actor, critic # make loss function for Policy Gradient # [log(action probability) * advantages] will be input for the back prop # we add entropy of action probability to loss
Example #15
Source File: models.py From sam with MIT License | 6 votes |
def kl_divergence(y_true, y_pred): max_y_pred = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.max(K.max(y_pred, axis=2), axis=2)), shape_r_out, axis=-1)), shape_c_out, axis=-1) y_pred /= max_y_pred sum_y_true = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(K.sum(y_true, axis=2), axis=2)), shape_r_out, axis=-1)), shape_c_out, axis=-1) sum_y_pred = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(K.sum(y_pred, axis=2), axis=2)), shape_r_out, axis=-1)), shape_c_out, axis=-1) y_true /= (sum_y_true + K.epsilon()) y_pred /= (sum_y_pred + K.epsilon()) return 10 * K.sum(K.sum(y_true * K.log((y_true / (y_pred + K.epsilon())) + K.epsilon()), axis=-1), axis=-1) # Correlation Coefficient Loss
Example #16
Source File: vae_definition.py From MIDI-VAE with MIT License | 6 votes |
def sample_vector(vector, sample_method): if np.sum(vector) > 0: if sample_method == 'argmax': max_index = np.argmax(vector) if sample_method == 'choice': vector = vector/(np.sum(vector)*1.0) vector = np.log(vector) / temperature vector = np.exp(vector) / np.sum(np.exp(vector)) #give it number_of_tries to find a note that is above the cutoff_sample_threshold for _ in range(number_of_tries): max_index = np.random.choice(len(vector), p=vector) if vector[max_index] > cutoff_sample_threshold: break else: max_index = 0 return max_index
Example #17
Source File: reinforce_agent.py From reinforcement-learning-kr with MIT License | 6 votes |
def optimizer(self): action = K.placeholder(shape=[None, 5]) discounted_rewards = K.placeholder(shape=[None, ]) # 크로스 엔트로피 오류함수 계산 action_prob = K.sum(action * self.model.output, axis=1) cross_entropy = K.log(action_prob) * discounted_rewards loss = -K.sum(cross_entropy) # 정책신경망을 업데이트하는 훈련함수 생성 optimizer = Adam(lr=self.learning_rate) updates = optimizer.get_updates(self.model.trainable_weights,[], loss) train = K.function([self.model.input, action, discounted_rewards], [], updates=updates) return train # 정책신경망으로 행동 선택
Example #18
Source File: ChainCRF.py From naacl18-multitask_argument_mining with Apache License 2.0 | 5 votes |
def logsumexp(x, axis=None): '''Returns `log(sum(exp(x), axis=axis))` with improved numerical stability. ''' xmax = K.max(x, axis=axis, keepdims=True) xmax_ = K.max(x, axis=axis) return xmax_ + K.log(K.sum(K.exp(x - xmax), axis=axis))
Example #19
Source File: training.py From aiexamples with Apache License 2.0 | 5 votes |
def compute_learning_rate(self, n, methode): n_desired = self.n_desired desired = self.desired base_lr = self.base_lr bias = self.bias offset = base_lr * desired * bias base_lr = base_lr - offset desired = desired / (1-desired*bias) * (1-bias) if methode == 'default': k = (1 - desired) / n_desired lr = np.maximum( -k * n + 1, 0) elif methode == 'linear': k = (1 / desired - 1) / n_desired lr = 1 / (1 + k * n) elif methode == 'quadratic': k = (np.sqrt(1/desired)-1) / n_desired lr = 1 / (1 + k * n)**2 elif methode == 'exponential': k = -1 * np.log(desired) / n_desired lr = np.exp(-k*n) lr = base_lr * lr + offset lr = np.maximum(lr, self.base_lr * self.minimum) return lr
Example #20
Source File: models.py From DeepFMPO with MIT License | 5 votes |
def maximization(y_true, y_pred): return K.mean(-K.log(y_pred) * y_true)
Example #21
Source File: model_connect4.py From connect4-alpha-zero with MIT License | 5 votes |
def objective_function_for_policy(y_true, y_pred): # can use categorical_crossentropy?? return K.sum(-y_true * K.log(y_pred + K.epsilon()), axis=-1)
Example #22
Source File: bert.py From keras-transformer with MIT License | 5 votes |
def __call__(self, y_true, y_pred): y_true_val = y_true[:, :, 0] mask = y_true[:, :, 1] # masked per-sample means of each loss num_items_masked = K.sum(mask, axis=-1) + 1e-6 masked_cross_entropy = ( K.sum(mask * K.sparse_categorical_crossentropy(y_true_val, y_pred), axis=-1) / num_items_masked) masked_entropy = ( K.sum(mask * -K.sum(y_pred * K.log(y_pred), axis=-1), axis=-1) / num_items_masked) return masked_cross_entropy - self.penalty_weight * masked_entropy
Example #23
Source File: ChainCRF.py From naacl18-multitask_argument_mining with Apache License 2.0 | 5 votes |
def sparse_chain_crf_loss(y, x, U, b_start=None, b_end=None, mask=None): '''Given the true sparsely encoded tag sequence y, input x (with mask), transition energies U, boundary energies b_start and b_end, it computes the loss function of a Linear Chain Conditional Random Field: loss(y, x) = NNL(P(y|x)), where P(y|x) = exp(E(y, x)) / Z. So, loss(y, x) = - E(y, x) + log(Z) Here, E(y, x) is the tag path energy, and Z is the normalization constant. The values log(Z) is also called free energy. ''' x = add_boundary_energy(x, b_start, b_end, mask) energy = path_energy0(y, x, U, mask) energy -= free_energy0(x, U, mask) return K.expand_dims(-energy, -1)
Example #24
Source File: loss.py From maskrcnn with MIT License | 5 votes |
def head_offsets_loss(gt_offsets, gt_labels, pred_offsets): """ヘッドのオフセット回帰の損失関数 positive(gt_fg > 0)データのみ評価対象とする gt_offsets: 正解オフセット [N, R, 4] gt_labels: 正解データのラベルID [N, R] pred_offsets: ラベル毎の予測値 [N, R, n_labels, 4]. """ # 正解データのラベルIDに対応するオフセットのみを損失評価対象とする。 # 論文には以下のようにあるので、正解ラベルのBBoxのみで良さそう。 # The second task loss, Lloc, is defined over a tuple of true bounding-box # regression targets for class u, v = (vx, vy, vw, vh), and a predicted # tuple tu = (tux , tuy , tuw, tuh ), again for class u. pos_idx = tf.where(gt_labels > 0) i = K.cast(pos_idx[:, 0], tf.int32) j = K.cast(pos_idx[:, 1], tf.int32) k = K.cast(tf.gather_nd(gt_labels, pos_idx), tf.int32) pos_pred_idx = K.stack((i, j, k), axis=1) pred_offsets = tf.gather_nd(pred_offsets, pos_pred_idx) gt_offsets = tf.gather_nd(gt_offsets, pos_idx) loss = offsets_loss(gt_offsets, pred_offsets) loss = log.tfprint(loss, "head_offsets_loss") return loss
Example #25
Source File: ChainCRF.py From naacl18-multitask_argument_mining with Apache License 2.0 | 5 votes |
def logsumexp(x, axis=None): '''Returns `log(sum(exp(x), axis=axis))` with improved numerical stability. ''' return tf.reduce_logsumexp(x, axis=[axis])
Example #26
Source File: wtte-rnn.py From keras-wtte-rnn with MIT License | 5 votes |
def weibull_loglik_continuous(y_true, ab_pred, name=None): y_ = y_true[:, 0] u_ = y_true[:, 1] a_ = ab_pred[:, 0] b_ = ab_pred[:, 1] ya = (y_ + 1e-35) / a_ return -1 * k.mean(u_ * (k.log(b_) + b_ * k.log(ya)) - k.pow(ya, b_))
Example #27
Source File: wtte-rnn.py From keras-wtte-rnn with MIT License | 5 votes |
def weibull_loglik_discrete(y_true, ab_pred, name=None): y_ = y_true[:, 0] u_ = y_true[:, 1] a_ = ab_pred[:, 0] b_ = ab_pred[:, 1] hazard0 = k.pow((y_ + 1e-35) / a_, b_) hazard1 = k.pow((y_ + 1) / a_, b_) return -1 * k.mean(u_ * k.log(k.exp(hazard1 - hazard0) - 1.0) - hazard1)
Example #28
Source File: train.py From async-rl with MIT License | 5 votes |
def learn(self, last_observations, actions, rewards, learning_rate=0.001): import keras.backend as K K.set_value(self.train_net.optimizer.lr, learning_rate) frames = len(last_observations) self.counter += frames # ----- values, policy = self.train_net.predict([last_observations, self.unroll]) # ----- self.targets.fill(0.) adventage = rewards - values.flatten() self.targets[self.unroll, actions] = 1. # ----- loss = self.train_net.train_on_batch([last_observations, adventage], [rewards, self.targets]) entropy = np.mean(-policy * np.log(policy + 0.00000001)) self.pol_loss.append(loss[2]) self.val_loss.append(loss[1]) self.entropy.append(entropy) self.values.append(np.mean(values)) min_val, max_val, avg_val = min(self.values), max(self.values), np.mean(self.values) print('\rFrames: %8d; Policy-Loss: %10.6f; Avg: %10.6f ' '--- Value-Loss: %10.6f; Avg: %10.6f ' '--- Entropy: %7.6f; Avg: %7.6f ' '--- V-value; Min: %6.3f; Max: %6.3f; Avg: %6.3f' % ( self.counter, loss[2], np.mean(self.pol_loss), loss[1], np.mean(self.val_loss), entropy, np.mean(self.entropy), min_val, max_val, avg_val), end='') # ----- self.swap_counter -= frames if self.swap_counter < 0: self.swap_counter += self.swap_freq return True return False
Example #29
Source File: train.py From async-rl with MIT License | 5 votes |
def policy_loss(adventage=0., beta=0.01): from keras import backend as K def loss(y_true, y_pred): return -K.sum(K.log(K.sum(y_true * y_pred, axis=-1) + K.epsilon()) * K.flatten(adventage)) + \ beta * K.sum(y_pred * K.log(y_pred + K.epsilon())) return loss
Example #30
Source File: models.py From voxelmorph with GNU General Public License v3.0 | 5 votes |
def _log_layer_wrap(reg=K.epsilon()): def _log_layer(tens): return K.log(tens + reg) return _log_layer # def _global_max_nd(x): # return K.exp(x)