def beta(self, class_weight): """See super class.""" max_class_weight = self.max_class_weight(class_weight, self.dtype) delta = _ops.convert_to_tensor_v2(self.delta, dtype=self.dtype) return self.C * max_class_weight / (delta * tf.constant(2, dtype=self.dtype)) + \ self.reg_lambda
def radius(self): """Radius, R, of the hypothesis space W. W is a convex set that forms the hypothesis space. Returns: radius """ return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def lipchitz_constant(self, class_weight): # pylint: disable=unused-argument """Lipchitz constant, L. Args: class_weight: class weights used Returns: L """ return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def beta(self, class_weight): # pylint: disable=unused-argument """Smoothness, beta. Args: class_weight: the class weights as scalar or 1d tensor, where its dimensionality is equal to the number of outputs. Returns: Beta """ return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def max_class_weight(self, class_weight, dtype): """The maximum weighting in class weights (max value) as a scalar tensor. Args: class_weight: class weights used dtype: the data type for tensor conversions. Returns: maximum class weighting as tensor scalar """ class_weight = _ops.convert_to_tensor_v2(class_weight, dtype) return tf.math.reduce_max(class_weight)
def call(self, y_true, y_pred): """Computes loss. Args: y_true: Ground truth values. One hot encoded using -1 and 1. y_pred: The predicted values. Returns: Loss values per sample. """ h = self.delta z = y_pred * y_true one = tf.constant(1, dtype=self.dtype) four = tf.constant(4, dtype=self.dtype) if z > one + h: # pylint: disable=no-else-return return _ops.convert_to_tensor_v2(0, dtype=self.dtype) elif tf.math.abs(one - z) <= h: return one / (four * h) * tf.math.pow(one + h - z, 2) return one - z
def gamma(self): """Returns strongly convex parameter, gamma.""" return _ops.convert_to_tensor_v2(1, dtype=tf.float32)
def calculate_class_weights(self, class_weights=None, class_counts=None, num_classes=None): """Calculates class weighting to be used in training. Args: class_weights: str specifying type, array giving weights, or None. class_counts: If class_weights is not None, then an array of the number of samples for each class num_classes: If class_weights is not None, then the number of classes. Returns: class_weights as 1D tensor, to be passed to model's fit method. """ # Value checking class_keys = ['balanced'] is_string = False if isinstance(class_weights, str): is_string = True if class_weights not in class_keys: raise ValueError('Detected string class_weights with ' 'value: {0}, which is not one of {1}.' 'Please select a valid class_weight type' 'or pass an array'.format( class_weights, class_keys)) if class_counts is None: raise ValueError('Class counts must be provided if using ' 'class_weights=%s' % class_weights) class_counts_shape = tf.Variable(class_counts, trainable=False, dtype=self._dtype).shape if len(class_counts_shape) != 1: raise ValueError('class counts must be a 1D array.' 'Detected: {0}'.format(class_counts_shape)) if num_classes is None: raise ValueError('num_classes must be provided if using ' 'class_weights=%s' % class_weights) elif class_weights is not None: if num_classes is None: raise ValueError('You must pass a value for num_classes if ' 'creating an array of class_weights') # performing class weight calculation if class_weights is None: class_weights = 1 elif is_string and class_weights == 'balanced': num_samples = sum(class_counts) weighted_counts = tf.dtypes.cast( tf.math.multiply(num_classes, class_counts), self._dtype) class_weights = (tf.Variable(num_samples, dtype=self._dtype) / tf.Variable(weighted_counts, dtype=self._dtype)) else: class_weights = _ops.convert_to_tensor_v2(class_weights) if len(class_weights.shape) != 1: raise ValueError( 'Detected class_weights shape: {0} instead of ' '1D array'.format(class_weights.shape)) if class_weights.shape[0] != num_classes: raise ValueError( 'Detected array length: {0} instead of: {1}'.format( class_weights.shape[0], num_classes)) return class_weights