def test_weighted_masked_objective(): a = Input(shape=(3,), name='input_a') # weighted_masked_objective def mask_dummy(y_true=None, y_pred=None, weight=None): return K.placeholder(y_true.shape) weighted_function = training_utils.weighted_masked_objective( losses.categorical_crossentropy) weighted_function(a, a, None)
def __init__(self, session, max_seq_length, num_symbols, batch_generator, metrics_name_postfix="unknown", inspector=None, num_samples_to_inspect=5, one_hot_encoding=True, **kwargs): super().__init__(batch_generator, metrics_name_postfix, **kwargs) self._sess = session self._one_hot_encoding = one_hot_encoding if self._one_hot_encoding: self._target_batch_placeholder = K.placeholder( (None, max_seq_length + 2, num_symbols)) else: self._target_batch_placeholder = K.placeholder( (None, max_seq_length + 2, 1)) self._output_batch_placeholder = K.placeholder( (None, max_seq_length + 2, num_symbols)) self._sample_weights_batch_placeholder = K.placeholder( (None, max_seq_length + 2)) loss = categorical_crossentropy if self._one_hot_encoding else sparse_categorical_crossentropy self._weighted_categorical_cross_entropy_op = weighted_masked_objective( loss)(self._target_batch_placeholder, self._output_batch_placeholder, self._sample_weights_batch_placeholder) metric = categorical_accuracy if self._one_hot_encoding else sparse_categorical_accuracy self._weighted_categorical_accuracy_op = weighted_masked_objective( metric)(self._target_batch_placeholder, self._output_batch_placeholder, self._sample_weights_batch_placeholder) self._inspector = inspector self._num_samples_to_inspect = num_samples_to_inspect
def test_loss_masking(): weighted_loss = weighted_masked_objective(losses.get('mae')) shape = (3, 4, 2) x = np.arange(24).reshape(shape) y = 2 * x # Normally the trailing 1 is added by standardize_weights weights = np.ones((3, )) mask = np.ones((3, 4)) mask[1, 0] = 0 out = K.eval( weighted_loss(K.variable(x), K.variable(y), K.variable(weights), K.variable(mask)))
def test_loss_masking(): weighted_loss = weighted_masked_objective(losses.get('mae')) shape = (3, 4, 2) x = np.arange(24).reshape(shape) y = 2 * x # Normally the trailing 1 is added by standardize_weights weights = np.ones((3,)) mask = np.ones((3, 4)) mask[1, 0] = 0 out = K.eval(weighted_loss(K.variable(x), K.variable(y), K.variable(weights), K.variable(mask)))