ganpdfs package¶
Subpackages¶
Submodules¶
ganpdfs.custom module¶
-
ganpdfs.custom.
wasserstein_loss
(y_true, y_pred)¶ Compute the wasserstein loss from the true and predictions. The loss function is implemented by multiplying the expected label for each sample by the predicted score (element wise), then calculating the mean. For more details, refer to: https://arxiv.org/abs/1506.05439
-
ganpdfs.custom.
get_optimizer
(optimizer)¶ Overwrite keras default parameters for optimizer.
- Parameters
optimizer (dict) – Dictionary containing information on the optimizer.
- Returns
Optimizer.
- Return type
tf.keras.optimizers
-
ganpdfs.custom.
get_activation
(model_params)¶ Extract activation functions from the input parameters. This is necessary for advanced activation functions.
- Parameters
model_params (dict) – Dictionary containing information on the Discriminator architecture.
- Returns
Activation function.
- Return type
tf.keras.activations
-
ganpdfs.custom.
get_init
(kinit_name)¶ Get keras kernel initializers from runcadr.
-
ganpdfs.custom.
save_ckpt
(generator, critic, adversarial)¶ Save the training information into a file. This includes but not limited to the information on the wieghts and the biases of the given network. The GANs model is a combination of three different neural networks (generator, critic/discriminator, adversarial) and the information on each one of them are saved.
For more information on the constructor Checkpoint from the module tensorflow.train, refer to https://www.tensorflow.org/api_docs/python/tf/train/Checkpoint
- Parameters
generator (ganpdfs.model.WassersteinGanModel.generator) – generator neural network
critic (ganpdfs.model.WassersteinGanModel.critic) – critic/discriminator neural network
adversarial (ganpdfs.model.WassersteinGanModel.adversarial) – adversarial neural network
- Returns
A load status object, which can be used to make assertions about
the status of a checkpoint restoration
-
ganpdfs.custom.
turn_on_training
(critic, generator)¶
-
ganpdfs.custom.
load_generator
(model_name)¶ Load a saved/trained keras model from a folder.
- Parameters
model_name (tf.keras.Model) – Name of the saved folder.
- Returns
Saved generator model.
- Return type
tf.keras.Model
-
class
ganpdfs.custom.
WeightsClipConstraint
(value)¶ Bases:
tensorflow.python.keras.constraints.Constraint
Put constraints on the weights of a given layer.
- Parameters
value (float) – Value to which the weights will be bounded on.
- Returns
Constraint class.
- Return type
tf.keras.constraints
-
get_config
()¶
-
class
ganpdfs.custom.
GenKinit
(valmin=- 1, valmax=1)¶ Bases:
tensorflow.python.keras.initializers.initializers_v2.Initializer
Custom kernel initialization.
-
get_config
()¶ Returns the configuration of the initializer as a JSON-serializable dict.
- Returns
A JSON-serializable Python dict.
-
-
class
ganpdfs.custom.
GradientPenalty
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Calculates the gradient penalty loss for a batch of “averaged” samples. In Improved WGANs, the 1-Lipschitz constraint is enforced by adding a term to the loss function that penalizes the network if the gradient norm moves away from 1. However, it is impossible to evaluate this function at all points in the input space. The compromise used in the paper is to choose random points on the lines between real and generated samples, and check the gradients at these points. Note that it is the gradient w.r.t. the input averaged samples, not the weights of the discriminator, that we’re penalizing! In order to evaluate the gradients, we must first run samples through the generator and evaluate the loss. Then we get the gradients of the discri- minator w.r.t. the input averaged samples. The l2 norm and penalty can then be calculated for this gradient. Note that this loss function requires the original averaged samples as input, but Keras only supports passing y_true and y_pred to loss functions. To get around this, we make a partial() of the function with the average argument, and use that for model training.
-
call
(inputs)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
-
class
ganpdfs.custom.
AddLatent
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Keras layer that inherates from the keras Layer class. This layer class basically expands the input latent space tensors to the generator.
- Parameters
latent_vector (np.array) – Input latent vector.
-
call
(inputs)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
class
ganpdfs.custom.
ExpLatent
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Keras layer that inherates from the keras Layer class. This layer class basically expands the input latent space tensors to the generator.
- Parameters
-
build
(input_shape)¶ Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
-
call
(inputs)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
class
ganpdfs.custom.
GenDense
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Custom layer that inherates from the keras Layer class. This layer class is proper to the Generator and takes the input parameters from the input runcard which contains all the parameters for the layer.
- Parameters
-
build
(input_shape)¶ Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
-
call
(inputs)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
class
ganpdfs.custom.
ExtraDense
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Keras layer that inherates from the keras Layer class. This layer class takes the input parameters from the input runcard which contains all the parameters for the layer.
- Parameters
-
build
(input_shape)¶ Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
-
call
(inputs)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
class
ganpdfs.custom.
ConvolutePDF
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Convolute the output of the previous layer with a subsample of the input/prior replica.
- Parameters
pdf (np.array) – Array of PDF grids
-
compute_output_shape
(input_shape)¶ Computes the output shape of the layer.
If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.
- Parameters
input_shape – Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns
An input shape tuple.
-
call
(previous_layer)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
class
ganpdfs.custom.
ConvXgrid
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Convolute the output of the previous layer with the input x-grid.
-
build
(input_shape)¶ Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
-
call
(x)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
-
compute_output_shape
(input_shape)¶ Computes the output shape of the layer.
If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.
- Parameters
input_shape – Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns
An input shape tuple.
-
-
class
ganpdfs.custom.
PreprocessFit
(*args, **kwargs)¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
Add preprocessing to the output of the previous layer. This is expected to assure the PDF-like behavior of the generated samples.
- Parameters
xval (np.array) – Array of x-grid
-
build
(input_shape)¶ Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
-
compute_output_shape
(input_shape)¶ Computes the output shape of the layer.
If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.
- Parameters
input_shape – Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns
An input shape tuple.
-
call
(pdf)¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
- Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
- Returns
A tensor or list/tuple of tensors.
ganpdfs.filetrials module¶
-
ganpdfs.filetrials.
space_eval_trial
(space, trial)¶ space_eval_trial.
- Parameters
space – space
trial – trial
-
class
ganpdfs.filetrials.
FileTrials
(replica_path, log=None, parameters=None, exp_key=None, refresh=True)¶ Bases:
hyperopt.base.Trials
FileTrials.
-
refresh
()¶ refresh.
-
new_trial_ids
(N)¶ new_trial_ids.
- Parameters
N – N
-
new_trial_docs
(tids, specs, results, miscs)¶ new_trial_docs.
- Parameters
tids – tids
specs – specs
results – results
miscs – miscs
-
ganpdfs.hyperscan module¶
ganpdfs.model module¶
-
class
ganpdfs.model.
WGanModel
(pdf, params)¶ Bases:
object
GAN class that represents the models in terms of Deep Neural Networks. It includes 3 Models: the Genator, the Discriminator, and the Adversarial Model which controls the training of the Generator. Apart from the Adversarial Model, the Generator and the Discriminator are not compiled directly.
- Parameters
-
generator_model
()¶ Generator Model based on a Deep Neural Network. It takes as input a latent space vector and gives as output a PDF grid sampled on a discrete x-grid.
-
critic_model
()¶ Discriminator/Critic model based on Deep Neural Networks. It takes as input a PDF grid (which could be the real or the synthetic). It gives as output logit values.
-
adversarial_model
(generator, critic)¶ Adversarial model based on Deep Neural Networks. It controls the training of the Generator through the output of the Discriminator.
- Parameters
generator (tf.Model) – generator model
critic (tf.model) – critic model
-
class
ganpdfs.model.
DWGanModel
(pdf, params, noise_dim)¶ Bases:
object
GAN class that represents the models in terms of Deep Convolu- tion Neural Networks. It includes 3 Models: the Generator, the Discriminator, and the Adversarial Model which controls the training of the Generator. Apart from the Adversarial Model, the Generator and the Discriminator are not compiled directly.
- Parameters
-
generator_model
()¶ Generator Model based on Deep Concolutional Neural Networks. It takes as input a random noise vector and gives as output a PDF grid sampled on a discrete x-grid.
-
critic_model
()¶ Discriminator/Critic model based on Deep Convolutional Neural Networks. It takes as input a PDF grid (which could be the real or the synthetic). It gives as output logit values.
-
adversarial_model
(generator, critic)¶ Adversarial model based on Deep Convolutional Neural Networks. It controls the training of the Generator through the Discriminator.
- Parameters
generator (tf.Model) – generator model
critic (tf.model) – critic model
ganpdfs.pdformat module¶
ganpdfs.run module¶
ganpdfs.train module¶
ganpdfs.utils module¶
-
ganpdfs.utils.
do_nothing
(tensor)¶ Function that does nothing.
- Parameters
tensor (tf.tensor) – Tensor
-
ganpdfs.utils.
gan_summary
(critic, generator, adversarial)¶
-
ganpdfs.utils.
axes_width
(ax, lw=1)¶ Change width of axes. :param ax: :type ax: matplotlib.axes.Axes :param Figure’s axes: :param lw: :type lw: float :param Define width of axes:
-
ganpdfs.utils.
latent_noise
(pdf, rndgen, s=0.0)¶ Apply gaussian noise to the latent input.
- Parameters
pdf (np.array) – Input PDF to be noised
rndgen – random number generator
s (float) – Intensity of the noise
-
ganpdfs.utils.
latent_sampling
(pdf, nb_output, rndgen, nsx=0.0)¶ latent_sampling.
- Parameters
pdf (np.array) – Array/Grid of input PDFs
nb_output (int) – Total number of replica
rndgen – Random replica generator
-
ganpdfs.utils.
factorize_number
(number)¶ Factorize_number using Pollard’s rho algorithm. This takes a number a return a list of the factors.
Example
Given an integer 70, this can be factorized a produc of the following integers [7,5,2].
Notice that by definition, 1 is not included.
-
ganpdfs.utils.
construct_cnn
(number, nb_layer)¶ Factorize_number using Pollard’s rho algorithm that is defined by the factorize_number method. This is used in order to define the dimension of the strides for performing the convolution in the model class DCNNWassersteinGanModel.
The issue is the following: given a pair of two integers (m, n) such that n < m, how can we decompose m into n factors.
Example
Given a pair (70, 3), we have [7,5,2]
If m cannot be decomposed further, then the remaining cases are naturally set to 1.
Example
Given a pair (58, 4) we have [29, 2, 1, 1]
Noe that the final result is sorted.
-
ganpdfs.utils.
interpol
(fake_pdf, gan_grid, lhapdf_grid, mthd='Intperp1D')¶ Interpolate the generated output according to the x-grid in order to match with the LHAPDF grid-format. It uses the interpolate module from scipy. For more details, refere to https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
- Parameters
fake_pdf (np.array) – generated PDF replicas of shape (nb_repl, nb_flv, gan_grid)
gan_grid (np.array) – custom x-grid of shape (gan_grid,)
lhapdf_grid – lhapdf-like grid of shape (lhapdf_grid,)
- Returns
fake PDF replica of shape (nb_repl, nb_flv, gan_grid)
- Return type
np.array(float)
-
ganpdfs.utils.
smm
(prior, generated)¶ Similarity Metric Measure that measures the quality of the generated PDF replicas using the Fréchet Inception Distance (FID).
TODO: Check how the total/final FIDs is computed.