Skip to content

Commit 9037aba

Browse files
cedricfarinazzopoyea
authored andcommitted
Fix spelling in neural_network/convolution_neural_network.py (TheAlgorithms#849)
* Fix spelling in neural_network/convolution_neural_network.py * fix import Signed-off-by: cedric.farinazzo <[email protected]>
1 parent fc95e7a commit 9037aba

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

‎neural_network/convolution_neural_network.py‎

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
1515
Date: 2017.9.20
1616
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
17-
'''
17+
'''
1818
from __future__ importprint_function
1919

20+
importpickle
2021
importnumpyasnp
2122
importmatplotlib.pyplotasplt
2223

2324
classCNN():
2425

25-
def__init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.2):
26+
def__init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.2):
2627
'''
2728
:param conv1_get: [a,c,d],size, number, step of convolution kernel
2829
:param size_p1: pooling size
@@ -48,32 +49,30 @@ def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.
4849
self.thre_bp3=-2*np.random.rand(self.num_bp3)+1
4950

5051

51-
defsave_model(self,save_path):
52+
defsave_model(self,save_path):
5253
#save model dict with pickle
53-
importpickle
5454
model_dic={'num_bp1':self.num_bp1,
55-
'num_bp2':self.num_bp2,
56-
'num_bp3':self.num_bp3,
57-
'conv1':self.conv1,
58-
'step_conv1':self.step_conv1,
59-
'size_pooling1':self.size_pooling1,
60-
'rate_weight':self.rate_weight,
61-
'rate_thre':self.rate_thre,
62-
'w_conv1':self.w_conv1,
63-
'wkj':self.wkj,
64-
'vji':self.vji,
65-
'thre_conv1':self.thre_conv1,
66-
'thre_bp2':self.thre_bp2,
67-
'thre_bp3':self.thre_bp3}
55+
'num_bp2':self.num_bp2,
56+
'num_bp3':self.num_bp3,
57+
'conv1':self.conv1,
58+
'step_conv1':self.step_conv1,
59+
'size_pooling1':self.size_pooling1,
60+
'rate_weight':self.rate_weight,
61+
'rate_thre':self.rate_thre,
62+
'w_conv1':self.w_conv1,
63+
'wkj':self.wkj,
64+
'vji':self.vji,
65+
'thre_conv1':self.thre_conv1,
66+
'thre_bp2':self.thre_bp2,
67+
'thre_bp3':self.thre_bp3}
6868
withopen(save_path, 'wb') asf:
6969
pickle.dump(model_dic, f)
7070

7171
print('Model saved: %s'%save_path)
7272

7373
@classmethod
74-
defReadModel(cls,model_path):
74+
defReadModel(cls,model_path):
7575
#read saved model
76-
importpickle
7776
withopen(model_path, 'rb') asf:
7877
model_dic=pickle.load(f)
7978

@@ -97,13 +96,13 @@ def ReadModel(cls,model_path):
9796
returnconv_ins
9897

9998

100-
defsig(self,x):
99+
defsig(self,x):
101100
return1/ (1+np.exp(-1*x))
102101

103-
defdo_round(self,x):
102+
defdo_round(self,x):
104103
returnround(x, 3)
105104

106-
defconvolute(self,data,convs,w_convs,thre_convs,conv_step):
105+
defconvolute(self,data,convs,w_convs,thre_convs,conv_step):
107106
#convolution process
108107
size_conv=convs[0]
109108
num_conv=convs[1]
@@ -132,7 +131,7 @@ def convolute(self,data,convs,w_convs,thre_convs,conv_step):
132131
focus_list=np.asarray(focus1_list)
133132
returnfocus_list,data_featuremap
134133

135-
defpooling(self,featuremaps,size_pooling,type='average_pool'):
134+
defpooling(self,featuremaps,size_pooling,type='average_pool'):
136135
#pooling process
137136
size_map=len(featuremaps[0])
138137
size_pooled=int(size_map/size_pooling)
@@ -153,7 +152,7 @@ def pooling(self,featuremaps,size_pooling,type='average_pool'):
153152
featuremap_pooled.append(map_pooled)
154153
returnfeaturemap_pooled
155154

156-
def_expand(self,datas):
155+
def_expand(self,datas):
157156
#expanding three dimension data to one dimension list
158157
data_expanded= []
159158
foriinrange(len(datas)):
@@ -164,14 +163,14 @@ def _expand(self,datas):
164163
data_expanded=np.asarray(data_expanded)
165164
returndata_expanded
166165

167-
def_expand_mat(self,data_mat):
166+
def_expand_mat(self,data_mat):
168167
#expanding matrix to one dimension list
169168
data_mat=np.asarray(data_mat)
170169
shapes=np.shape(data_mat)
171170
data_expanded=data_mat.reshape(1,shapes[0]*shapes[1])
172171
returndata_expanded
173172

174-
def_calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_pooling):
173+
def_calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_pooling):
175174
'''
176175
calcluate the gradient from the data slice of pool layer
177176
pd_pool: list of matrix
@@ -190,7 +189,7 @@ def _calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_poo
190189
pd_all.append(pd_conv2)
191190
returnpd_all
192191

193-
deftrian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e=bool):
192+
deftrain(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e=bool):
194193
#model traning
195194
print('----------------------Start Training-------------------------')
196195
print((' - - Shape: Train_Data ',np.shape(datas_train)))
@@ -206,7 +205,7 @@ def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_
206205
data_train=np.asmatrix(datas_train[p])
207206
data_teach=np.asarray(datas_teach[p])
208207
data_focus1,data_conved1=self.convolute(data_train,self.conv1,self.w_conv1,
209-
self.thre_conv1,conv_step=self.step_conv1)
208+
self.thre_conv1,conv_step=self.step_conv1)
210209
data_pooled1=self.pooling(data_conved1,self.size_pooling1)
211210
shape_featuremap1=np.shape(data_conved1)
212211
'''
@@ -231,7 +230,7 @@ def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_
231230
pd_conv1_pooled=pd_i_all/ (self.size_pooling1*self.size_pooling1)
232231
pd_conv1_pooled=pd_conv1_pooled.T.getA().tolist()
233232
pd_conv1_all=self._calculate_gradient_from_pool(data_conved1,pd_conv1_pooled,shape_featuremap1[0],
234-
shape_featuremap1[1],self.size_pooling1)
233+
shape_featuremap1[1],self.size_pooling1)
235234
#weight and threshold learning process---------
236235
#convolution layer
237236
fork_convinrange(self.conv1[1]):
@@ -268,15 +267,15 @@ def draw_error():
268267
draw_error()
269268
returnmse
270269

271-
defpredict(self,datas_test):
270+
defpredict(self,datas_test):
272271
#model predict
273272
produce_out= []
274273
print('-------------------Start Testing-------------------------')
275274
print((' - - Shape: Test_Data ',np.shape(datas_test)))
276275
forpinrange(len(datas_test)):
277276
data_test=np.asmatrix(datas_test[p])
278277
data_focus1, data_conved1=self.convolute(data_test, self.conv1, self.w_conv1,
279-
self.thre_conv1, conv_step=self.step_conv1)
278+
self.thre_conv1, conv_step=self.step_conv1)
280279
data_pooled1=self.pooling(data_conved1, self.size_pooling1)
281280
data_bp_input=self._expand(data_pooled1)
282281

@@ -289,11 +288,11 @@ def predict(self,datas_test):
289288
res= [list(map(self.do_round,each)) foreachinproduce_out]
290289
returnnp.asarray(res)
291290

292-
defconvolution(self,data):
291+
defconvolution(self,data):
293292
#return the data of image after convoluting process so we can check it out
294293
data_test=np.asmatrix(data)
295294
data_focus1, data_conved1=self.convolute(data_test, self.conv1, self.w_conv1,
296-
self.thre_conv1, conv_step=self.step_conv1)
295+
self.thre_conv1, conv_step=self.step_conv1)
297296
data_pooled1=self.pooling(data_conved1, self.size_pooling1)
298297

299298
returndata_conved1,data_pooled1
@@ -303,4 +302,4 @@ def convolution(self,data):
303302
pass
304303
'''
305304
I will put the example on other file
306-
'''
305+
'''

0 commit comments

Comments
(0)