art.estimators.encoding
¶
Encoder API.
Mixin Base Class Encoder¶
TensorFlow Encoder¶
-
class
art.estimators.encoding.
TensorFlowEncoder
(input_ph: tf.Placeholder, model: tf.Tensor, loss: Optional[tf.Tensor] = None, sess: Optional[tf.compat.v1.Session] = None, channels_first: bool = False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0, 1), feed_dict: Optional[Dict[Any, Any]] = None)¶ This class implements an encoder model using the TensorFlow framework.
-
__init__
(input_ph: tf.Placeholder, model: tf.Tensor, loss: Optional[tf.Tensor] = None, sess: Optional[tf.compat.v1.Session] = None, channels_first: bool = False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0, 1), feed_dict: Optional[Dict[Any, Any]] = None)¶ Initialization specific to encoder estimator implementation in TensorFlow.
- Parameters
input_ph – The input placeholder.
model – TensorFlow model, neural network or other.
loss – The loss function for which to compute gradients. This parameter is necessary when training the model and when computing gradients w.r.t. the loss function.
sess – Computation session.
channels_first (
bool
) – Set channels first or last.clip_values – Tuple of the form (min, max) of floats or np.ndarray representing the minimum and maximum values allowed for features. If floats are provided, these will be used as the range of all features. If arrays are provided, each value will be considered the bound for a feature, thus the shape of clip values needs to match the total number of features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
feed_dict – A feed dictionary for the session run evaluating the classifier. This dictionary includes all additionally required placeholders except the placeholders defined in this class.
-
property
channel_index
¶ - Returns
Index of the axis containing the color channels in the samples x.
-
property
channels_first
¶ - Returns
Boolean to indicate index of the color channels in the sample x.
-
property
clip_values
¶ Return the clip values of the input samples.
- Returns
Clip values (min, max).
-
property
encoding_length
¶ Returns the length of the encoding size output.
- Returns
The length of the encoding size output.
-
fit
(x: np.ndarray, y: np.ndarray, batch_size: int = 128, nb_epochs: int = 10, **kwargs) → None¶ Do nothing.
-
fit_generator
(generator: DataGenerator, nb_epochs: int = 20, **kwargs) → None¶ Fit the estimator using a generator yielding training batches. Implementations can provide framework-specific versions of this function to speed-up computation.
- Parameters
generator – Batch generator providing (x, y) for each epoch.
nb_epochs (
int
) – Number of training epochs.
-
get_activations
(x: np.ndarray, layer: Union[int, str], batch_size: int, framework: bool = False) → np.ndarray¶ Do nothing.
-
get_params
() → Dict[str, Any]¶ Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
-
property
input_shape
¶ Return the shape of one input sample.
- Returns
Shape of one input sample.
-
property
layer_names
¶ Return the names of the hidden layers in the model, if applicable.
- Returns
The names of the hidden layers in the model, input and output layers are ignored.
Warning
layer_names tries to infer the internal structure of the model. This feature comes with no guarantees on the correctness of the result. The intended order of the layers tries to match their order in the model, but this is not guaranteed either.
-
property
learning_phase
¶ The learning phase set by the user. Possible values are True for training or False for prediction and None if it has not been set by the library. In the latter case, the library does not do any explicit learning phase manipulation and the current value of the backend framework is used. If a value has been set by the user for this property, it will impact all following computations for model fitting, prediction and gradients.
- Returns
Learning phase.
-
loss
(x: np.ndarray, y: np.ndarray, **kwargs) → np.ndarray¶ Compute the loss of the neural network for samples x.
- Parameters
x – Samples of shape (nb_samples, nb_features) or (nb_samples, nb_pixels_1, nb_pixels_2, nb_channels) or (nb_samples, nb_channels, nb_pixels_1, nb_pixels_2).
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape (nb_samples,).
- Returns
Loss values.
- Return type
Format as expected by the model
-
loss_gradient
(x: np.ndarray, y: np.ndarray, **kwargs) → np.ndarray¶ No gradients to compute for this method; do nothing.
-
property
model
¶ Return the model.
- Returns
The model.
-
predict
(x: np.ndarray, batch_size: int = 128, **kwargs)¶ Perform prediction for a batch of inputs.
- Parameters
x – Test set.
batch_size (
int
) – Batch size.
- Returns
Array of encoding predictions of shape (num_inputs, encoding_length).
-
property
sess
¶ Get current TensorFlow session.
- Returns
The current TensorFlow session.
-
set_learning_phase
(train: bool) → None¶ Do nothing.
-
set_params
(**kwargs) → None¶ Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
-