berry.objectives¶
berry.objectives module contains a list of loss functions and other evaluation methods for training or testing neural networks.
A small list of loss functions currently defined in berry are given below.
categorical_crossentropy |
Categorical crossentropy between an output tensor and a target tensor, where the target is a tensor of the same shape as the output. |
triplet_loss |
Triplet loss function. |
The following evaluation methods are provided to montior the train or test performance of the neural network.
accuracy |
Multi-class zero-one accuracy |
Helper function¶
-
braid.berry.objectives.get_objective(key, output, target)¶ Helper function to retrieve the appropriate objective function.
- key : string
- Name of the objective function - “categorical_crossentropy”, “accuracy”, etc.
- function
- The appropriate function given the
key.
>>> from berry.objectives import get_objective >>> # assume: `target` - output Tensor to predict, `output` - predicted >>> # Tensor of the neural network >>> accuracy_op = get_objective("accuracy", output, target)
Examples¶
You are encouraged to use the helper function get_objective(). Alternatively, you can use the objective functions directly,
>>> from berry.objectives import categorical_crossentropy
>>> # assume: `target` - output Tensor to predict, `output` - predicted Tensor
>>> # of the neural network
>>> loss_op = categorical_crossentropy(output, target)
Loss Functions¶
-
braid.berry.objectives.categorical_crossentropy(output, target)¶ Categorical crossentropy between an output tensor and a target tensor, where the target is a tensor of the same shape as the output.
- output :
- Tensor containing the predicted class probabilities of the neural network.
- target :
- Tensor containing the true classes for the corresponding inputs.
tf.Tensortf.Tensortf.Tensor- Symbolic tensorflow Tensor which performs the loss calculation.
This function expects
outputto contain class probabilities instead of class prediction scores. Typically,activations.softmax()activation should be used for the layer producingoutput.
-
braid.berry.objectives.triplet_loss(Xa, Xp, Xn, margin=1.0)¶ Triplet loss function.
This function is useful for generating embeddings for images/inputs such that similar inputs lie close in the embedding and different objects lie further away. It takes 3 inputs - anchor (
Xa), positive sample (Xp) and negative sample (Xn).XaandXpare known to be similar inputs whereasXaandXnare different. For more details refer to [1].- output :
- Tensor containing the predicted class probabilities of the neural network.
- target :
- Tensor containing the true classes for the corresponding inputs.
tf.Tensortf.Tensortf.Tensor- Symbolic tensorflow Tensor which performs the loss calculation.
[1] Florian Schroff, Dmitry Kalenichenko, James Philbin; “FaceNet: A Unified Embedding for Face Recognition and Clustering”, The IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2015, pp. 815-823.
Evaluation Methods¶
-
braid.berry.objectives.accuracy(output, target)¶ Multi-class zero-one accuracy
The fraction of samples in the given mini-batch with the correct predicted class.
- output :
- Tensor containing the predicted class probabilities of the neural network.
- target :
- Tensor containing the true classes for the corresponding inputs.
tf.Tensortf.Tensortf.Tensor- Symbolic tensorflow Tensor which performs the accuracy calculation.