Skip to content

Commit

Permalink
Merge branch 'dev' of http://github.com/ragavvenkatesan/yann into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragav Venkatesan (Student) authored and Ragav Venkatesan (Student) committed Mar 13, 2017
2 parents cd16675 + 9ace813 commit 63e0fa0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 177 deletions.
38 changes: 0 additions & 38 deletions yann/layers/batch_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,6 @@ def __init__ ( self,
self.active_params = [self.gamma, self.beta]
self.input_shape = input_shape
self.output_shape = input_shape

def get_params (self , borrow = True, verbose = 2):
"""
This method returns the parameters of the layer in a numpy ndarray format.
Args:
borrow : Theano borrow, default is True.
verbose: As always
Notes:
This is a slow method, because we are taking the values out of GPU. Ordinarily, I should
have used get_value( borrow = True ), but I can't do this because some parameters are
theano.tensor.var.TensorVariable which needs to be run through eval.
"""
out = []

for p in self.params:
out.append(p.get_value(borrow = borrow))
return out

class dropout_batch_norm_layer_2d(batch_norm_layer_2d):
"""
Expand Down Expand Up @@ -254,25 +235,6 @@ def __init__ ( self,
self.active_params = [self.gamma, self.beta]
self.input_shape = input_shape
self.output_shape = input_shape

def get_params (self , borrow = True, verbose = 2):
"""
This method returns the parameters of the layer in a numpy ndarray format.
Args:
borrow : Theano borrow, default is True.
verbose: As always
Notes:
This is a slow method, because we are taking the values out of GPU. Ordinarily, I should
have used get_value( borrow = True ), but I can't do this because some parameters are
theano.tensor.var.TensorVariable which needs to be run through eval.
"""
out = []

for p in self.params:
out.append(p.get_value(borrow = borrow))
return out

class dropout_batch_norm_layer_1d(batch_norm_layer_2d):
"""
Expand Down
146 changes: 53 additions & 93 deletions yann/layers/conv_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ def __init__ ( self,
rng = numpy.random

# To copy weights previously created or some wierd initializations
if input_params is not None:
init_w = input_params[0]
init_b = input_params[1]

create_w = False
create_b = False
create_bn = False

if not input_params is None:
if input_params[0] is None:
create_w = True
if input_params[1] is None:
create_b = True
if batch_norm is True:
init_gamma = input_params[2]
init_beta = input_params[3]
init_mean = input_params[4]
init_var = input_params[5]
if input_params [2] is None:
create_bn = True

mini_batch_size = input_shape[0]
channels = input_shape[1]
Expand All @@ -91,18 +96,22 @@ def __init__ ( self,
# Initialize the parameters of this layer.
w_shp = (nkerns, channels, filter_shape[0], filter_shape[1])

if input_params is None:
# fan_in = filter_shape[0]*filter_shape[1]
# fan_out = filter_shape[0]*filter_shape[1] / numpy.prod(poolsize)
# w_bound = numpy.sqrt(6. / (fan_in + fan_out))
if create_w is True:
self.w = theano.shared(value=
# numpy.asarray(rng.uniform(low=-w_bound, high=w_bound, size =w_shp),
numpy.asarray( 0.01 * rng.standard_normal( size=w_shp),
dtype=theano.config.floatX ), borrow=borrow,
name ='filterbank' )
else:
self.w = input_params[0]

if create_b is True:
self.b = theano.shared(value=numpy.zeros((nkerns,), dtype=theano.config.floatX),
name = 'bias', borrow=borrow)
if batch_norm is True:
else:
self.b = input_params[1]

if batch_norm is True:
if create_bn is True:
self.gamma = theano.shared(value=numpy.ones((nkerns,),
dtype=theano.config.floatX), name = 'gamma', borrow = borrow)
self.beta = theano.shared(value=numpy.zeros((nkerns,),
Expand All @@ -115,14 +124,11 @@ def __init__ ( self,
value=numpy.ones((nkerns,),
dtype=theano.config.floatX),
name = 'population_var', borrow=borrow)
else:
self.w = init_w
self.b = init_b
if batch_norm is True:
self.gamma = init_gamma
self.beta = init_beta
self.running_mean = init_mean
self.running_var = init_var
else:
self.gamma = input_params[2]
self.beta = input_params[3]
self.running_mean = input_params[4]
self.running_var = input_params[5]

# Perform the convolution part
convolver = convolver_2d (
Expand Down Expand Up @@ -243,25 +249,6 @@ def print_layer(self, prefix = " " , nest = False, last = True):

return prefix

def get_params (self , borrow = True, verbose = 2):
"""
This method returns the parameters of the layer in a numpy ndarray format.
Args:
borrow : Theano borrow, default is True.
verbose: As always
Notes:
This is a slow method, because we are taking the values out of GPU. Ordinarily, I should
have used get_value( borrow = True ), but I can't do this because some parameters are
theano.tensor.var.TensorVariable which needs to be run through eval.
"""
out = []

for p in self.params:
out.append(p.get_value(borrow = borrow))
return out

class dropout_conv_pool_layer_2d(conv_pool_layer_2d):
"""
This class is the typical 2D convolutional pooling and batch normalizationlayer. It is called
Expand Down Expand Up @@ -404,15 +391,19 @@ def __init__ ( self,
if rng is None:
rng = numpy.random

create_w = False
create_b = False
create_bn = False

# To copy weights previously created or some wierd initializations
if input_params is not None:
init_w = input_params[0]
init_b = input_params[1]
if not input_params is None:
if input_params[0] is None:
create_w = True
if input_params[1] is None:
create_b = True
if batch_norm is True:
init_gamma = input_params[2]
init_beta = input_params[3]
init_mean = input_params[4]
init_var = input_params[5]
if input_params [2] is None:
create_bn = True

mini_batch_size = input_shape[0]
channels = input_shape[1]
Expand All @@ -424,18 +415,22 @@ def __init__ ( self,
w_shp = (nkerns, output_shape[2], filter_shape[0], filter_shape[1])
o_shp = (input_shape[0],output_shape[2],output_shape[0],output_shape[1])

if input_params is None:
# fan_in = filter_shape[0]*filter_shape[1]
# fan_out = filter_shape[0]*filter_shape[1] / numpy.prod(poolsize)
# w_bound = numpy.sqrt(6. / (fan_in + fan_out))
if create_w is True:
self.w = theano.shared(value=
# numpy.asarray(rng.uniform(low=-w_bound, high=w_bound, size =w_shp),
numpy.asarray( 0.01 * rng.standard_normal( size=w_shp),
dtype=theano.config.floatX ), borrow=borrow,
name ='filterbank' )
else:
self.w = input_params[0]

if create_b is True:
self.b = theano.shared(value=numpy.zeros((output_shape[2],), dtype=theano.config.floatX),
name = 'bias', borrow=borrow)
if batch_norm is True:
else:
self.b = input_params[1]

if batch_norm is True:
if create_bn is True:
self.gamma = theano.shared(value=numpy.ones((output_shape[2],),
dtype=theano.config.floatX), name = 'gamma', borrow = borrow)
self.beta = theano.shared(value=numpy.zeros((output_shape[2],),
Expand All @@ -448,28 +443,12 @@ def __init__ ( self,
value=numpy.ones((output_shape[2],),
dtype=theano.config.floatX),
name = 'population_var', borrow=borrow)
else:
if init_w is None: # I need to do this the same way I did fully connected.
self.w = theano.shared(value=
# numpy.asarray(rng.uniform(low=-w_bound, high=w_bound, size =w_shp),
numpy.asarray( 0.01 * rng.standard_normal( size=w_shp),
dtype=theano.config.floatX ), borrow=borrow,
name ='filterbank' )
else:
self.w = init_w

if init_b is None:
self.b = theano.shared(value=numpy.zeros((output_shape[2],),
dtype=theano.config.floatX),
name = 'bias', borrow=borrow)
else:
self.b = init_b
if batch_norm is True:
self.gamma = init_gamma
self.beta = init_beta
self.running_mean = init_mean
self.running_var = init_var

self.gamma = input_params[2]
self.beta = input_params[3]
self.running_mean = input_params[4]
self.running_var = input_params[5]

# Perform the convolution part
convolver = deconvolver_2d (
input = input,
Expand Down Expand Up @@ -596,25 +575,6 @@ def print_layer(self, prefix = " " , nest = False, last = True):

return prefix

def get_params (self , borrow = True, verbose = 2):
"""
This method returns the parameters of the layer in a numpy ndarray format.
Args:
borrow : Theano borrow, default is True.
verbose: As always
Notes:
This is a slow method, because we are taking the values out of GPU. Ordinarily, I should
have used get_value( borrow = True ), but I can't do this because some parameters are
theano.tensor.var.TensorVariable which needs to be run through eval.
"""
out = []

for p in self.params:
out.append(p.get_value(borrow = borrow))
return out

class dropout_deconv_layer_2d(deconv_layer_2d):
"""
This class is the typical 2D deconvolutional and batch normalization layer. It is called
Expand Down
60 changes: 20 additions & 40 deletions yann/layers/fully_connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ def __init__ (self,
if rng is None:
rng = numpy.random

create = False
if input_params is None:
create = True
elif input_params[0] is None:
create = True
if create is True:
create_w = False
create_b = False
create_bn = False

if not input_params is None:
if input_params[0] is None:
create_w = True
if input_params[1] is None:
create_b = True
if batch_norm is True:
if input_params [2] is None:
create_bn = True
else:
create_w = True
create_b = True
create_bn = True

if create_w is True:
w_values = numpy.asarray(0.01 * rng.standard_normal(
size=(input_shape[1], num_neurons)), dtype=theano.config.floatX)
if activation == 'sigmoid':
Expand All @@ -64,24 +76,14 @@ def __init__ (self,
else:
self.w = input_params[0]

create = False
if input_params is None:
create = True
elif input_params[1] is None:
create = True
if create is True:
if create_b is True:
b_values = numpy.zeros((num_neurons,), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, name='bias')
else:
self.b = input_params[1]

if batch_norm is True:
create = False
if input_params is None:
create = True
elif input_params[2] is None:
create = True
if create is True:
if create_bn is True:
gamma_values = numpy.ones((1,num_neurons), dtype = theano.config.floatX)
self.gamma = theano.shared(value = gamma_values, name = 'gamma')
beta_values = numpy.zeros((1,num_neurons), dtype=theano.config.floatX)
Expand Down Expand Up @@ -163,28 +165,6 @@ def __init__ (self,
self.activation = activation
self.batch_norm = batch_norm

def get_params (self , borrow = True, verbose = 2):
"""
This method returns the parameters of the layer in a numpy ndarray format.
Args:
borrow : Theano borrow, default is True.
verbose: As always
Notes:
This is a slow method, because we are taking the values out of GPU. Ordinarily, I should
have used get_value( borrow = True ), but I can't do this because some parameters are
theano.tensor.var.TensorVariable which needs to be run through eval.
"""
out = []

for p in self.params:
try:
out.append(p.get_value(borrow = borrow))
except:
out.append(p.eval())
return out

class dropout_dot_product_layer (dot_product_layer):
"""
This class is the typical dropout neural hidden layer and batch normalization layer. Called
Expand Down
10 changes: 4 additions & 6 deletions yann/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3127,13 +3127,11 @@ def get_params (self, verbose = 2):
print "... Collecting network parameters"
params = OrderedDict()
for lyr in self.dropout_layers.keys():
params_list = list()
if not self.dropout_layers[lyr].params is None:
for p in self.dropout_layers[lyr].params:
if verbose >=3:
print "... Collecting parameters of layer " + lyr
params_list.append(p.get_value(borrow = True))
params[lyr] = params_list
if verbose >=3:
print "... Collecting parameters of layer " + lyr
params_list = self.dropout_layers[lyr].get_params()
params[lyr] = params_list
return params

def save_params (self, epoch = 0, verbose = 2):
Expand Down

0 comments on commit 63e0fa0

Please sign in to comment.