Skip to content

Commit 7d597b5

Browse files
committed
Fix:minor
1 parent 326b262 commit 7d597b5

11 files changed

Lines changed: 138 additions & 97 deletions

File tree

config/CNN/conv_spec.json

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
{
2-
3-
"comment" : "layers :: convolution layer configuration",
4-
"layers": [
5-
{
6-
"convmat_dim" : [5,5],
7-
"num_filters" : 100,
8-
"poolsize" : [2,2],
9-
"flatten" : false,
10-
"update" : true
11-
},
12-
{
13-
"convmat_dim" : [4,4],
14-
"num_filters" : 200,
15-
"poolsize" : [2,2],
16-
"flatten" : true,
17-
"update" : true
18-
}
2+
"conv_spec":{
3+
"comment" : "layers :: convolution layer configuration",
4+
"layers": [
5+
{
6+
"convmat_dim" : [5,5],
7+
"num_filters" : 100,
8+
"poolsize" : [2,2],
9+
"flatten" : false,
10+
"update" : true
11+
},
12+
{
13+
"convmat_dim" : [4,4],
14+
"num_filters" : 200,
15+
"poolsize" : [2,2],
16+
"flatten" : true,
17+
"update" : true
18+
}
1919
],
2020

21-
"comment" : "activation :: sigmoid or tanh",
22-
"activation" : "tanh",
21+
"comment" : "activation :: sigmoid or tanh",
22+
"activation" : "tanh",
23+
24+
"comment" : "use_fast :: To use pylearn2 library for faster computation or not",
25+
"use_fast" : false
26+
},
27+
"mlp_spec":{
28+
29+
"comment" : "layers :: hidden layer configuration",
30+
"layers": [500,200],
2331

24-
"comment" : "use_fast :: To use pylearn2 library for faster computation or not",
25-
"use_fast" : false
32+
"comment" : "activation :: sigmoid or tanh",
33+
"activation" : "tanh"
34+
}
2635
}

config/CNN/model_conf.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
"comment" : "valid_data (Mandatory) specify the path of the validation data relative to the working directory",
99
"data_spec" : "data_spec.json",
1010

11-
"comment" : "hidden_nnet_spec :: (Mandatory) specify the path of hidden network configuration specification relative to working directory",
12-
"hidden_nnet_spec" : "mlp_spec.json",
13-
14-
"comment" : "conv_nnet_spec :: (Mandatory) specify the path of convolution network configuration specification relative to working directory",
15-
"conv_nnet_spec" : "conv_spec.json",
11+
"comment" : "nnet_spec :: (Mandatory) specify the path of network configuration specification relative to working directory",
12+
"nnet_spec" : "conv_spec.json",
1613

1714
"comment" : "hidden_output_file :: (Mandatory) specify the path of convolution network output file relative to working directory",
1815
"hidden_output_file" : "hidden_out.model",
1916

2017
"comment" : "conv_output_file :: (Mandatory) specify the path of convolution network output file relative to working directory",
2118
"conv_output_file" : "conv_out.model",
2219

20+
"comment" : "conv_output_file :: (Mandatory) specify the path of convolution network output file relative to working directory",
21+
"output_file" : "conv_out.model",
22+
2323
"comment" : "input_shape :: (Mandatory) specify the input shape of a given feature vector" ,
2424
"input_shape" : [3,28,28],
2525

@@ -47,5 +47,13 @@
4747
"min_epoch_decay_start" : 15,
4848
"init_error" :100
4949
},
50+
51+
"processes":{
52+
"pretraining":false,
53+
"finetuning":true,
54+
"testing":true,
55+
"export_data":false
56+
},
57+
5058
"export_path" : "data/export"
5159
}

config/CNN1/model_conf.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
"comment" : "valid_data (Mandatory) specify the path of the validation data relative to the working directory",
99
"data_spec" : "data_spec.json",
1010

11-
"comment" : "hidden_nnet_spec :: (Mandatory) specify the path of hidden network configuration specification relative to working directory",
12-
"hidden_nnet_spec" : "mlp_spec.json",
13-
1411
"comment" : "conv_nnet_spec :: (Mandatory) specify the path of convolution network configuration specification relative to working directory",
15-
"conv_nnet_spec" : "conv_spec.json",
12+
"nnet_spec" : "conv_spec.json",
1613

1714
"comment" : "hidden_output_file :: (Mandatory) specify the path of convolution network output file relative to working directory",
1815
"hidden_output_file" : "hidden_out.model",
@@ -47,5 +44,13 @@
4744
"min_epoch_decay_start" : 15,
4845
"init_error" :100
4946
},
47+
48+
"processes":{
49+
"pretraining":false,
50+
"finetuning":true,
51+
"testing":true,
52+
"export_data":false,
53+
},
54+
5055
"export_path" : "data/export"
5156
}

config/DBN/model_conf.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"data_spec" : "data_spec.json",
1010

1111
"comment" : "rbm_nnet_spec:: (Mandatory) specify the path of RBM network configuration specification relative to working directory",
12-
"rbm_nnet_spec" : "rbm_spec.json",
12+
"nnet_spec" : "rbm_spec.json",
1313

1414
"comment" : "output_file :: (Mandatory) specify the path of RBM network output file relative to working directory",
1515
"output_file" : "rbm_out.model",
@@ -44,7 +44,13 @@
4444
},
4545

4646
"comment" : "finetune_momentum :: Specify the momentum factor while finetuning",
47-
"finetune_momentum": 0.5
48-
47+
"finetune_momentum": 0.5,
48+
49+
"processes":{
50+
"pretraining":false,
51+
"finetuning":true,
52+
"testing":true,
53+
"export_data":false
54+
}
4955

5056
}

config/DBN/rbm_spec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"n_outs":200,
1111

1212
"comment" : "activation :: sigmoid or tanh",
13-
"activation" : "tanh",
13+
"activation" : "sigmoid",
1414

1515
"comment" : "pretrained_layers:number of layers to be pre-trained",
1616
"pretrained_layers" : 5,

config/SDA/model_conf.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"data_spec" : "data_spec.json",
1010

1111
"comment" : "sda_nnet_spec:: (Mandatory) specify the path of SDA network configuration specification relative to working directory",
12-
"sda_nnet_spec" : "sda_spec.json",
12+
"nnet_spec" : "sda_spec.json",
1313

1414
"comment" : "output_file :: (Mandatory) specify the path of SDA network output file relative to working directory",
1515
"output_file" : "sda_out.model",
@@ -41,6 +41,12 @@
4141
},
4242

4343
"comment" : "finetune_momentum :: Specify the momentum factor while finetuning",
44-
"finetune_momentum": 0.5
45-
44+
"finetune_momentum": 0.5,
45+
46+
"processes":{
47+
"pretraining":true,
48+
"finetuning":true,
49+
"testing":true,
50+
"export_data":false
51+
}
4652
}

layers/rbm.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
55
from collections import OrderedDict
66

7+
78
class RBM(object):
89
"""Bernoulli-bernoulli restricted Boltzmann machine (RBM) """
910

1011
def __init__(self, input=None, n_visible=1024, n_hidden=1024,
1112
W = None, hbias = None, vbias = None, numpy_rng = None,
12-
theano_rng = None,activation=T.nnet.sigmoid):
13+
theano_rng = None):
1314

1415
self.n_visible = n_visible
1516
self.n_hidden = n_hidden
16-
self.activation = activation
17+
1718

1819
if numpy_rng is None:
1920
numpy_rng = numpy.random.RandomState(1234)
@@ -44,9 +45,12 @@ def __init__(self, input=None, n_visible=1024, n_hidden=1024,
4445
if not input:
4546
self.input = T.matrix('input')
4647

47-
self.delta_W = theano.shared(value=numpy.zeros_like(W.get_value(borrow=True), dtype=theano.config.floatX), name='delta_W')
48-
self.delta_hbias = theano.shared(value=numpy.zeros_like(hbias.get_value(borrow=True), dtype=theano.config.floatX), name='delta_hbias')
49-
self.delta_vbias = theano.shared(value=numpy.zeros_like(vbias.get_value(borrow=True), dtype=theano.config.floatX), name='delta_vbias')
48+
self.delta_W = theano.shared(value=numpy.zeros_like(W.get_value(borrow=True),
49+
dtype=theano.config.floatX), name='delta_W')
50+
self.delta_hbias = theano.shared(value=numpy.zeros_like(hbias.get_value(borrow=True),
51+
dtype=theano.config.floatX), name='delta_hbias')
52+
self.delta_vbias = theano.shared(value=numpy.zeros_like(vbias.get_value(borrow=True),
53+
dtype=theano.config.floatX), name='delta_vbias')
5054

5155
self.W = W
5256
self.hbias = hbias
@@ -67,7 +71,7 @@ def free_energy(self, v_sample):
6771
def propup(self, vis):
6872
''' Propagate the visible activations up to the hidden units '''
6973
pre_sigmoid_activation = T.dot(vis, self.W) + self.hbias
70-
return [pre_sigmoid_activation, self.activation(pre_sigmoid_activation)]
74+
return [pre_sigmoid_activation, T.nnet.sigmoid(pre_sigmoid_activation)]
7175

7276
def sample_h_given_v(self, v0_sample):
7377
''' Generates hidden unit outputs given visible inputs '''
@@ -82,7 +86,7 @@ def sample_h_given_v(self, v0_sample):
8286
def propdown(self, hid):
8387
'''Propagates the hidden activation downwards to the visible units'''
8488
pre_sigmoid_activation = T.dot(hid, self.W.T) + self.vbias
85-
return [pre_sigmoid_activation, self.activation(pre_sigmoid_activation)]
89+
return [pre_sigmoid_activation, T.nnet.sigmoid(pre_sigmoid_activation)]
8690

8791
def sample_v_given_h(self, h0_sample):
8892
''' Generates visible units given hidden units '''
@@ -106,7 +110,8 @@ def gibbs_vhv(self, v0_sample):
106110
pre_sigmoid_v1, v1_mean, v1_sample = self.sample_v_given_h(h1_sample)
107111
return [pre_sigmoid_h1, h1_mean, h1_sample, pre_sigmoid_v1, v1_mean, v1_sample]
108112

109-
# def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_cost=0.00001, persistent=None, k=1):
113+
# def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5,
114+
# weight_cost=0.00001, persistent=None, k=1):
110115
def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_cost=0.00001):
111116
"""
112117
get the cost and the gradient corresponding to one step of CD-k (k=1)
@@ -117,9 +122,15 @@ def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_c
117122

118123
# gradient of parameters
119124
updates=OrderedDict()
120-
updates[self.delta_W] = momentum * self.delta_W + lr * (1.0/batch_size) * (T.dot(self.input.T, hp_data) - T.dot(v_rec_sigm.T, hp_rec)) - lr * weight_cost * self.W
121-
updates[self.delta_hbias] = momentum * self.delta_hbias + lr * (1.0/batch_size) * (T.sum(h_data, axis=0) - T.sum(hp_rec, axis=0))
122-
updates[self.delta_vbias] = momentum * self.delta_vbias + lr * (1.0/batch_size) * (T.sum(self.input, axis=0) - T.sum(v_rec_sigm, axis=0))
125+
updates[self.delta_W] = (
126+
momentum * self.delta_W + lr * (1.0/batch_size) *(T.dot(self.input.T, hp_data) -
127+
T.dot(v_rec_sigm.T, hp_rec)) - lr * weight_cost * self.W)
128+
updates[self.delta_hbias] = (
129+
momentum * self.delta_hbias + lr * (1.0/batch_size) *
130+
(T.sum(h_data, axis=0) - T.sum(hp_rec, axis=0)))
131+
updates[self.delta_vbias] = (
132+
momentum * self.delta_vbias + lr * (1.0/batch_size) *
133+
(T.sum(self.input, axis=0) - T.sum(v_rec_sigm, axis=0)))
123134

124135
for param, dparam in zip(self.params, self.delta_params):
125136
updates[param] = param + updates[dparam]
@@ -138,14 +149,13 @@ class GBRBM(RBM):
138149

139150
def __init__(self, input=None, n_visible=351, n_hidden=1000,
140151
W = None, hbias = None, vbias = None, numpy_rng = None,
141-
theano_rng = None,activation=T.nnet.sigmoid):
152+
theano_rng = None):
142153

143154
super(GBRBM, self).__init__(input=input, n_visible=n_visible,
144155
n_hidden=n_hidden,
145156
W=W, hbias=hbias,
146157
vbias=vbias, numpy_rng=numpy_rng,
147-
theano_rng=theano_rng,
148-
activation=activation)
158+
theano_rng=theano_rng)
149159

150160
def free_energy(self, v_sample):
151161
''' Compute the free energy '''
@@ -163,7 +173,8 @@ def sample_v_given_h(self, h0_sample):
163173

164174
return [pre_sigmoid_v1, v1_mean, v1_sample]
165175

166-
# def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_cost=0.00001, persistent=None, k = 1):
176+
# def get_cost_updates(self, batch_size = 128, lr = 0.0001,
177+
# momentum=0.5, weight_cost=0.00001, persistent=None, k = 1):
167178
def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_cost=0.00001):
168179

169180
x, hp_data, h_data = self.sample_h_given_v(self.input)
@@ -172,9 +183,15 @@ def get_cost_updates(self, batch_size = 128, lr = 0.0001, momentum=0.5, weight_c
172183

173184
updates=OrderedDict()
174185

175-
updates[self.delta_W] = momentum * self.delta_W + lr * (1.0/batch_size) * (T.dot(self.input.T, hp_data) - T.dot(v_rec.T, hp_rec)) - lr * weight_cost * self.W
176-
updates[self.delta_hbias] = momentum * self.delta_hbias + lr * (1.0/batch_size) * (T.sum(h_data, axis=0) - T.sum(hp_rec, axis=0))
177-
updates[self.delta_vbias] = momentum * self.delta_vbias + lr * (1.0/batch_size) * (T.sum(self.input, axis=0) - T.sum(v_rec, axis=0))
186+
updates[self.delta_W] = (
187+
momentum * self.delta_W + lr * (1.0/batch_size) * (T.dot(self.input.T, hp_data) -
188+
T.dot(v_rec.T, hp_rec)) - lr * weight_cost * self.W)
189+
updates[self.delta_hbias] = (
190+
momentum * self.delta_hbias + lr * (1.0/batch_size) *
191+
(T.sum(h_data, axis=0) - T.sum(hp_rec, axis=0)))
192+
updates[self.delta_vbias] = (
193+
momentum * self.delta_vbias + lr * (1.0/batch_size) *
194+
(T.sum(self.input, axis=0) - T.sum(v_rec, axis=0)))
178195

179196
updates[self.W] = self.W + updates[self.delta_W]
180197
updates[self.hbias] = self.hbias + updates[self.delta_hbias]

models/dbn.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,15 @@ 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,
126-
activation=activation)
125+
hbias=sigmoid_layer.b)
127126
else:
128127
rbm_layer = RBM(numpy_rng=numpy_rng,
129128
theano_rng=theano_rng,
130129
input=layer_input,
131130
n_visible=input_size,
132131
n_hidden=hidden_layers_sizes[i],
133132
W=sigmoid_layer.W,
134-
hbias=sigmoid_layer.b,
135-
activation=activation)
133+
hbias=sigmoid_layer.b)
136134
self.rbm_layers.append(rbm_layer)
137135

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

run/run_DBN.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def runRBM(arg):
9797
else :
9898
model_config = load_model(arg,'RBM')
9999

100-
rbm_config = load_rbm_spec(model_config['rbm_nnet_spec'])
100+
rbm_config = load_rbm_spec(model_config['nnet_spec'])
101101
data_spec = load_data_spec(model_config['data_spec']);
102102

103103

@@ -110,19 +110,21 @@ def runRBM(arg):
110110
createDir(model_config['wdir']);
111111
#create working dir
112112

113+
keep_layer_num = model_config['keep_layer_num']
114+
batch_size = model_config['batch_size']
115+
wdir = model_config['wdir']
116+
117+
113118
dbn = DBN(numpy_rng=numpy_rng, theano_rng = theano_rng, n_ins=rbm_config['n_ins'],
114119
hidden_layers_sizes=rbm_config['hidden_layers'],n_outs=rbm_config['n_outs'],
115120
first_layer_gb = rbm_config['first_layer_gb'],
116121
pretrainedLayers=rbm_config['pretrained_layers'],
117122
activation=activationFn)
118123

124+
119125
train_sets, train_xy, train_x, train_y = read_dataset(data_spec['training'],
120126
model_config['batch_size'])
121127

122-
keep_layer_num = model_config['keep_layer_num']
123-
batch_size = model_config['batch_size']
124-
wdir = model_config['wdir']
125-
126128
if keep_layer_num > 0:
127129
current_nnet = wdir + '/nnet.ptr.current'
128130
logger.info('Initializing model from ' + str(current_nnet) + '....')
@@ -132,10 +134,6 @@ def runRBM(arg):
132134

133135
preTraining(dbn,train_sets,train_xy,train_x,train_y,model_config)
134136

135-
# save the pretrained nnet to file
136-
logger.info('Saving model to ' + str(model_config['output_file']) + '....')
137-
_nnet2file(dbn.sigmoid_layers, filename=model_config['output_file'], withfinal=True)
138-
139137

140138
########################
141139
# FINETUNING THE MODEL #
@@ -164,8 +162,6 @@ def runRBM(arg):
164162
fineTunning(dbn,train_sets,train_xy,train_x,train_y,
165163
valid_sets,valid_xy,valid_x,valid_y,lrate,momentum,batch_size)
166164

167-
logger.info('Saving model to ' + str(model_config['output_file']) + '.final ....')
168-
_nnet2file(dbn.sigmoid_layers, filename=model_config['output_file']+'.final', withfinal=True)
169165

170166
try:
171167
test_sets, test_xy, test_x, test_y = read_dataset(data_spec['testing'],
@@ -178,6 +174,9 @@ def runRBM(arg):
178174

179175
testing(dbn,test_sets, test_xy, test_x, test_y,batch_size)
180176

177+
logger.info('Saving model to ' + str(model_config['output_file']) + ' ....')
178+
_nnet2file(dbn.sigmoid_layers, filename=model_config['output_file'], withfinal=True)
179+
181180

182181

183182
if __name__ == '__main__':

0 commit comments

Comments
 (0)