code stringlengths 66 870k | docstring stringlengths 19 26.7k | func_name stringlengths 1 138 | language stringclasses 1
value | repo stringlengths 7 68 | path stringlengths 5 324 | url stringlengths 46 389 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
def __init__(self,
args,
backbone):
"""Contains the encoder model, the loss function,
loading of datasets, train and evaluation routines
to implement IIC unsupervised clustering via mutual
information maximization
Arguments:
... | Contains the encoder model, the loss function,
loading of datasets, train and evaluation routines
to implement IIC unsupervised clustering via mutual
information maximization
Arguments:
args : Command line arguments to indicate choice
of batch siz... | __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def build_model(self):
"""Build the n_heads of the IIC model
"""
inputs = Input(shape=self.train_gen.input_shape, name='x')
x = self.backbone(inputs)
x = Flatten()(x)
# number of output heads
outputs = []
for i in range(self.args.heads):
name =... | Build the n_heads of the IIC model
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def mi_loss(self, y_true, y_pred):
"""Mutual information loss computed from the joint
distribution matrix and the marginals
Arguments:
y_true (tensor): Not used since this is
unsupervised learning
y_pred (tensor): stack of softmax predictions for
... | Mutual information loss computed from the joint
distribution matrix and the marginals
Arguments:
y_true (tensor): Not used since this is
unsupervised learning
y_pred (tensor): stack of softmax predictions for
the Siamese latent vectors (Z and Z... | mi_loss | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def train(self):
"""Train function uses the data generator,
accuracy computation, and learning rate
scheduler callbacks
"""
accuracy = AccuracyCallback(self)
lr_scheduler = LearningRateScheduler(lr_schedule,
verbose=1)
... | Train function uses the data generator,
accuracy computation, and learning rate
scheduler callbacks
| train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def load_eval_dataset(self):
"""Pre-load test data for evaluation
"""
(_, _), (x_test, self.y_test) = self.args.dataset.load_data()
image_size = x_test.shape[1]
x_test = np.reshape(x_test,[-1, image_size, image_size, 1])
x_test = x_test.astype('float32') / 255
x_e... | Pre-load test data for evaluation
| load_eval_dataset | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def eval(self):
"""Evaluate the accuracy of the current model weights
"""
y_pred = self._model.predict(self.x_test)
print("")
# accuracy per head
for head in range(self.args.heads):
if self.args.heads == 1:
y_head = y_pred
else:
... | Evaluate the accuracy of the current model weights
| eval | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/iic-13.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/iic-13.5.1.py | MIT |
def sample(joint=True,
mean=[0, 0],
cov=[[1, 0.5], [0.5, 1]],
n_data=1000000):
"""Helper function to obtain samples
fr a bivariate Gaussian distribution
Arguments:
joint (Bool): If joint distribution is desired
mean (list): The mean values of the 2D Gau... | Helper function to obtain samples
fr a bivariate Gaussian distribution
Arguments:
joint (Bool): If joint distribution is desired
mean (list): The mean values of the 2D Gaussian
cov (list): The covariance matrix of the 2D Gaussian
n_data (int): Number of samples fr 2D Gaussi... | sample | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def compute_mi(cov_xy=0.5, n_bins=100):
"""Analytic computation of MI using binned
2D Gaussian
Arguments:
cov_xy (list): Off-diagonal elements of covariance
matrix
n_bins (int): Number of bins to "quantize" the
continuous 2D Gaussian
"""
cov=[[1, cov_xy]... | Analytic computation of MI using binned
2D Gaussian
Arguments:
cov_xy (list): Off-diagonal elements of covariance
matrix
n_bins (int): Number of bins to "quantize" the
continuous 2D Gaussian
| compute_mi | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def __init__(self,
args,
input_dim=1,
hidden_units=16,
output_dim=1):
"""Learn to compute MI using MINE (Algorithm 13.7.1)
Arguments:
args : User-defined arguments such as off-diagonal
elements of covariance... | Learn to compute MI using MINE (Algorithm 13.7.1)
Arguments:
args : User-defined arguments such as off-diagonal
elements of covariance matrix, batch size,
epochs, etc
input_dim (int): Input size dimension
hidden_units (int): Number of hidden ... | __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def build_model(self,
input_dim,
hidden_units,
output_dim):
"""Build a simple MINE model
Arguments:
See class arguments.
"""
inputs1 = Input(shape=(input_dim), name="x")
inputs2 = Input(shape=(input_... | Build a simple MINE model
Arguments:
See class arguments.
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def mi_loss(self, y_true, y_pred):
""" MINE loss function
Arguments:
y_true (tensor): Not used since this is
unsupervised learning
y_pred (tensor): stack of predictions for
joint T(x,y) and marginal T(x,y)
"""
size = self.args.batc... | MINE loss function
Arguments:
y_true (tensor): Not used since this is
unsupervised learning
y_pred (tensor): stack of predictions for
joint T(x,y) and marginal T(x,y)
| mi_loss | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def train(self):
"""Train MINE to estimate MI between
X and Y of a 2D Gaussian
"""
optimizer = Adam(lr=0.01)
self._model.compile(optimizer=optimizer,
loss=self.mi_loss)
plot_loss = []
cov=[[1, self.args.cov_xy], [self.args.cov_xy, ... | Train MINE to estimate MI between
X and Y of a 2D Gaussian
| train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def __init__(self,
latent_dim=10,
n_classes=10):
"""A simple MLP-based linear classifier.
A linear classifier is an MLP network
without non-linear activation like ReLU.
This can be used as a substitute to linear
assignment algori... | A simple MLP-based linear classifier.
A linear classifier is an MLP network
without non-linear activation like ReLU.
This can be used as a substitute to linear
assignment algorithm.
Arguments:
latent_dim (int): Latent vector dimensionality
... | __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def build_model(self, latent_dim, n_classes):
"""Linear classifier model builder.
Arguments: (see class arguments)
"""
inputs = Input(shape=(latent_dim,), name="cluster")
x = Dense(256)(inputs)
outputs = Dense(n_classes,
activation='softmax',
... | Linear classifier model builder.
Arguments: (see class arguments)
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def train(self, x_test, y_test):
"""Linear classifier training.
Arguments:
x_test (tensor): Image fr test dataset
y_test (tensor): Corresponding image label
fr test dataset
"""
self._model.fit(x_test,
y_test,
... | Linear classifier training.
Arguments:
x_test (tensor): Image fr test dataset
y_test (tensor): Corresponding image label
fr test dataset
| train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def eval(self, x_test, y_test):
"""Linear classifier evaluation.
Arguments:
x_test (tensor): Image fr test dataset
y_test (tensor): Corresponding image label
fr test dataset
"""
self._model.fit(x_test,
y_test,
... | Linear classifier evaluation.
Arguments:
x_test (tensor): Image fr test dataset
y_test (tensor): Corresponding image label
fr test dataset
| eval | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def __init__(self,
args,
backbone):
"""Contains the encoder, SimpleMINE, and linear
classifier models, the loss function,
loading of datasets, train and evaluation routines
to implement MINE unsupervised clustering via mutual
inf... | Contains the encoder, SimpleMINE, and linear
classifier models, the loss function,
loading of datasets, train and evaluation routines
to implement MINE unsupervised clustering via mutual
information maximization
Arguments:
args : Command line argumen... | __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def build_model(self):
"""Build the MINE model unsupervised classifier
"""
inputs = Input(shape=self.train_gen.input_shape,
name="x")
x = self.backbone(inputs)
x = Flatten()(x)
y = Dense(self.latent_dim,
activation='linear',
... | Build the MINE model unsupervised classifier
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def train(self):
"""Train MINE to estimate MI between
X and Y (eg MNIST image and its transformed
version)
"""
accuracy = AccuracyCallback(self)
lr_scheduler = LearningRateScheduler(lr_schedule,
verbose=1)
call... | Train MINE to estimate MI between
X and Y (eg MNIST image and its transformed
version)
| train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/mine-13.8.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/mine-13.8.1.py | MIT |
def unsupervised_labels(y, yp, n_classes, n_clusters):
"""Linear assignment algorithm
Arguments:
y (tensor): Ground truth labels
yp (tensor): Predicted clusters
n_classes (int): Number of classes
n_clusters (int): Number of clusters
"""
assert n_classes == n_clusters... | Linear assignment algorithm
Arguments:
y (tensor): Ground truth labels
yp (tensor): Predicted clusters
n_classes (int): Number of classes
n_clusters (int): Number of clusters
| unsupervised_labels | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/utils.py | MIT |
def center_crop(image, crop_size=4):
"""Crop the image from the center
Argument:
crop_size (int): Number of pixels to crop
from each side
"""
height, width = image.shape[0], image.shape[1]
x = height - crop_size
y = width - crop_size
dx = dy = crop_size // 2
image = i... | Crop the image from the center
Argument:
crop_size (int): Number of pixels to crop
from each side
| center_crop | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/utils.py | MIT |
def lr_schedule(epoch):
"""Simple learning rate scheduler
Argument:
epoch (int): Which epoch
"""
lr = 1e-3
power = epoch // 400
lr *= 0.8**power
return lr | Simple learning rate scheduler
Argument:
epoch (int): Which epoch
| lr_schedule | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/utils.py | MIT |
def __init__(self, cfg, input_shape=(24, 24, 1)):
"""VGG network model creator to be used as backbone
feature extractor
Arguments:
cfg (dict): Summarizes the network configuration
input_shape (list): Input image dims
"""
self.cfg = cfg
self.in... | VGG network model creator to be used as backbone
feature extractor
Arguments:
cfg (dict): Summarizes the network configuration
input_shape (list): Input image dims
| __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/vgg.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/vgg.py | MIT |
def build_model(self):
"""Model builder uses a helper function
make_layers to read the config dict and
create a VGG network model
"""
inputs = Input(shape=self.input_shape, name='x')
x = VGG.make_layers(self.cfg, inputs)
self._model = Model(inputs, x, name... | Model builder uses a helper function
make_layers to read the config dict and
create a VGG network model
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/vgg.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/vgg.py | MIT |
def make_layers(cfg,
inputs,
batch_norm=True,
in_channels=1):
"""Helper function to ease the creation of VGG
network model
Arguments:
cfg (dict): Summarizes the network layer
configuration
... | Helper function to ease the creation of VGG
network model
Arguments:
cfg (dict): Summarizes the network layer
configuration
inputs (tensor): Input from previous layer
batch_norm (Bool): Whether to use batch norm
between Conv2D and... | make_layers | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter13-mi-unsupervised/vgg.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter13-mi-unsupervised/vgg.py | MIT |
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
... | Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
| lr_schedule | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter2-deep-networks/densenet-cifar10-2.4.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/densenet-cifar10-2.4.1.py | MIT |
def resnet_layer(inputs,
num_filters=16,
kernel_size=3,
strides=1,
activation='relu',
batch_normalization=True,
conv_first=True):
"""2D Convolution-Batch Normalization-Activation stack builder
Arguments:
... | 2D Convolution-Batch Normalization-Activation stack builder
Arguments:
inputs (tensor): input tensor from input image or previous layer
num_filters (int): Conv2D number of filters
kernel_size (int): Conv2D square kernel dimensions
strides (int): Conv2D square stride dimensions
... | resnet_layer | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter2-deep-networks/resnet-cifar10-2.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/resnet-cifar10-2.2.1.py | MIT |
def resnet_v1(input_shape, depth, num_classes=10):
"""ResNet Version 1 Model builder [a]
Stacks of 2 x (3 x 3) Conv2D-BN-ReLU
Last ReLU is after the shortcut connection.
At the beginning of each stage, the feature map size is halved
(downsampled) by a convolutional layer with strides=2, while
... | ResNet Version 1 Model builder [a]
Stacks of 2 x (3 x 3) Conv2D-BN-ReLU
Last ReLU is after the shortcut connection.
At the beginning of each stage, the feature map size is halved
(downsampled) by a convolutional layer with strides=2, while
the number of filters is doubled. Within each stage,
... | resnet_v1 | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter2-deep-networks/resnet-cifar10-2.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/resnet-cifar10-2.2.1.py | MIT |
def resnet_v2(input_shape, depth, num_classes=10):
"""ResNet Version 2 Model builder [b]
Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or
also known as bottleneck layer.
First shortcut connection per layer is 1 x 1 Conv2D.
Second and onwards shortcut connection is identity.
At the beginning... | ResNet Version 2 Model builder [b]
Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or
also known as bottleneck layer.
First shortcut connection per layer is 1 x 1 Conv2D.
Second and onwards shortcut connection is identity.
At the beginning of each stage,
the feature map size is halved (downs... | resnet_v2 | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter2-deep-networks/resnet-cifar10-2.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/resnet-cifar10-2.2.1.py | MIT |
def plot_results(models,
data,
batch_size=32,
model_name="autoencoder_2dim"):
"""Plots 2-dim latent values as scatter plot of digits
then, plot MNIST digits as function of 2-dim latent vector
Arguments:
models (list): encoder and decoder models... | Plots 2-dim latent values as scatter plot of digits
then, plot MNIST digits as function of 2-dim latent vector
Arguments:
models (list): encoder and decoder models
data (list): test data and label
batch_size (int): prediction batch size
model_name (string): which model is us... | plot_results | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter3-autoencoders/autoencoder-2dim-mnist-3.2.2.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter3-autoencoders/autoencoder-2dim-mnist-3.2.2.py | MIT |
def build_generator(inputs, labels, image_size):
"""Build a Generator Model
Inputs are concatenated before Dense layer.
Stack of BN-ReLU-Conv2DTranpose to generate fake images.
Output activation is sigmoid instead of tanh in orig DCGAN.
Sigmoid converges easily.
Arguments:
inputs (Laye... | Build a Generator Model
Inputs are concatenated before Dense layer.
Stack of BN-ReLU-Conv2DTranpose to generate fake images.
Output activation is sigmoid instead of tanh in orig DCGAN.
Sigmoid converges easily.
Arguments:
inputs (Layer): Input layer of the generator (the z-vector)
... | build_generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/cgan-mnist-4.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/cgan-mnist-4.3.1.py | MIT |
def build_discriminator(inputs, labels, image_size):
"""Build a Discriminator Model
Inputs are concatenated after Dense layer.
Stack of LeakyReLU-Conv2D to discriminate real from fake.
The network does not converge with BN so it is not used here
unlike in DCGAN paper.
Arguments:
inputs... | Build a Discriminator Model
Inputs are concatenated after Dense layer.
Stack of LeakyReLU-Conv2D to discriminate real from fake.
The network does not converge with BN so it is not used here
unlike in DCGAN paper.
Arguments:
inputs (Layer): Input layer of the discriminator (the image)
... | build_discriminator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/cgan-mnist-4.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/cgan-mnist-4.3.1.py | MIT |
def train(models, data, params):
"""Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly labelled real and fake images.
Adversarial is trained next with fake images pretending to be real.
Dis... | Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly labelled real and fake images.
Adversarial is trained next with fake images pretending to be real.
Discriminator inputs are conditioned by tra... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/cgan-mnist-4.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/cgan-mnist-4.3.1.py | MIT |
def plot_images(generator,
noise_input,
noise_class,
show=False,
step=0,
model_name="gan"):
"""Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
Arguments:
... | Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
Arguments:
generator (Model): The Generator Model for fake images generation
noise_input (ndarray): Array of z-vectors
show (bool): Whether to show plot or not
... | plot_images | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/cgan-mnist-4.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/cgan-mnist-4.3.1.py | MIT |
def build_generator(inputs, image_size):
"""Build a Generator Model
Stack of BN-ReLU-Conv2DTranpose to generate fake images
Output activation is sigmoid instead of tanh in [1].
Sigmoid converges easily.
Arguments:
inputs (Layer): Input layer of the generator
the z-vector)
... | Build a Generator Model
Stack of BN-ReLU-Conv2DTranpose to generate fake images
Output activation is sigmoid instead of tanh in [1].
Sigmoid converges easily.
Arguments:
inputs (Layer): Input layer of the generator
the z-vector)
image_size (tensor): Target size of one side... | build_generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/dcgan-mnist-4.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/dcgan-mnist-4.2.1.py | MIT |
def build_discriminator(inputs):
"""Build a Discriminator Model
Stack of LeakyReLU-Conv2D to discriminate real from fake.
The network does not converge with BN so it is not used here
unlike in [1] or original paper.
Arguments:
inputs (Layer): Input layer of the discriminator (the image)
... | Build a Discriminator Model
Stack of LeakyReLU-Conv2D to discriminate real from fake.
The network does not converge with BN so it is not used here
unlike in [1] or original paper.
Arguments:
inputs (Layer): Input layer of the discriminator (the image)
Returns:
discriminator (Model... | build_discriminator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/dcgan-mnist-4.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/dcgan-mnist-4.2.1.py | MIT |
def train(models, x_train, params):
"""Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly real and fake images.
Adversarial is trained next with fake images pretending to be real
Generate s... | Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly real and fake images.
Adversarial is trained next with fake images pretending to be real
Generate sample images per save_interval.
Argume... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/dcgan-mnist-4.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/dcgan-mnist-4.2.1.py | MIT |
def plot_images(generator,
noise_input,
show=False,
step=0,
model_name="gan"):
"""Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
Arguments:
generator (Model): Th... | Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
Arguments:
generator (Model): The Generator Model for
fake images generation
noise_input (ndarray): Array of z-vectors
show (bool): Whether to show plot ... | plot_images | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter4-gan/dcgan-mnist-4.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter4-gan/dcgan-mnist-4.2.1.py | MIT |
def train(models, data, params):
"""Train the discriminator and adversarial Networks
Alternately train discriminator and adversarial
networks by batch.
Discriminator is trained first with real and fake
images and corresponding one-hot labels.
Adversarial is trained next with fake images prete... | Train the discriminator and adversarial Networks
Alternately train discriminator and adversarial
networks by batch.
Discriminator is trained first with real and fake
images and corresponding one-hot labels.
Adversarial is trained next with fake images pretending
to be real and corresponding ... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter5-improved-gan/acgan-mnist-5.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter5-improved-gan/acgan-mnist-5.3.1.py | MIT |
def build_and_train_models():
"""Load the dataset, build ACGAN discriminator,
generator, and adversarial models.
Call the ACGAN train routine.
"""
# load MNIST dataset
(x_train, y_train), (_, _) = mnist.load_data()
# reshape data for CNN as (28, 28, 1) and normalize
image_size = x_train... | Load the dataset, build ACGAN discriminator,
generator, and adversarial models.
Call the ACGAN train routine.
| build_and_train_models | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter5-improved-gan/acgan-mnist-5.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter5-improved-gan/acgan-mnist-5.3.1.py | MIT |
def build_and_train_models():
"""Load the dataset, build LSGAN discriminator,
generator, and adversarial models.
Call the LSGAN train routine.
"""
# load MNIST dataset
(x_train, _), (_, _) = mnist.load_data()
# reshape data for CNN as (28, 28, 1) and normalize
image_size = x_train.shape... | Load the dataset, build LSGAN discriminator,
generator, and adversarial models.
Call the LSGAN train routine.
| build_and_train_models | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter5-improved-gan/lsgan-mnist-5.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter5-improved-gan/lsgan-mnist-5.2.1.py | MIT |
def train(models, x_train, params):
"""Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial
networks by batch.
Discriminator is trained first with properly labelled
real and fake images for n_critic times.
Discriminator weights are clipped as a requir... | Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial
networks by batch.
Discriminator is trained first with properly labelled
real and fake images for n_critic times.
Discriminator weights are clipped as a requirement
of Lipschitz constraint.
Gen... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter5-improved-gan/wgan-mnist-5.1.2.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter5-improved-gan/wgan-mnist-5.1.2.py | MIT |
def build_and_train_models():
"""Load the dataset, build WGAN discriminator,
generator, and adversarial models.
Call the WGAN train routine.
"""
# load MNIST dataset
(x_train, _), (_, _) = mnist.load_data()
# reshape data for CNN as (28, 28, 1) and normalize
image_size = x_train.shape[1... | Load the dataset, build WGAN discriminator,
generator, and adversarial models.
Call the WGAN train routine.
| build_and_train_models | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter5-improved-gan/wgan-mnist-5.1.2.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter5-improved-gan/wgan-mnist-5.1.2.py | MIT |
def train(models, data, params):
"""Train the Discriminator and Adversarial networks
Alternately train discriminator and adversarial networks by batch.
Discriminator is trained first with real and fake images,
corresponding one-hot labels and continuous codes.
Adversarial is trained next with fake ... | Train the Discriminator and Adversarial networks
Alternately train discriminator and adversarial networks by batch.
Discriminator is trained first with real and fake images,
corresponding one-hot labels and continuous codes.
Adversarial is trained next with fake images pretending
to be real, corre... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/infogan-mnist-6.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/infogan-mnist-6.1.1.py | MIT |
def mi_loss(c, q_of_c_given_x):
""" Mutual information, Equation 5 in [2],
assuming H(c) is constant
"""
# mi_loss = -c * log(Q(c|x))
return -K.mean(K.sum(c * K.log(q_of_c_given_x + K.epsilon()),
axis=1)) | Mutual information, Equation 5 in [2],
assuming H(c) is constant
| mi_loss | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/infogan-mnist-6.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/infogan-mnist-6.1.1.py | MIT |
def build_and_train_models(latent_size=100):
"""Load the dataset, build InfoGAN discriminator,
generator, and adversarial models.
Call the InfoGAN train routine.
"""
# load MNIST dataset
(x_train, y_train), (_, _) = mnist.load_data()
# reshape data for CNN as (28, 28, 1) and normalize
i... | Load the dataset, build InfoGAN discriminator,
generator, and adversarial models.
Call the InfoGAN train routine.
| build_and_train_models | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/infogan-mnist-6.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/infogan-mnist-6.1.1.py | MIT |
def build_encoder(inputs, num_labels=10, feature1_dim=256):
""" Build the Classifier (Encoder) Model sub networks
Two sub networks:
1) Encoder0: Image to feature1 (intermediate latent feature)
2) Encoder1: feature1 to labels
# Arguments
inputs (Layers): x - images, feature1 -
... | Build the Classifier (Encoder) Model sub networks
Two sub networks:
1) Encoder0: Image to feature1 (intermediate latent feature)
2) Encoder1: feature1 to labels
# Arguments
inputs (Layers): x - images, feature1 -
feature1 layer output
num_labels (int): number of class la... | build_encoder | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def build_generator(latent_codes, image_size, feature1_dim=256):
"""Build Generator Model sub networks
Two sub networks: 1) Class and noise to feature1
(intermediate feature)
2) feature1 to image
# Arguments
latent_codes (Layers): dicrete code (labels),
noise and featu... | Build Generator Model sub networks
Two sub networks: 1) Class and noise to feature1
(intermediate feature)
2) feature1 to image
# Arguments
latent_codes (Layers): dicrete code (labels),
noise and feature1 features
image_size (int): Target size of one side
... | build_generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def build_discriminator(inputs, z_dim=50):
"""Build Discriminator 1 Model
Classifies feature1 (features) as real/fake image and recovers
the input noise or latent code (by minimizing entropy loss)
# Arguments
inputs (Layer): feature1
z_dim (int): noise dimensionality
# Returns
... | Build Discriminator 1 Model
Classifies feature1 (features) as real/fake image and recovers
the input noise or latent code (by minimizing entropy loss)
# Arguments
inputs (Layer): feature1
z_dim (int): noise dimensionality
# Returns
dis1 (Model): feature1 as real/fake and recov... | build_discriminator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def train(models, data, params):
"""Train the discriminator and adversarial Networks
Alternately train discriminator and adversarial networks by batch.
Discriminator is trained first with real and fake images,
corresponding one-hot labels and latent codes.
Adversarial is trained next with fake imag... | Train the discriminator and adversarial Networks
Alternately train discriminator and adversarial networks by batch.
Discriminator is trained first with real and fake images,
corresponding one-hot labels and latent codes.
Adversarial is trained next with fake images pretending
to be real, correspond... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def plot_images(generators,
noise_params,
show=False,
step=0,
model_name="gan"):
"""Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
# Arguments
generators (Models... | Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
# Arguments
generators (Models): gen0 and gen1 models for
fake images generation
noise_params (list): noise parameters
(label, z0 and z1 codes)
... | plot_images | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def train_encoder(model,
data,
model_name="stackedgan_mnist",
batch_size=64):
""" Train the Encoder Model (enc0 and enc1)
# Arguments
model (Model): Encoder
data (tensor): Train and test data
model_name (string): model name
... | Train the Encoder Model (enc0 and enc1)
# Arguments
model (Model): Encoder
data (tensor): Train and test data
model_name (string): model name
batch_size (int): Train batch size
| train_encoder | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def build_and_train_models():
"""Load the dataset, build StackedGAN discriminator,
generator, and adversarial models.
Call the StackedGAN train routine.
"""
# load MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# reshape and normalize images
image_size = x_train.... | Load the dataset, build StackedGAN discriminator,
generator, and adversarial models.
Call the StackedGAN train routine.
| build_and_train_models | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter6-disentangled-gan/stackedgan-mnist-6.2.1.py | MIT |
def encoder_layer(inputs,
filters=16,
kernel_size=3,
strides=2,
activation='relu',
instance_norm=True):
"""Builds a generic encoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyReLU may be replaced by ReLU
"... | Builds a generic encoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyReLU may be replaced by ReLU
| encoder_layer | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def decoder_layer(inputs,
paired_inputs,
filters=16,
kernel_size=3,
strides=2,
activation='relu',
instance_norm=True):
"""Builds a generic decoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyRe... | Builds a generic decoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyReLU may be replaced by ReLU
Arguments: (partial)
inputs (tensor): the decoder layer input
paired_inputs (tensor): the encoder layer output
provided by U-Net skip connection &
concatenated to inputs.
... | decoder_layer | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def build_generator(input_shape,
output_shape=None,
kernel_size=3,
name=None):
"""The generator is a U-Network made of a 4-layer encoder
and a 4-layer decoder. Layer n-i is connected to layer i.
Arguments:
input_shape (tuple): input shape
... | The generator is a U-Network made of a 4-layer encoder
and a 4-layer decoder. Layer n-i is connected to layer i.
Arguments:
input_shape (tuple): input shape
output_shape (tuple): output shape
kernel_size (int): kernel size of encoder & decoder layers
name (string): name assigned to generator mo... | build_generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def build_discriminator(input_shape,
kernel_size=3,
patchgan=True,
name=None):
"""The discriminator is a 4-layer encoder that outputs either
a 1-dim or a n x n-dim patch of probability that input is real
Arguments:
input_shape (tu... | The discriminator is a 4-layer encoder that outputs either
a 1-dim or a n x n-dim patch of probability that input is real
Arguments:
input_shape (tuple): input shape
kernel_size (int): kernel size of decoder layers
patchgan (bool): whether the output is a patch
or just a 1-dim
name (s... | build_discriminator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def train_cyclegan(models,
data,
params,
test_params,
test_generator):
""" Trains the CycleGAN.
1) Train the target discriminator
2) Train the source discriminator
3) Train the forward and backward cyles of
adver... | Trains the CycleGAN.
1) Train the target discriminator
2) Train the source discriminator
3) Train the forward and backward cyles of
adversarial networks
Arguments:
models (Models): Source/Target Discriminator/Generator,
Adversarial Model
data (tuple): source and target t... | train_cyclegan | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def build_cyclegan(shapes,
source_name='source',
target_name='target',
kernel_size=3,
patchgan=False,
identity=False
):
"""Build the CycleGAN
1) Build target and source discriminators
2) Build ... | Build the CycleGAN
1) Build target and source discriminators
2) Build target and source generators
3) Build the adversarial network
Arguments:
shapes (tuple): source and target shapes
source_name (string): string to be appended on dis/gen models
target_name (string): string to be appended ... | build_cyclegan | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def graycifar10_cross_colorcifar10(g_models=None):
"""Build and train a CycleGAN that can do
grayscale <--> color cifar10 images
"""
model_name = 'cyclegan_cifar10'
batch_size = 32
train_steps = 100000
patchgan = True
kernel_size = 3
postfix = ('%dp' % kernel_size) \
... | Build and train a CycleGAN that can do
grayscale <--> color cifar10 images
| graycifar10_cross_colorcifar10 | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def mnist_cross_svhn(g_models=None):
"""Build and train a CycleGAN that can do mnist <--> svhn
"""
model_name = 'cyclegan_mnist_svhn'
batch_size = 32
train_steps = 100000
patchgan = True
kernel_size = 5
postfix = ('%dp' % kernel_size) \
if patchgan else ('%d' % kernel_size)
... | Build and train a CycleGAN that can do mnist <--> svhn
| mnist_cross_svhn | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/cyclegan-7.1.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/cyclegan-7.1.1.py | MIT |
def display_images(imgs,
filename,
title='',
imgs_dir=None,
show=False):
"""Display images in an nxn grid
Arguments:
imgs (tensor): array of images
filename (string): filename to save the displayed image
title (string): tit... | Display images in an nxn grid
Arguments:
imgs (tensor): array of images
filename (string): filename to save the displayed image
title (string): title on the displayed image
imgs_dir (string): directory where to save the files
show (bool): whether to display the image or not
(False du... | display_images | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/other_utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/other_utils.py | MIT |
def test_generator(generators,
test_data,
step,
titles,
dirs,
todisplay=100,
show=False):
"""Test the generator models
Arguments:
generators (tuple): source and target generators
test_data ... | Test the generator models
Arguments:
generators (tuple): source and target generators
test_data (tuple): source and target test data
step (int): step number during training (0 during testing)
titles (tuple): titles on the displayed image
dirs (tuple): folders to save the outputs of testings
... | test_generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/other_utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/other_utils.py | MIT |
def load_data(data, titles, filenames, todisplay=100):
"""Generic loaded data transformation
Arguments:
data (tuple): source, target, test source, test target data
titles (tuple): titles of the test and source images to display
filenames (tuple): filenames of the test and source images to
di... | Generic loaded data transformation
Arguments:
data (tuple): source, target, test source, test target data
titles (tuple): titles of the test and source images to display
filenames (tuple): filenames of the test and source images to
display
todisplay (int): number of images to display (must b... | load_data | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter7-cross-domain-gan/other_utils.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter7-cross-domain-gan/other_utils.py | MIT |
def sampling(args):
"""Implements reparameterization trick by sampling
from a gaussian with zero mean and std=1.
Arguments:
args (tensor): mean and log of variance of Q(z|X)
Returns:
sampled latent vector (tensor)
"""
z_mean, z_log_var = args
batch = K.shape(z_mean)[0]
... | Implements reparameterization trick by sampling
from a gaussian with zero mean and std=1.
Arguments:
args (tensor): mean and log of variance of Q(z|X)
Returns:
sampled latent vector (tensor)
| sampling | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter8-vae/cvae-cnn-mnist-8.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter8-vae/cvae-cnn-mnist-8.2.1.py | MIT |
def plot_results(models,
data,
y_label,
batch_size=128,
model_name="cvae_mnist"):
"""Plots 2-dim mean values of Q(z|X) using labels
as color gradient then, plot MNIST digits as
function of 2-dim latent vector
Arguments:
... | Plots 2-dim mean values of Q(z|X) using labels
as color gradient then, plot MNIST digits as
function of 2-dim latent vector
Arguments:
models (list): encoder and decoder models
data (list): test data and label
y_label (array): one-hot vector of which digit to plot
... | plot_results | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter8-vae/cvae-cnn-mnist-8.2.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter8-vae/cvae-cnn-mnist-8.2.1.py | MIT |
def sampling(args):
"""Reparameterization trick by sampling
fr an isotropic unit Gaussian.
# Arguments:
args (tensor): mean and log of variance of Q(z|X)
# Returns:
z (tensor): sampled latent vector
"""
z_mean, z_log_var = args
batch = K.shape(z_mean)[0]
dim = K.i... | Reparameterization trick by sampling
fr an isotropic unit Gaussian.
# Arguments:
args (tensor): mean and log of variance of Q(z|X)
# Returns:
z (tensor): sampled latent vector
| sampling | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter8-vae/vae-cnn-mnist-8.1.2.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter8-vae/vae-cnn-mnist-8.1.2.py | MIT |
def plot_results(models,
data,
batch_size=128,
model_name="vae_mnist"):
"""Plots labels and MNIST digits as function
of 2-dim latent vector
# Arguments:
models (tuple): encoder and decoder models
data (tuple): test data and label
... | Plots labels and MNIST digits as function
of 2-dim latent vector
# Arguments:
models (tuple): encoder and decoder models
data (tuple): test data and label
batch_size (int): prediction batch size
model_name (string): which model is using this function
| plot_results | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter8-vae/vae-cnn-mnist-8.1.2.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter8-vae/vae-cnn-mnist-8.1.2.py | MIT |
def __init__(self,
state_space,
action_space,
episodes=500):
"""DQN Agent on CartPole-v0 environment
Arguments:
state_space (tensor): state space
action_space (tensor): action space
episodes (int): number of episod... | DQN Agent on CartPole-v0 environment
Arguments:
state_space (tensor): state space
action_space (tensor): action space
episodes (int): number of episodes to train
| __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def build_model(self, n_inputs, n_outputs):
"""Q Network is 256-256-256 MLP
Arguments:
n_inputs (int): input dim
n_outputs (int): output dim
Return:
q_model (Model): DQN
"""
inputs = Input(shape=(n_inputs, ), name='state')
x = Dense(2... | Q Network is 256-256-256 MLP
Arguments:
n_inputs (int): input dim
n_outputs (int): output dim
Return:
q_model (Model): DQN
| build_model | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def act(self, state):
"""eps-greedy policy
Return:
action (tensor): action to execute
"""
if np.random.rand() < self.epsilon:
# explore - do random action
return self.action_space.sample()
# exploit
q_values = self.q_model.predict(stat... | eps-greedy policy
Return:
action (tensor): action to execute
| act | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def get_target_q_value(self, next_state, reward):
"""compute Q_max
Use of target Q Network solves the
non-stationarity problem
Arguments:
reward (float): reward received after executing
action on state
next_state (tensor): next state
... | compute Q_max
Use of target Q Network solves the
non-stationarity problem
Arguments:
reward (float): reward received after executing
action on state
next_state (tensor): next state
Return:
q_value (float): max Q-value computed
... | get_target_q_value | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def replay(self, batch_size):
"""experience replay addresses the correlation issue
between samples
Arguments:
batch_size (int): replay buffer batch
sample size
"""
# sars = state, action, reward, state' (next_state)
sars_batch = random.sa... | experience replay addresses the correlation issue
between samples
Arguments:
batch_size (int): replay buffer batch
sample size
| replay | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def get_target_q_value(self, next_state, reward):
"""compute Q_max
Use of target Q Network solves the
non-stationarity problem
Arguments:
reward (float): reward received after executing
action on state
next_state (tensor): next state
... | compute Q_max
Use of target Q Network solves the
non-stationarity problem
Arguments:
reward (float): reward received after executing
action on state
next_state (tensor): next state
Returns:
q_value (float): max Q-value computed
... | get_target_q_value | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/dqn-cartpole-9.6.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/dqn-cartpole-9.6.1.py | MIT |
def __init__(self,
observation_space,
action_space,
demo=False,
slippery=False,
episodes=40000):
"""Q-Learning agent on FrozenLake-v0 environment
Arguments:
observation_space (tensor): state space
... | Q-Learning agent on FrozenLake-v0 environment
Arguments:
observation_space (tensor): state space
action_space (tensor): action space
demo (Bool): whether for demo or training
slippery (Bool): 2 versions of FLv0 env
episodes (int): number of episodes t... | __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-frozenlake-9.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-frozenlake-9.5.1.py | MIT |
def act(self, state, is_explore=False):
"""determine the next action
if random, choose from random action space
else use the Q Table
Arguments:
state (tensor): agent's current state
is_explore (Bool): exploration mode or not
Return:
act... | determine the next action
if random, choose from random action space
else use the Q Table
Arguments:
state (tensor): agent's current state
is_explore (Bool): exploration mode or not
Return:
action (tensor): action that the agent
... | act | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-frozenlake-9.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-frozenlake-9.5.1.py | MIT |
def update_q_table(self, state, action, reward, next_state):
"""TD(0) learning (generalized Q-Learning) with learning rate
Arguments:
state (tensor): environment state
action (tensor): action executed by the agent for
the given state
reward (float): re... | TD(0) learning (generalized Q-Learning) with learning rate
Arguments:
state (tensor): environment state
action (tensor): action executed by the agent for
the given state
reward (float): reward received by the agent for
executing the action
... | update_q_table | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-frozenlake-9.5.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-frozenlake-9.5.1.py | MIT |
def __init__(self):
"""Simulated deterministic world made of 6 states.
Q-Learning by Bellman Equation.
"""
# 4 actions
# 0 - Left, 1 - Down, 2 - Right, 3 - Up
self.col = 4
# 6 states
self.row = 6
# setup the environment
self.q_table = np... | Simulated deterministic world made of 6 states.
Q-Learning by Bellman Equation.
| __init__ | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def step(self, action):
"""execute the action on the environment
Argument:
action (tensor): An action in Action space
Returns:
next_state (tensor): next env state
reward (float): reward received by the agent
done (Bool): whether the terminal state ... | execute the action on the environment
Argument:
action (tensor): An action in Action space
Returns:
next_state (tensor): next env state
reward (float): reward received by the agent
done (Bool): whether the terminal state
is reached
... | step | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def act(self):
"""determine the next action
either fr Q Table(exploitation) or
random(exploration)
Return:
action (tensor): action that the agent
must execute
"""
# 0 - Left, 1 - Down, 2 - Right, 3 - Up
# action is from explorat... | determine the next action
either fr Q Table(exploitation) or
random(exploration)
Return:
action (tensor): action that the agent
must execute
| act | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def update_q_table(self, state, action, reward, next_state):
"""Q-Learning - update the Q Table using Q(s, a)
Arguments:
state (tensor) : agent state
action (tensor): action executed by the agent
reward (float): reward after executing action
for a giv... | Q-Learning - update the Q Table using Q(s, a)
Arguments:
state (tensor) : agent state
action (tensor): action executed by the agent
reward (float): reward after executing action
for a given state
next_state (tensor): next state after executing
... | update_q_table | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def print_cell(self, row=0):
"""UI to display agent moving on the grid"""
print("")
for i in range(13):
j = i - 2
if j in [0, 4, 8]:
if j == 8:
if self.state == 2 and row == 0:
marker = "\033[4mG\033[0m"
... | UI to display agent moving on the grid | print_cell | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def print_world(self, action, step):
"""UI to display mode and action of agent"""
actions = { 0: "(Left)", 1: "(Down)", 2: "(Right)", 3: "(Up)" }
explore = "Explore" if self.is_explore else "Exploit"
print("Step", step, ":", explore, actions[action])
for _ in range(13):
... | UI to display mode and action of agent | print_world | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def print_episode(episode, delay=1):
"""UI to display episode count
Arguments:
episode (int): episode number
delay (int): sec delay
"""
os.system('clear')
for _ in range(13):
print('=', end='')
print("")
print("Episode ", episode)
for _ in range(13):
prin... | UI to display episode count
Arguments:
episode (int): episode number
delay (int): sec delay
| print_episode | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def print_status(q_world, done, step, delay=1):
"""UI to display the world,
delay of 1 sec for ease of understanding
"""
os.system('clear')
q_world.print_world(action, step)
q_world.print_q_table()
if done:
print("-------EPISODE DONE--------")
delay *= 2
time.sleep(d... | UI to display the world,
delay of 1 sec for ease of understanding
| print_status | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | chapter9-drl/q-learning-9.3.1.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter9-drl/q-learning-9.3.1.py | MIT |
def generator(inputs,
image_size,
activation='sigmoid',
labels=None,
codes=None):
"""Build a Generator Model
Stack of BN-ReLU-Conv2DTranpose to generate fake images.
Output activation is sigmoid instead of tanh in [1].
Sigmoid converges easily.
... | Build a Generator Model
Stack of BN-ReLU-Conv2DTranpose to generate fake images.
Output activation is sigmoid instead of tanh in [1].
Sigmoid converges easily.
Arguments:
inputs (Layer): Input layer of the generator (the z-vector)
image_size (int): Target size of one side
... | generator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | lib/gan.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/lib/gan.py | MIT |
def discriminator(inputs,
activation='sigmoid',
num_labels=None,
num_codes=None):
"""Build a Discriminator Model
Stack of LeakyReLU-Conv2D to discriminate real from fake
The network does not converge with BN so it is not used here
unlike in [1]
... | Build a Discriminator Model
Stack of LeakyReLU-Conv2D to discriminate real from fake
The network does not converge with BN so it is not used here
unlike in [1]
Arguments:
inputs (Layer): Input layer of the discriminator (the image)
activation (string): Name of output activation layer
... | discriminator | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | lib/gan.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/lib/gan.py | MIT |
def train(models, x_train, params):
"""Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly real and fake images.
Adversarial is trained next with fake images pretending to be real
Generate s... | Train the Discriminator and Adversarial Networks
Alternately train Discriminator and Adversarial networks by batch.
Discriminator is trained first with properly real and fake images.
Adversarial is trained next with fake images pretending to be real
Generate sample images per save_interval.
# Argu... | train | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | lib/gan.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/lib/gan.py | MIT |
def plot_images(generator,
noise_input,
noise_label=None,
noise_codes=None,
show=False,
step=0,
model_name="gan"):
"""Generate fake images and plot them
For visualization purposes, generate fake images
then plot... | Generate fake images and plot them
For visualization purposes, generate fake images
then plot them in a square grid
# Arguments
generator (Model): The Generator Model for
fake images generation
noise_input (ndarray): Array of z-vectors
show (bool): Whether to show plot... | plot_images | python | PacktPublishing/Advanced-Deep-Learning-with-Keras | lib/gan.py | https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/lib/gan.py | MIT |
def chunked(iterator, chunksize):
"""
Yields items from 'iterator' in chunks of size 'chunksize'.
>>> list(chunked([1, 2, 3, 4, 5], chunksize=2))
[(1, 2), (3, 4), (5,)]
"""
chunk = []
for idx, item in enumerate(iterator, 1):
chunk.append(item)
if idx % chunksize == 0:
... |
Yields items from 'iterator' in chunks of size 'chunksize'.
>>> list(chunked([1, 2, 3, 4, 5], chunksize=2))
[(1, 2), (3, 4), (5,)]
| chunked | python | pmclanahan/django-celery-email | djcelery_email/utils.py | https://github.com/pmclanahan/django-celery-email/blob/master/djcelery_email/utils.py | BSD-3-Clause |
def celery_queue_pop():
""" Pops a single task from Celery's 'memory://' queue. """
with celery.current_app.connection() as conn:
queue = conn.SimpleQueue('django_email', no_ack=True)
return queue.get().payload | Pops a single task from Celery's 'memory://' queue. | celery_queue_pop | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_single_email_object(self):
""" It should accept and send a single EmailMessage object. """
msg = mail.EmailMessage()
tasks.send_email(msg, backend_kwargs={})
self.assertEqual(len(mail.outbox), 1)
# we can't compare them directly as it's converted into a dict
... | It should accept and send a single EmailMessage object. | test_send_single_email_object | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_single_email_object_no_backend_kwargs(self):
""" It should send email with backend_kwargs not provided. """
msg = mail.EmailMessage()
tasks.send_email(msg)
self.assertEqual(len(mail.outbox), 1)
# we can't compare them directly as it's converted into a dict
#... | It should send email with backend_kwargs not provided. | test_send_single_email_object_no_backend_kwargs | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_single_email_object_response(self):
""" It should return the number of messages sent, 1 here. """
msg = mail.EmailMessage()
messages_sent = tasks.send_email(msg)
self.assertEqual(messages_sent, 1)
self.assertEqual(len(mail.outbox), 1) | It should return the number of messages sent, 1 here. | test_send_single_email_object_response | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_single_email_dict(self):
""" It should accept and send a single EmailMessage dict. """
msg = mail.EmailMessage()
tasks.send_email(email_to_dict(msg), backend_kwargs={})
self.assertEqual(len(mail.outbox), 1)
# we can't compare them directly as it's converted into a d... | It should accept and send a single EmailMessage dict. | test_send_single_email_dict | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_multiple_email_objects(self):
""" It should accept and send a list of EmailMessage objects. """
N = 10
msgs = [mail.EmailMessage() for i in range(N)]
tasks.send_emails([email_to_dict(msg) for msg in msgs],
backend_kwargs={})
self.assertEqu... | It should accept and send a list of EmailMessage objects. | test_send_multiple_email_objects | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_multiple_email_dicts(self):
""" It should accept and send a list of EmailMessage dicts. """
N = 10
msgs = [mail.EmailMessage() for i in range(N)]
tasks.send_emails(msgs, backend_kwargs={})
self.assertEqual(len(mail.outbox), N)
for i in range(N):
... | It should accept and send a list of EmailMessage dicts. | test_send_multiple_email_dicts | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_send_multiple_email_dicts_response(self):
""" It should return the number of messages sent. """
N = 10
msgs = [mail.EmailMessage() for i in range(N)]
messages_sent = tasks.send_emails(msgs, backend_kwargs={})
self.assertEqual(messages_sent, N)
self.assertEqual(le... | It should return the number of messages sent. | test_send_multiple_email_dicts_response | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_uses_correct_backend(self):
""" It should use the backend configured in CELERY_EMAIL_BACKEND. """
TracingBackend.called = False
msg = mail.EmailMessage()
tasks.send_email(email_to_dict(msg), backend_kwargs={})
self.assertTrue(TracingBackend.called) | It should use the backend configured in CELERY_EMAIL_BACKEND. | test_uses_correct_backend | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
def test_backend_parameters(self):
""" It should pass kwargs like username and password to the backend. """
TracingBackend.kwargs = None
msg = mail.EmailMessage()
tasks.send_email(email_to_dict(msg), backend_kwargs={'foo': 'bar'})
self.assertEqual(TracingBackend.kwargs.get('foo')... | It should pass kwargs like username and password to the backend. | test_backend_parameters | python | pmclanahan/django-celery-email | tests/tests.py | https://github.com/pmclanahan/django-celery-email/blob/master/tests/tests.py | BSD-3-Clause |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.