Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix spelling in neural_network/convolution_neural_network.py
  • Loading branch information
cedricfarinazzo authored May 28, 2019
commit bf2ed759cfb1ff1de79b85a9d39a1aac5b5e5999
28 changes: 14 additions & 14 deletions neural_network/convolution_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class CNN():

def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.2):
def __init__(self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, rate_t=0.2):
'''
:param conv1_get: [a,c,d],size, number, step of convolution kernel
:param size_p1: pooling size
Expand All @@ -48,7 +48,7 @@ def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.
self.thre_bp3 = -2*np.random.rand(self.num_bp3)+1


def save_model(self,save_path):
def save_model(self, save_path):
#save model dict with pickle
import pickle
model_dic = {'num_bp1':self.num_bp1,
Expand All @@ -71,7 +71,7 @@ def save_model(self,save_path):
print('Model saved: %s'% save_path)

@classmethod
def ReadModel(cls,model_path):
def ReadModel(cls, model_path):
#read saved model
import pickle
with open(model_path, 'rb') as f:
Expand All @@ -97,13 +97,13 @@ def ReadModel(cls,model_path):
return conv_ins


def sig(self,x):
def sig(self, x):
return 1 / (1 + np.exp(-1*x))

def do_round(self,x):
def do_round(self, x):
return round(x, 3)

def convolute(self,data,convs,w_convs,thre_convs,conv_step):
def convolute(self, data, convs, w_convs, thre_convs, conv_step):
#convolution process
size_conv = convs[0]
num_conv =convs[1]
Expand Down Expand Up @@ -132,7 +132,7 @@ def convolute(self,data,convs,w_convs,thre_convs,conv_step):
focus_list = np.asarray(focus1_list)
return focus_list,data_featuremap

def pooling(self,featuremaps,size_pooling,type='average_pool'):
def pooling(self, featuremaps, size_pooling, type='average_pool'):
#pooling process
size_map = len(featuremaps[0])
size_pooled = int(size_map/size_pooling)
Expand All @@ -153,7 +153,7 @@ def pooling(self,featuremaps,size_pooling,type='average_pool'):
featuremap_pooled.append(map_pooled)
return featuremap_pooled

def _expand(self,datas):
def _expand(self, datas):
#expanding three dimension data to one dimension list
data_expanded = []
for i in range(len(datas)):
Expand All @@ -164,14 +164,14 @@ def _expand(self,datas):
data_expanded = np.asarray(data_expanded)
return data_expanded

def _expand_mat(self,data_mat):
def _expand_mat(self, data_mat):
#expanding matrix to one dimension list
data_mat = np.asarray(data_mat)
shapes = np.shape(data_mat)
data_expanded = data_mat.reshape(1,shapes[0]*shapes[1])
return data_expanded

def _calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_pooling):
def _calculate_gradient_from_pool(self, out_map, pd_pool,num_map, size_map, size_pooling):
'''
calcluate the gradient from the data slice of pool layer
pd_pool: list of matrix
Expand All @@ -190,7 +190,7 @@ def _calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_poo
pd_all.append(pd_conv2)
return pd_all

def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e = bool):
def train(self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, draw_e = bool):
#model traning
print('----------------------Start Training-------------------------')
print((' - - Shape: Train_Data ',np.shape(datas_train)))
Expand Down Expand Up @@ -268,7 +268,7 @@ def draw_error():
draw_error()
return mse

def predict(self,datas_test):
def predict(self, datas_test):
#model predict
produce_out = []
print('-------------------Start Testing-------------------------')
Expand All @@ -289,7 +289,7 @@ def predict(self,datas_test):
res = [list(map(self.do_round,each)) for each in produce_out]
return np.asarray(res)

def convolution(self,data):
def convolution(self, data):
#return the data of image after convoluting process so we can check it out
data_test = np.asmatrix(data)
data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1,
Expand All @@ -303,4 +303,4 @@ def convolution(self,data):
pass
'''
I will put the example on other file
'''
'''