Skip to content

Commit a2b63dc

Browse files
committed
TEST:DNN
1 parent 5c29563 commit a2b63dc

3 files changed

Lines changed: 272 additions & 230 deletions

File tree

run_DNN.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/env python2.7
2+
# Copyright 2014 G.K SUDHARSHAN <sudharpun90@gmail.comIIT Madras
3+
# Copyright 2014 Abil N George<mail@abilng.inIIT Madras
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12+
# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
13+
# WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
14+
# MERCHANTABLITY OR NON-INFRINGEMENT.
15+
# See the Apache 2 License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
import time,sys
19+
import numpy
20+
import theano
21+
22+
import theano
23+
import theano.tensor as T
24+
from theano.tensor.shared_randomstreams import RandomStreams
25+
26+
from models.dnn import DNN
27+
from models.dropout_nnet import DNN_Dropout
28+
from utils.load_conf import load_model,load_sda_spec,load_data_spec
29+
from io_modules.file_reader import read_dataset
30+
from io_modules import setLogger
31+
from utils.learn_rates import LearningRate
32+
33+
from io_modules.model_io import _nnet2file, _file2nnet
34+
from models import fineTunning,testing
35+
36+
import logging
37+
logger = logging.getLogger(__name__)
38+
39+
40+
def runDNN(configFile):
41+
42+
model_config = load_model(configFile)
43+
dnn_config = load_dnn_spec(model_config['dnn_nnet_spec'])
44+
data_spec = load_data_spec(model_config['data_spec']);
45+
46+
47+
#generating Random
48+
numpy_rng = numpy.random.RandomState(dnn_config['random_seed'])
49+
theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
50+
51+
# pretraining
52+
ptr_file = dnn_config['ptr_file']
53+
ptr_layer_number = dnn_config['ptr_layer_number'])
54+
55+
max_col_norm = dnn_config['max_col_norm']
56+
l1_reg = dnn_config['l1_reg']
57+
l2_reg = dnn_config['l2_reg']
58+
59+
60+
# learning rate
61+
lrate = LearningRate.get_instance(model_configs['l_rate_method'],
62+
model_configs['l_rate']);
63+
64+
# batch_size and momentum
65+
batch_size = model_configs['batch_size'];
66+
momentum = model_configs['momentum']
67+
68+
69+
n_ins = dnn_configs['n_ins']
70+
hidden_layers_sizes = dnn_config['hidden_layers']
71+
n_outs = dnn_configs['n_outs']
72+
73+
if dnn_configs['activation'] == 'sigmoid':
74+
activation = T.nnet.sigmoid
75+
else:
76+
activation = T.tanh
77+
78+
do_maxout = dnn_configs['do_maxout']
79+
pool_size = dnn_configs['pool_size']
80+
do_pnorm = dnn_configs['do_pnorm']
81+
pnorm_order = dnn_configs['pnorm_order']
82+
83+
do_dropout = dnn_configs['do_dropout']
84+
dropout_factor = dnn_configs['dropout_factor']
85+
input_dropout_factor = dnn_configs['input_dropout_factor']
86+
87+
train_sets, train_xy, train_x, train_y = read_dataset(data_spec['training'])
88+
valid_sets, valid_xy, valid_x, valid_y = read_dataset(data_spec['validation'])
89+
90+
91+
numpy_rng = numpy.random.RandomState(89677)
92+
theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
93+
94+
logger.info('Building the model')
95+
if do_dropout:
96+
dnn = DNN_Dropout(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=n_ins,
97+
hidden_layers_sizes=hidden_layers_sizes, n_outs=n_outs,
98+
activation = activation, dropout_factor = dropout_factor, input_dropout_factor = input_dropout_factor,
99+
do_maxout = do_maxout, pool_size = pool_size,
100+
max_col_norm = max_col_norm, l1_reg = l1_reg, l2_reg = l2_reg)
101+
else:
102+
dnn = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=n_ins,
103+
hidden_layers_sizes=hidden_layers_sizes, n_outs=n_outs,
104+
activation = activation, do_maxout = do_maxout, pool_size = pool_size,
105+
do_pnorm = do_pnorm, pnorm_order = pnorm_order,
106+
max_col_norm = max_col_norm, l1_reg = l1_reg, l2_reg = l2_reg)
107+
108+
if ptr_layer_number > 0:
109+
_file2nnet(dnn.sigmoid_layers, set_layer_num = ptr_layer_number, filename = ptr_file, withfinal=False)
110+
111+
# get the training, validation and testing function for the model
112+
logger.info('Getting the finetuning functions')
113+
train_fn, valid_fn = dnn.build_finetune_functions(
114+
(train_x, train_y), (valid_x, valid_y),
115+
batch_size=batch_size)
116+
117+
logger.info('Finetunning the model')
118+
start_time = time.clock()
119+
while (lrate.get_rate() != 0):
120+
train_error = []
121+
while (not train_sets.is_finish()):
122+
train_sets.load_next_partition(train_xy)
123+
for batch_index in xrange(train_sets.cur_frame_num / batch_size): # loop over mini-batches
124+
train_error.append(train_fn(index=batch_index, learning_rate = lrate.get_rate(), momentum = momentum))
125+
train_sets.initialize_read()
126+
logger.info('Epoch %d, training error %f' % (lrate.epoch, numpy.mean(train_error)))
127+
128+
valid_error = []
129+
while (not valid_sets.is_finish()):
130+
valid_sets.load_next_partition(valid_xy)
131+
for batch_index in xrange(valid_sets.cur_frame_num / batch_size): # loop over mini-batches
132+
valid_error.append(valid_fn(index=batch_index))
133+
valid_sets.initialize_read()
134+
logger.info('Epoch %d, lrate %f, validation error %f' % (lrate.epoch, lrate.get_rate(), numpy.mean(valid_error)))
135+
136+
lrate.get_next_rate(current_error = 100 * numpy.mean(valid_error))
137+
138+
if do_dropout:
139+
_nnet2file(dnn.sigmoid_layers, filename=wdir + '/nnet.finetune.tmp', input_factor = input_dropout_factor, factor = dropout_factor)
140+
else:
141+
_nnet2file(dnn.sigmoid_layers, filename=wdir + '/nnet.finetune.tmp')
142+
143+
# determine whether it's BNF based on layer sizes
144+
set_layer_num = -1
145+
withfinal = True
146+
bnf_layer_index = 1
147+
while bnf_layer_index < len(hidden_layers_sizes):
148+
if hidden_layers_sizes[bnf_layer_index] < hidden_layers_sizes[bnf_layer_index - 1]:
149+
break
150+
bnf_layer_index = bnf_layer_index + 1
151+
152+
if bnf_layer_index < len(hidden_layers_sizes): # is bottleneck
153+
set_layer_num = bnf_layer_index+1
154+
withfinal = False
155+
156+
end_time = time.clock()
157+
158+
logger.info('The Training ran for %.2fm' % ((end_time - start_time) / 60.))
159+
160+
if do_maxout:
161+
_nnet2janus_maxout(nnet_spec, pool_size = pool_size, set_layer_num = set_layer_num, filein = wdir + '/nnet.finetune.tmp', fileout = output_file, withfinal=withfinal)
162+
else:
163+
_nnet2janus(nnet_spec, set_layer_num = set_layer_num, filein = wdir + '/nnet.finetune.tmp', fileout = output_file, withfinal=withfinal)
164+
165+
166+
167+
168+
if __name__ == '__main__':
169+
import sys
170+
setLogger(level="INFO");
171+
logger.info('Stating....');
172+
runDNN(sys.argv[1]);
173+
sys.exit(0)

0 commit comments

Comments
 (0)