def train_step(self, g_input, real_input): with tf.GradientTape() as g_tape,\ tf.GradientTape() as d_tape: # Feed forward fake_input = self.G(g_input) pred_fake = self.D(fake_input) pred_real = self.D(real_input) # Calculate losses d_loss = self.discriminator_loss(pred_fake, pred_real) g_loss = self.generator_loss(pred_fake) # Accuracy fake_accuracy = tf.math.reduce_mean( binary_accuracy(tf.zeros_like(pred_fake), pred_fake)) real_accuracy = tf.math.reduce_mean( binary_accuracy(tf.ones_like(pred_real), pred_real)) # backprop gradients gradient_g = g_tape.gradient(g_loss, self.G.trainable_variables) gradient_d = d_tape.gradient(d_loss, self.D.trainable_variables) gradient_g_l1_norm = [ tf.norm(gradient).numpy() for gradient in gradient_g ] self.g_gradients.append(gradient_g_l1_norm) # update weights self.G_optimizer.apply_gradients( zip(gradient_g, self.G.trainable_variables)) self.D_optimizer.apply_gradients( zip(gradient_d, self.D.trainable_variables)) return g_loss, d_loss, fake_accuracy, real_accuracy
def _generic_accuracy(y_true, y_pred): if K.int_shape(y_pred)[1] == 1: return binary_accuracy(y_true, y_pred) if K.int_shape(y_true)[-1] == 1: return sparse_categorical_accuracy(y_true, y_pred) return categorical_accuracy(y_true, y_pred)
def be_binary_accuracy(y_true, y_pred): class_nums = y_pred.shape[-1] // 2 y_true = y_true[..., class_nums:] y_pred = y_pred[..., class_nums:] bi_acc = binary_accuracy(y_true, y_pred) return bi_acc
def optimal_accuracy(self, X_test, y_test): # Required when some distributions are inherently `float32` such as # the `MixtureSameFamily`. # TODO: Add flexibility for whether to cast to `float64`. y_pred = tf.cast(tf.squeeze(self.prob(X_test)), dtype=tf.float64) return binary_accuracy(y_test, y_pred)
def my_binary_accuracy(y_true, y_pred): # print("my_binary_accuracy") # print(f"y_true:{y_true}, y_pred:{y_pred}") y_true_, y_pred_ = tarnsform_metrics(y_true, y_pred) # print(f"y_true_:{y_true_}, y_pred_:{y_pred_}") accuracy = binary_accuracy(y_true_, y_pred_) return accuracy
def accuracy(y_true, y_pred): """ Computes the accuracy of the predictions. """ # Notice that `y_true` is 0 whenever two images are not the same and 1 # otherwise, but `y_pred` is the opposite. The closer `y_pred` is to 0, # the shorter the distance between both images, therefore the more likely # it is that they are the same image. To correctly compute the accuracy we # need to substract `y_pred` from 1 so both vectors are comparable. return metrics.binary_accuracy(y_true, 1 - y_pred)
def treatment_accuracy(concat_true, concat_pred): """ Returns keras' binary_accuracy between treatment and prediction of propensity. Args: - concat_true (tf.tensor): tensor of true samples, with shape (n_samples, 2) Each row in concat_true is comprised of (y, treatment) - concat_pred (tf.tensor): tensor of predictions, with shape (n_samples, 4) Each row in concat_pred is comprised of (y0, y1, propensity, epsilon) Returns: - (float): binary accuracy """ t_true = concat_true[:, 1] t_pred = concat_pred[:, 2] return binary_accuracy(t_true, t_pred)
def obj_acc(y_true, y_pred): y_true = tf.reshape(y_true, (-1, *grid_shape, 1, 5 + class_num)) # N*S*S*1*5+C y_pred = tf.reshape( y_pred, (-1, *grid_shape, bbox_num, 5 + class_num)) # N*S*S*B*5+C c_true = y_true[..., 4] # N*S*S*1 c_pred = tf.reduce_max( y_pred[..., 4], # N*S*S*B axis=-1, keepdims=True) # N*S*S*1 bi_acc = binary_accuracy(c_true, c_pred) return bi_acc
def ia_acc(y_true, y_pred): binary_truth = y_true[:, -1] binary_pred = y_pred[:, -1] return binary_accuracy(binary_truth, binary_pred)
def invasion_acc(y_true, y_pred): binary_truth = y_true[:, -2] + y_true[:, -1] binary_pred = y_pred[:, -2] + y_pred[:, -1] return binary_accuracy(binary_truth, binary_pred)
def personal_signature_accuracy(y_true, y_pred): return metrics.binary_accuracy( *_binarize_pred_tensors('personal_signature', y_true, y_pred))
def log_data_accuracy(y_true, y_pred): return metrics.binary_accuracy( *_binarize_pred_tensors('log_data', y_true, y_pred))
def paragraph_accuracy(y_true, y_pred): return metrics.binary_accuracy( *_binarize_pred_tensors('paragraph', y_true, y_pred))
def quotation_accuracy(y_true, y_pred): return metrics.binary_accuracy( *_binarize_pred_tensors('quotation', y_true, y_pred))
def acc(y_true, y_pred): corr = binary_accuracy( y_true, Activation('sigmoid', name='activation_output')(y_pred)) return K.mean(corr)
def compute_metrics(truth, preds, _type='binary_accuracy'): binary_accuracy(truth, preds) if _type == 'binary_accuracy': return np.mean(tf.keras.backend.get_session().run(binary_accuracy(truth, preds))) else: raise NotImplementedError(f'{_type} metrics is not implemented')