foolbox.utils

foolbox.utils.softmax(logits)[source]

Transforms predictions into probability values.

Parameters:
logits : array_like

The logits predicted by the model.

Returns:
numpy.ndarray

Probability values corresponding to the logits.

foolbox.utils.crossentropy(label, logits)[source]

Calculates the cross-entropy.

Parameters:
logits : array_like

The logits predicted by the model.

label : int

The label describing the target distribution.

Returns:
float

The cross-entropy between softmax(logits) and onehot(label).

foolbox.utils.batch_crossentropy(label, logits)[source]

Calculates the cross-entropy for a batch of logits.

Parameters:
logits : array_like

The logits predicted by the model for a batch of inputs.

label : int

The label describing the target distribution.

Returns:
np.ndarray

The cross-entropy between softmax(logits[i]) and onehot(label) for all i.

foolbox.utils.binarize(x, values, threshold=None, included_in='upper')[source]

Binarizes the values of x.

Parameters:
values : tuple of two floats

The lower and upper value to which the inputs are mapped.

threshold : float

The threshold; defaults to (values[0] + values[1]) / 2 if None.

included_in : str

Whether the threshold value itself belongs to the lower or upper interval.

foolbox.utils.imagenet_example(shape=(224, 224), data_format='channels_last')[source]

Returns an example image and its imagenet class label.

Parameters:
shape : list of integers

The shape of the returned image.

data_format : str

“channels_first” or “channels_last”

Returns:
image : array_like

The example image.

label : int

The imagenet label associated with the image.

foolbox.utils.onehot_like(a, index, value=1)[source]

Creates an array like a, with all values set to 0 except one.

Parameters:
a : array_like

The returned one-hot array will have the same shape and dtype as this array

index : int

The index that should be set to value

value : single value compatible with a.dtype

The value to set at the given index

Returns:
numpy.ndarray

One-hot array with the given value at the given location and zeros everywhere else.