Skip to content

Commit

Permalink
Conv GAN is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragav Venkatesan committed Mar 10, 2017
1 parent fbc3f22 commit 3a66aeb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
61 changes: 33 additions & 28 deletions pantry/tutorials/gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,21 +583,14 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):
sigma = 1,
# limits = (0,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 = 1200,
activation = 'relu',
regularize = True,
# batch_norm = True,
verbose = verbose
)
Expand All @@ -607,6 +600,7 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):
id = "G2",
num_neurons = 1440,
activation = 'relu',
regularize = True,
# batch_norm = True,
verbose = verbose
)
Expand Down Expand Up @@ -637,22 +631,31 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):
filter_size = (3,3),
output_shape = (28,28,1),
activation = 'tanh',
regularize = True,
# regularize = True,
stride = (1,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 )

#D(x) - Contains params theta_d creates features 1 X 800.
# Discriminator Layers
# add first convolutional layer

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

Expand All @@ -663,30 +666,32 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):
filter_size = (5,5),
pool_size = (2,2),
activation = 'relu',
# regularize = True,
regularize = True,
input_params = net.dropout_layers["D1-x"].params,
verbose = verbose
)

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

net.add_layer ( type = "conv_pool",
origin = "D1-z",
# origin = "G(z)",
id = "D2-z",
num_neurons = 50,
filter_size = (3,3),
pool_size = (2,2),
activation = 'relu',
# regularize = True,
regularize = True,
input_params = net.dropout_layers["D2-x"].params,
verbose = verbose
)
Expand All @@ -695,44 +700,44 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):
id = "D3-x",
origin = "D2-x",
num_neurons = 1200,
activation = ('maxout','maxout',5),
activation = 'relu',
regularize = True,
# batch_norm = True,
# dropout_rate = 0.5,
dropout_rate = 0.5,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D3-z",
origin = "D2-z",
input_params = net.dropout_layers["D1-x"].params,
input_params = net.dropout_layers["D3-x"].params,
num_neurons = 1200,
activation = ('maxout','maxout',5),
activation = 'relu',
regularize = True,
# batch_norm = True,
# dropout_rate = 0.5,
dropout_rate = 0.5,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D4-x",
origin = "D3-x",
num_neurons = 1200,
activation = ('maxout','maxout',5),
activation = 'relu',
regularize = True,
# batch_norm = True,
# dropout_rate = 0.5,
dropout_rate = 0.5,
verbose = verbose
)

net.add_layer ( type = "dot_product",
id = "D4-z",
origin = "D3-z",
input_params = net.dropout_layers["D2-x"].params,
input_params = net.dropout_layers["D4-x"].params,
num_neurons = 1200,
activation = ('maxout','maxout',5),
activation = 'relu',
regularize = True,
# dropout_rate = 0.5,
dropout_rate = 0.5,
# batch_norm = True,
verbose = verbose
)
Expand Down Expand Up @@ -814,9 +819,9 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):

net.cook ( objective_layers = ["classifier_obj", "discriminator_obj", "generator_obj"],
optimizer_params = optimizer_params,
discriminator_layers = ["D1-x","D2-x","D3-x","D4-x"],
discriminator_layers = ["D1-x", "D2-x","D3-x","D4-x"],
generator_layers = ["G1","G2","G3","G(z)"],
classifier_layers = ["D1-x","D2-x","D3-x","D4-x","softmax"],
classifier_layers = ["D1-x", "D2-x","D3-x","D4-x","softmax"],
softmax_layer = "softmax",
game_layers = ("D(x)", "D(G(z))"),
verbose = verbose )
Expand Down Expand Up @@ -855,4 +860,4 @@ def deep_deconvolutional_gan (dataset, verbose = 1 ):

# net = shallow_gan ( dataset, verbose = 2 )
# net = deep_gan ( dataset, verbose = 2 )
net = deep_deconvolutional_gan ( dataset, verbose = 3 )
net = deep_deconvolutional_gan ( dataset, verbose = 2 )
4 changes: 2 additions & 2 deletions yann/layers/conv_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__ ( self,
self.beta = init_beta
self.running_mean = init_mean
self.running_var = init_var

# Perform the convolution part
convolver = convolver_2d (
input = input,
Expand Down Expand Up @@ -483,7 +483,7 @@ def __init__ ( self,
)

conv_out = convolver.out
conv_out_shp = (mini_batch_size, channels, convolver.out_shp[0], convolver.out_shp[1])
conv_out_shp = o_shp

self.conv_out = conv_out
if not poolsize == (1,1):
Expand Down
10 changes: 5 additions & 5 deletions yann/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,6 @@ def _add_conv_layer(self, id, options, verbose = 2):
else:
origin = options["origin"]

input_shape = self.layers[origin].output_shape

if not 'num_neurons' in options.keys():
if verbose >=3:
print("... num_neurons not provided for layer " + id + ". Asumming 20")
Expand Down Expand Up @@ -741,12 +739,14 @@ def _add_conv_layer(self, id, options, verbose = 2):
from yann.layers.conv_pool import dropout_conv_pool_layer_2d as dcpl2d
from yann.layers.conv_pool import conv_pool_layer_2d as cpl2d

input_shape = self.layers[origin].output_shape

self.dropout_layers[id] = dcpl2d (
input = self.dropout_layers[origin].output,
dropout_rate = dropout_rate,
nkerns = num_neurons,
id = id,
input_shape = self.dropout_layers[origin].output_shape,
input_shape = input_shape,
filter_shape = filter_size,
poolsize = pool_size,
pooltype = pool_type,
Expand Down Expand Up @@ -784,7 +784,7 @@ def _add_conv_layer(self, id, options, verbose = 2):
input = self.layers[origin].output,
nkerns = num_neurons,
id = id,
input_shape = self.layers[origin].output_shape,
input_shape = input_shape,
filter_shape = filter_size,
poolsize = pool_size,
pooltype = pool_type,
Expand All @@ -802,7 +802,7 @@ def _add_conv_layer(self, id, options, verbose = 2):
input = self.inference_layers[origin].inference,
nkerns = num_neurons,
id = id,
input_shape = self.layers[origin].output_shape,
input_shape = input_shape,
filter_shape = filter_size,
poolsize = pool_size,
pooltype = pool_type,
Expand Down
17 changes: 13 additions & 4 deletions yann/special/gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,18 @@ def _create_layer_activities(self, datastream = None, verbose = 2):
self.layer_activities_created = True
for id, _layer in self.inference_layers.iteritems():
if verbose >=3 :
print("... collecting the activities of layer " + id)
activity = _layer.output
if self.softmax_head is True:
print("... collecting the activities of layer " + id)
activity = _layer.output

out = True
""" This would avoid printing the layer that has only one output
if len(_layer.output_shape) == 2:
if _layer.output_shape[1] == 1:
out = False
elif _layer.output_shape == (1,):
out = False
"""
if self.softmax_head is True and out is True:
self.layer_activities[id] = theano.function(
name = 'layer_activity_' + id,
inputs = [index],
Expand All @@ -479,7 +488,7 @@ def _create_layer_activities(self, datastream = None, verbose = 2):
self.cooked_datastream.mini_batch_size:(index + 1) *
self.cooked_datastream.mini_batch_size]},
on_unused_input = 'ignore')
else:
elif out is True:
self.layer_activities[id] = theano.function(
name = 'layer_activity_' + id,
inputs = [index],
Expand Down

0 comments on commit 3a66aeb

Please sign in to comment.