Skip to content

Commit 08fa94c

Browse files
committed
Merge pull request #1 from IITM-DONLAB/devel
Added DNN
2 parents 14651dd + c71bb91 commit 08fa94c

10 files changed

Lines changed: 479 additions & 41 deletions

File tree

config/DNN/README

Whitespace-only changes.

config/DNN/data_spec.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"validation": {
3+
"base_path" : "data/NPFILE",
4+
"filename" : "val.dat",
5+
"partition" : 200,
6+
"random" : true,
7+
"random_seed" : 123,
8+
"keep_flatten" : true,
9+
"reader_type" : "NP"
10+
},
11+
12+
"training" : {
13+
"base_path" : "data/NPFILE",
14+
"filename" : "train.dat",
15+
"partition" : 200,
16+
"random" : true,
17+
"random_seed" : 123,
18+
"keep_flatten" : true,
19+
"reader_type" : "NP"
20+
},
21+
22+
"testing" : {
23+
"base_path" : "data/NPFILE",
24+
"filename" : "train.dat",
25+
"partition" : 200,
26+
"random" : true,
27+
"random_seed" : 123,
28+
"keep_flatten" : true,
29+
"reader_type" : "NP"
30+
}
31+
}

config/DNN/dnn_spec.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{
3+
4+
"comment" : "layers :: RBM layer configuration (No: of Nodes)",
5+
"hidden_layers": [ 2350,1024,1024,1024,1024,1901 ],
6+
7+
"comment" : "activation :: sigmoid or tanh",
8+
"activation" : "sigmoid",
9+
10+
"comment" : "pretrained_layers:number of layers to be pre-trained",
11+
"pretrained_layers" : 5,
12+
13+
"comment" : "random_seed::",
14+
"random_seed" : 89677,
15+
16+
"comment" :"regularization for hidden layer parameter",
17+
"max_col_norm":null,
18+
"l1_reg":null,
19+
"l2_reg":null,
20+
21+
"do_maxout":false,
22+
"pool_size":1,
23+
"do_pnorm": false,
24+
"pnorm_order": 1,
25+
26+
"do_dropout": false,
27+
"dropout_factor": [0.0],
28+
"input_dropout_factor": 0.0
29+
}

config/DNN/model_conf.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"comment" : "nnetType :: (Mandatory) specify Type of Network (CNN,RBM) ",
3+
"nnetType" : "DNN",
4+
5+
"comment" : "train_data :: (Mandatory) specify the working directory containing data configuration and output ",
6+
"wdir" : "wdir",
7+
8+
"comment" : "valid_data (Mandatory) specify the path of the validation data relative current directory",
9+
"data_spec" : "data_spec.json",
10+
11+
"comment" : "rbm_nnet_spec:: (Mandatory) specify the path of RBM network configuration specification relative to current directory",
12+
"nnet_spec" : "dnn_spec.json",
13+
14+
"comment" : "output_file :: (Mandatory) specify the path of RBM network output file relative to working directory",
15+
"output_file" : "dnn_out.model",
16+
17+
"comment" : "batch_size :: specify the mini batch size while training, default 128",
18+
"batch_size" : 128,
19+
20+
"comment": "",
21+
"n_ins":2352,
22+
23+
"comment":"",
24+
"n_outs":200,
25+
26+
"comment" : "input_file :: (Mandatory) specify the path of PreTrained network input file relative to working directory",
27+
"input_file" : "rbm_in.model",
28+
29+
"comment" : "finetune_method:: Two methods are supported C: Constant learning rate and E : Exponential decay",
30+
"finetune_method":"C",
31+
32+
"comment" : "finetune_rate :: learning rate configuration",
33+
"finetune_rate" : {
34+
"learning_rate" : 0.08,
35+
"epoch_num" : 10,
36+
37+
"start_rate" : 0.08,
38+
"scale_by" : 0.5,
39+
"min_derror_decay_start" : 0.05,
40+
"min_derror_stop" : 0.05,
41+
"min_epoch_decay_start" : 15,
42+
"init_error" :100
43+
},
44+
45+
"comment" : "finetune_momentum :: Specify the momentum factor while finetuning",
46+
"finetune_momentum": 0.5,
47+
48+
"processes":{
49+
"pretraining":false,
50+
"finetuning":true,
51+
"testing":false,
52+
"export_data":false
53+
}
54+
55+
}

layers/mlp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def __init__(self, rng, input, n_in, n_out, W=None, b=None, activation=T.tanh,
6464
super(DropoutHiddenLayer, self).__init__(rng=rng, input=input, n_in=n_in, n_out=n_out, W=W, b=b,
6565
activation=activation, do_maxout = do_maxout, pool_size = pool_size)
6666
self.theano_rng = RandomStreams(rng.randint(2 ** 30))
67-
dropout_prob = self.theano_rng.binomial(n=1, p=1-dropout_factor, size=self.output.shape, dtype=theano.config.floatX)
67+
dropout_prob = self.theano_rng.binomial(n=1, p=1-dropout_factor, size=self.output.shape,
68+
dtype=theano.config.floatX)
6869
self.dropout_output = dropout_prob * self.output
6970

71+
def _dropout_from_layer(theano_rng, hid_out, p):
72+
""" p is the factor for dropping a unit """
73+
# p=1-p because 1's indicate keep and p is prob of dropping
74+
return theano_rng.binomial(n=1, p=1-p, size=hid_out.shape,dtype=theano.config.floatX) * hid_out
75+

models/dnn.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import time
2121

2222
import numpy
23+
from collections import OrderedDict
2324

2425
import theano
2526
import theano.tensor as T
@@ -115,20 +116,47 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
115116
W = self.params[i * 2]
116117
self.finetune_cost += self.l2_reg * T.sqr(W).sum()
117118

119+
self.output = self.logLayer.prediction();
120+
self.features = self.sigmoid_layers[-2].output;
121+
118122
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size):
123+
"""
124+
Generates a function `train` that implements one step of
125+
finetuning and a function `validate` that computes the error on
126+
a batch from the validation set
127+
128+
:type train_shared_xy: pairs of theano.tensor.TensorType
129+
:param train_shared_xy: It is a list that contain all the train dataset,
130+
pair is formed of two Theano variables, one for the datapoints,
131+
the other for the labels
132+
133+
:type valid_shared_xy: pairs of theano.tensor.TensorType
134+
:param valid_shared_xy: It is a list that contain all the valid dataset,
135+
pair is formed of two Theano variables, one for the datapoints,
136+
the other for the labels
137+
138+
:type batch_size: int
139+
:param batch_size: size of a minibatch
140+
141+
:returns (theano.function,theano.function)
142+
* A function for training takes minibatch_index,learning_rate,momentum
143+
which updates weights,and return error rate
144+
* A function for validation takes minibatch_indexand return error rate
145+
146+
"""
119147

120148
(train_set_x, train_set_y) = train_shared_xy
121149
(valid_set_x, valid_set_y) = valid_shared_xy
122150

123151
index = T.lscalar('index') # index to a [mini]batch
124-
learning_rate = T.fscalar('learning_rate')
125-
momentum = T.fscalar('momentum')
152+
learning_rate = T.scalar('learning_rate',dtype=theano.config.floatX)
153+
momentum = T.scalar('momentum',dtype=theano.config.floatX)
126154

127155
# compute the gradients with respect to the model parameters
128156
gparams = T.grad(self.finetune_cost, self.params)
129157

130158
# compute list of fine-tuning updates
131-
updates = {}
159+
updates = OrderedDict()
132160
for dparam, gparam in zip(self.delta_params, gparams):
133161
updates[dparam] = momentum * dparam - gparam*learning_rate
134162
for dparam, param in zip(self.delta_params, self.params):

models/dropout_nnet.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import time
2121

2222
import numpy
23+
from collections import OrderedDict
2324

2425
import theano
2526
import theano.tensor as T
@@ -30,6 +31,7 @@
3031

3132
from models import nnet
3233

34+
3335
class DNN_Dropout(nnet):
3436

3537
def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
@@ -127,6 +129,9 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
127129
self.finetune_cost = self.dropout_logLayer.negative_log_likelihood(self.y)
128130
self.errors = self.logLayer.errors(self.y)
129131

132+
self.output = self.logLayer.prediction();
133+
self.features = self.sigmoid_layers[-2].output;
134+
130135
if self.l1_reg is not None:
131136
for i in xrange(self.n_layers):
132137
W = self.params[i * 2]
@@ -138,19 +143,43 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
138143
self.finetune_cost += self.l2_reg * T.sqr(W).sum()
139144

140145
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size):
146+
"""
147+
Generates a function `train` that implements one step of
148+
finetuning and a function `validate` that computes the error on
149+
a batch from the validation set
150+
151+
:type train_shared_xy: pairs of theano.tensor.TensorType
152+
:param train_shared_xy: It is a list that contain all the train dataset,
153+
pair is formed of two Theano variables, one for the datapoints,
154+
the other for the labels
155+
156+
:type valid_shared_xy: pairs of theano.tensor.TensorType
157+
:param valid_shared_xy: It is a list that contain all the valid dataset,
158+
pair is formed of two Theano variables, one for the datapoints,
159+
the other for the labels
160+
161+
:type batch_size: int
162+
:param batch_size: size of a minibatch
163+
164+
:returns (theano.function,theano.function)
165+
* A function for training takes minibatch_index,learning_rate,momentum
166+
which updates weights,and return error rate
167+
* A function for validation takes minibatch_indexand return error rate
168+
169+
"""
141170

142171
(train_set_x, train_set_y) = train_shared_xy
143172
(valid_set_x, valid_set_y) = valid_shared_xy
144173

145174
index = T.lscalar('index') # index to a [mini]batch
146-
learning_rate = T.fscalar('learning_rate')
147-
momentum = T.fscalar('momentum')
175+
learning_rate = T.scalar('learning_rate',dtype=theano.config.floatX)
176+
momentum = T.scalar('momentum',dtype=theano.config.floatX)
148177

149178
# compute the gradients with respect to the model parameters
150179
gparams = T.grad(self.finetune_cost, self.params)
151180

152181
# compute list of fine-tuning updates
153-
updates = {}
182+
updates = OrderedDict()
154183
for dparam, gparam in zip(self.delta_params, gparams):
155184
updates[dparam] = momentum * dparam - gparam*learning_rate
156185
for dparam, param in zip(self.delta_params, self.params):

run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def runNet(modelConfig):
5454
from run.run_DBN import runRBM as runModel
5555
elif nnetType == 'SDA':
5656
from run.run_SDA import runSdA as runModel
57+
elif nnetType == 'DNN':
58+
from run.run_DNN import runDNN as runModel
5759
else :
5860
logger.error('Unknown nnet Type')
5961
return 1

0 commit comments

Comments
 (0)