Skip to content

Commit

Permalink
Added a Deep GAN
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragav Venkatesan committed Mar 4, 2017
1 parent 7d260bf commit 984e38d
Showing 1 changed file with 283 additions and 1 deletion.
284 changes: 283 additions & 1 deletion pantry/tutorials/gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,287 @@ def shallow_gan ( dataset= None, verbose = 1 ):

return net


def deep_gan (dataset, verbose = 1 ):
"""
This function is a demo example of a generative adversarial network.
This is an example code. You should study this code rather than merely run it.
Args:
dataset: Supply a dataset.
verbose: Similar to the rest of the dataset.
Returns:
net: A Network object.
"""
if verbose >=2:
print (".. Creating a GAN network")

optimizer_params = {
"momentum_type" : 'polyak',
"momentum_params" : (0.65, 0.9, 50),
"regularization" : (0.000, 0.000),
"optimizer_type" : 'rmsprop',
"id" : "main"
}


dataset_params = {
"dataset" : dataset,
"type" : 'xy',
"id" : 'data'
}

visualizer_params = {
"root" : '.',
"frequency" : 1,
"sample_size": 225,
"rgb_filters": False,
"debug_functions" : False,
"debug_layers": True,
"id" : 'main'
}

# intitialize the network
net = gan ( borrow = True,
verbose = verbose )

net.add_module ( type = 'datastream',
params = dataset_params,
verbose = verbose )

net.add_module ( type = 'visualizer',
params = visualizer_params,
verbose = verbose
)

#z - latent space created by random layer
net.add_layer(type = 'random',
id = 'z',
num_neurons = (100,32),
distribution = 'normal',
mu = 0,
sigma = 1,
verbose = verbose)

#x - inputs come from dataset 1 X 784
net.add_layer ( type = "input",
id = "x",
verbose = verbose,
datastream_origin = 'data', # if you didnt add a dataset module, now is
# the time.
mean_subtract = False )

# Generator layers
net.add_layer ( type = "dot_product",
origin = "z",
id = "G1",
num_neurons = 128,
activation = 'tanh',
verbose = verbose
)

net.add_layer ( type = "dot_product",
origin = "G1",
id = "G2",
num_neurons = 384,
activation = 'tanh',
verbose = verbose
)

net.add_layer ( type = "dot_product",
origin = "G2",
id = "G(z)",
num_neurons = 784,
activation = 'tanh',
verbose = verbose
) # This layer is the one that creates the images.

#D(x) - Contains params theta_d creates features 1 X 800.
# Discriminator Layers
net.add_layer ( type = "unflatten",
origin = "G(z)",
shape = (28,28),
verbose = verbose )

net.add_layer ( type = "conv_pool",
id = "D1-x",
origin = "x",
num_neurons = 20,
filter_shape = (5,5),
activation = 'relu',
regularize = True,
verbose = verbose
)

net.add_layer ( type = "conv_pool",
id = "D1-z",
origin = "G(z)",
num_neurons = 20,
filter_shape = (5,5),
activation = 'relu',
regularize = True,
input_params = net.dropout_layers['D1-x'].params,
verbose = verbose
)

net.add_layer ( type = "conv_pool",
id = "D2-x",
origin = "D1-x",
num_neurons = 50,
filter_shape = (2,2),
activation = 'relu',
regularize = True,
verbose = verbose
)

net.add_layer ( type = "conv_pool",
id = "D2-z",
origin = "D1-z",
num_neurons = 50,
activation = 'relu',
filter_shape = (2,2),
regularize = True,
input_params = net.dropout_layers['D2-x'].params,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D3-x",
origin = "D2-x",
num_neurons = 800,
activation = 'relu',
regularize = True,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D3-z",
origin = "D2-z",
input_params = net.dropout_layers["D3-x"].params,
num_neurons = 800,
activation = 'relu',
regularize = True,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D4-x",
origin = "D3-x",
num_neurons = 800,
activation = 'relu',
regularize = True,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D4-z",
origin = "D3-z",
input_params = net.dropout_layers["D4-x"].params,
num_neurons = 800,
activation = 'relu',
regularize = True,
verbose = verbose
)

#C(D(x)) - This is the opposite of C(D(G(z))), real
net.add_layer ( type = "dot_product",
id = "D(x)",
origin = "D4-x",
num_neurons = 1,
activation = 'sigmoid',
verbose = verbose
)

#C(D(G(z))) fake - the classifier for fake/real that always predicts fake
net.add_layer ( type = "dot_product",
id = "D(G(z))",
origin = "D4-z",
num_neurons = 1,
activation = 'sigmoid',
input_params = net.dropout_layers["D(x)"].params,
verbose = verbose
)


#C(D(x)) - This is the opposite of C(D(G(z))), real
net.add_layer ( type = "classifier",
id = "softmax",
origin = "D4-x",
num_classes = 10,
activation = 'softmax',
verbose = verbose
)

# objective layers
# discriminator objective
net.add_layer (type = "tensor",
input = - 0.5 * T.mean(T.log(net.layers['D(x)'].output)) - \
0.5 * T.mean(T.log(1-net.layers['D(G(z))'].output)),
input_shape = (1,),
id = "discriminator_task"
)

net.add_layer ( type = "objective",
id = "discriminator_obj",
origin = "discriminator_task",
layer_type = 'value',
objective = net.dropout_layers['discriminator_task'].output,
datastream_origin = 'data',
verbose = verbose
)
#generator objective
net.add_layer (type = "tensor",
input = - 0.5 * T.mean(T.log(net.layers['D(G(z))'].output)),
input_shape = (1,),
id = "objective_task"
)
net.add_layer ( type = "objective",
id = "generator_obj",
layer_type = 'value',
origin = "objective_task",
objective = net.dropout_layers['objective_task'].output,
datastream_origin = 'data',
verbose = verbose
)

#softmax objective.
net.add_layer ( type = "objective",
id = "classifier_obj",
origin = "softmax",
objective = "nll",
layer_type = 'discriminator',
datastream_origin = 'data',
verbose = verbose
)

from yann.utils.graph import draw_network
draw_network(net.graph, filename = 'gan.png')
net.pretty_print()

net.cook ( objective_layers = ["classifier_obj", "discriminator_obj", \
"generator_obj"],
optimizer_params = optimizer_params,
discriminator_layers = ["D1-x","D2-x","D3-x","D4-x"],
generator_layers = ["G1","G2","G(z)"],
classifier_layers = ["D1-x","D2-x","D3-x","D4-x", "softmax"],
softmax_layer = "softmax",
game_layers = ("D(x)", "D(G(z))"),
verbose = verbose )

learning_rates = (0.05, 0.01 )

net.train( epochs = (20),
k = 2,
pre_train_discriminator = 3,
validate_after_epochs = 1,
visualize_after_epochs = 1,
training_accuracy = True,
show_progress = True,
early_terminate = True,
verbose = verbose)

return net
if __name__ == '__main__':
import sys
dataset = None
Expand All @@ -271,4 +552,5 @@ def shallow_gan ( dataset= None, verbose = 1 ):
data = cook_mnist (verbose = 2)
dataset = data.dataset_location()

net = shallow_gan ( dataset, verbose = 2 )
#net = shallow_gan ( dataset, verbose = 2 )
net = deep_gan ( dataset, verbose = 2 )

0 comments on commit 984e38d

Please sign in to comment.