Skip to content

Commit d1628ff

Browse files
committed
Added:Activation Fn
1 parent 3857a92 commit d1628ff

8 files changed

Lines changed: 63 additions & 26 deletions

File tree

layers/dA.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class dA(object):
6262

6363
def __init__(self, numpy_rng, theano_rng=None, input=None,
6464
n_visible=784, n_hidden=500,
65-
W=None, bhid=None, bvis=None):
65+
W=None, bhid=None, bvis=None,activation=T.nnet.sigmoid):
6666
"""
6767
Initialize the dA class by specifying the number of visible units (the
6868
dimension d of the input ), the number of hidden units ( the dimension
@@ -107,10 +107,13 @@ def __init__(self, numpy_rng, theano_rng=None, input=None,
107107
visible units) that should be shared belong dA and another
108108
architecture; if dA should be standalone set this to None
109109
110+
:type activation: <theano.tensor.elemwise.Elemwise object>
111+
:param activation: activation function
110112
111113
"""
112114
self.n_visible = n_visible
113115
self.n_hidden = n_hidden
116+
self.activation = activation
114117

115118
# create a Theano random generator that gives symbolic random values
116119
if not theano_rng:
@@ -186,14 +189,14 @@ def get_corrupted_input(self, input, corruption_level):
186189

187190
def get_hidden_values(self, input):
188191
""" Computes the values of the hidden layer """
189-
return T.nnet.sigmoid(T.dot(input, self.W) + self.b)
192+
return self.activation(T.dot(input, self.W) + self.b)
190193

191194
def get_reconstructed_input(self, hidden):
192195
"""Computes the reconstructed input given the values of the
193196
hidden layer
194197
195198
"""
196-
return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
199+
return self.activation(T.dot(hidden, self.W_prime) + self.b_prime)
197200

198201
def get_cost_updates(self, corruption_level, learning_rate):
199202
""" This function computes the cost and the updates for one trainng

layers/rbm.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class RBM(object):
99

1010
def __init__(self, input=None, n_visible=1024, n_hidden=1024,
1111
W = None, hbias = None, vbias = None, numpy_rng = None,
12-
theano_rng = None):
12+
theano_rng = None,activation=T.nnet.sigmoid):
1313

1414
self.n_visible = n_visible
1515
self.n_hidden = n_hidden
16+
self.activation = activation
1617

1718
if numpy_rng is None:
1819
numpy_rng = numpy.random.RandomState(1234)
@@ -66,7 +67,7 @@ def free_energy(self, v_sample):
6667
def propup(self, vis):
6768
''' Propagate the visible activations up to the hidden units '''
6869
pre_sigmoid_activation = T.dot(vis, self.W) + self.hbias
69-
return [pre_sigmoid_activation, T.nnet.sigmoid(pre_sigmoid_activation)]
70+
return [pre_sigmoid_activation, self.activation(pre_sigmoid_activation)]
7071

7172
def sample_h_given_v(self, v0_sample):
7273
''' Generates hidden unit outputs given visible inputs '''
@@ -81,7 +82,7 @@ def sample_h_given_v(self, v0_sample):
8182
def propdown(self, hid):
8283
'''Propagates the hidden activation downwards to the visible units'''
8384
pre_sigmoid_activation = T.dot(hid, self.W.T) + self.vbias
84-
return [pre_sigmoid_activation, T.nnet.sigmoid(pre_sigmoid_activation)]
85+
return [pre_sigmoid_activation, self.activation(pre_sigmoid_activation)]
8586

8687
def sample_v_given_h(self, h0_sample):
8788
''' Generates visible units given hidden units '''
@@ -136,11 +137,15 @@ class GBRBM(RBM):
136137
"""Gaussian-bernoulli restricted Boltzmann machine"""
137138

138139
def __init__(self, input=None, n_visible=351, n_hidden=1000,
139-
W = None, hbias = None, vbias = None,
140-
numpy_rng = None, theano_rng = None):
140+
W = None, hbias = None, vbias = None, numpy_rng = None,
141+
theano_rng = None,activation=T.nnet.sigmoid):
141142

142-
super(GBRBM, self).__init__(input=input, n_visible=n_visible, n_hidden=n_hidden,
143-
W=W, hbias=hbias, vbias=vbias, numpy_rng=numpy_rng, theano_rng=theano_rng)
143+
super(GBRBM, self).__init__(input=input, n_visible=n_visible,
144+
n_hidden=n_hidden,
145+
W=W, hbias=hbias,
146+
vbias=vbias, numpy_rng=numpy_rng,
147+
theano_rng=theano_rng,
148+
activation=activation)
144149

145150
def free_energy(self, v_sample):
146151
''' Compute the free energy '''

models/dbn.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DBN(nnet):
2525

2626
def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
2727
hidden_layers_sizes=[500, 500], n_outs=10,
28-
first_layer_gb = True,pretrainedLayers=None):
28+
first_layer_gb = True,pretrainedLayers=None,activation=T.nnet.sigmoid):
2929
"""This class is made to support a variable number of layers.
3030
3131
:type numpy_rng: numpy.random.RandomState
@@ -101,7 +101,7 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
101101
input=layer_input,
102102
n_in=input_size,
103103
n_out=hidden_layers_sizes[i],
104-
activation=T.nnet.sigmoid)
104+
activation=activation)
105105

106106
# add the layer to our list of layers
107107
self.sigmoid_layers.append(sigmoid_layer)
@@ -122,15 +122,17 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
122122
n_visible=input_size,
123123
n_hidden=hidden_layers_sizes[i],
124124
W=sigmoid_layer.W,
125-
hbias=sigmoid_layer.b)
125+
hbias=sigmoid_layer.b,
126+
activation=activation)
126127
else:
127128
rbm_layer = RBM(numpy_rng=numpy_rng,
128129
theano_rng=theano_rng,
129130
input=layer_input,
130131
n_visible=input_size,
131132
n_hidden=hidden_layers_sizes[i],
132133
W=sigmoid_layer.W,
133-
hbias=sigmoid_layer.b)
134+
hbias=sigmoid_layer.b,
135+
activation=activation)
134136
self.rbm_layers.append(rbm_layer)
135137

136138
# We now need to add a logistic layer on top of the MLP

models/sda.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SDA(nnet):
5656

5757
def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
5858
hidden_layers_sizes=[500, 500], n_outs=10,
59-
corruption_levels=[0.1, 0.1]):
59+
corruption_levels=[0.1, 0.1],activation=T.nnet.sigmoid):
6060
""" This class is made to support a variable number of layers.
6161
6262
:type numpy_rng: numpy.random.RandomState
@@ -147,7 +147,8 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
147147
n_visible=input_size,
148148
n_hidden=hidden_layers_sizes[i],
149149
W=sigmoid_layer.W,
150-
bhid=sigmoid_layer.b)
150+
bhid=sigmoid_layer.b,
151+
activation=T.nnet.sigmoid)
151152
self.dA_layers.append(dA_layer)
152153

153154
# We now need to add a logistic layer on top of the MLP

run/run_DBN.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from io_modules.file_reader import read_dataset
3030
from io_modules import setLogger
3131
from utils.learn_rates import LearningRate
32+
from utils.utils import parse_activation
3233
from io_modules.model_io import _nnet2file, _file2nnet
3334

3435
from models import fineTunning,testing
@@ -104,11 +105,14 @@ def runRBM(arg):
104105
numpy_rng = numpy.random.RandomState(rbm_config['random_seed'])
105106
theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
106107

108+
activationFn = parse_activation(rbm_config['activation']);
109+
107110

108111
dbn = DBN(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=rbm_config['n_ins'],
109112
hidden_layers_sizes=rbm_config['hidden_layers'],n_outs=rbm_config['n_outs'],
110113
first_layer_gb = rbm_config['first_layer_gb'],
111-
pretrainedLayers=rbm_config['pretrained_layers'])
114+
pretrainedLayers=rbm_config['pretrained_layers'],
115+
activation=activationFn)
112116

113117
train_sets, train_xy, train_x, train_y = read_dataset(data_spec['training'])
114118

run/run_SDA.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import theano
2323

2424
#module imports
25-
from utils.load_conf import load_model,load_sda_spec,load_data_spec
25+
from io_modules.model_io import _nnet2file, _file2nnet
2626
from io_modules.file_reader import read_dataset
2727
from io_modules import setLogger
2828
from utils.learn_rates import LearningRate
29+
from utils.utils import parse_activation
30+
from utils.load_conf import load_model,load_sda_spec,load_data_spec
31+
2932
from models.sda import SDA
30-
from io_modules.model_io import _nnet2file, _file2nnet
3133
from models import fineTunning,testing
3234

3335

@@ -75,11 +77,14 @@ def runSdA(arg):
7577
numpy_rng = numpy.random.RandomState(sda_config['random_seed'])
7678
#theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
7779

80+
#get Activation function
81+
activationFn = parse_activation(sda_config['activation']);
82+
7883
logger.info('building the model')
7984
# construct the stacked denoising autoencoder class
8085
sda = SDA(numpy_rng=numpy_rng, n_ins=sda_config['n_ins'],
8186
hidden_layers_sizes=sda_config['hidden_layers'],
82-
n_outs=sda_config['n_outs'])
87+
n_outs=sda_config['n_outs'],activation=activationFn)
8388

8489

8590

utils/load_conf.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,22 @@ def load_data_spec(input_file):
9898
logger.info("Loading data specification properties from %s..",input_file)
9999
data = load_json(input_file);
100100
for x in ['training','testing','validation']:
101+
logger.debug('Validating data specification: %s',x)
102+
requiredKeys=['base_path','filename','partition','reader_type']
103+
101104
if not data.has_key(x):
102105
continue;
103106
if not data[x].has_key('keep_flatten') or not type(data[x]['keep_flatten']) is bool:
104-
data[x]['keep_flatten']=False
107+
data[x]['keep_flatten'] = False
108+
109+
if not data[x]['keep_flatten'] :
110+
requiredKeys.append('dim_shuffle');
111+
if not data[x].has_key('random') or not type(data[x]['keep_flatten']) is bool:
112+
data[x]['keep_flatten'] = True
113+
114+
if not isKeysPresents(data[x],requiredKeys):
115+
logger.critical("The mandatory arguments are missing in data spec(%s)",x)
116+
exit(1)
105117
return data
106118

107119

utils/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ def dimshuffle(a,shuffle):
3535
return a
3636

3737

38-
def parse_activation(act_str):
39-
if act_str == 'sigmoid':
40-
return T.nnet.sigmoid
41-
if act_str == 'tanh':
38+
def parse_activation(activation):
39+
if activation == 'tanh':
4240
return T.tanh
43-
return T.nnet.sigmoid
41+
elif activation == 'sigmoid':
42+
return T.nnet.sigmoid
43+
elif activation == 'relu':
44+
return lambda x: x * (x > 0)
45+
elif activation == 'cappedrelu':
46+
return lambda x: T.minimum(x * (x > 0), 6)
47+
else:
48+
raise NotImplementedError
4449

4550
def activation_to_txt(act_func):
4651
if act_func == T.nnet.sigmoid:

0 commit comments

Comments
 (0)