Fix spelling in neural_network/convolution_neural_network.py (#849)

* Fix spelling in neural_network/convolution_neural_network.py

* fix import

Signed-off-by: cedric.farinazzo <cedric.farinazzo@epita.fr>
This commit is contained in:
cedricfarinazzo 2019-05-30 02:47:00 +02:00 committed by John Law
parent fc95e7a91a
commit 9037abae11

View File

@ -14,15 +14,16 @@
Github: 245885195@qq.com Github: 245885195@qq.com
Date: 2017.9.20 Date: 2017.9.20
- - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
''' '''
from __future__ import print_function from __future__ import print_function
import pickle
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
class CNN(): 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 conv1_get: [a,c,d]size, number, step of convolution kernel
:param size_p1: pooling size :param size_p1: pooling size
@ -48,9 +49,8 @@ class CNN():
self.thre_bp3 = -2*np.random.rand(self.num_bp3)+1 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 #save model dict with pickle
import pickle
model_dic = {'num_bp1':self.num_bp1, model_dic = {'num_bp1':self.num_bp1,
'num_bp2':self.num_bp2, 'num_bp2':self.num_bp2,
'num_bp3':self.num_bp3, 'num_bp3':self.num_bp3,
@ -71,9 +71,8 @@ class CNN():
print('Model saved %s'% save_path) print('Model saved %s'% save_path)
@classmethod @classmethod
def ReadModel(cls,model_path): def ReadModel(cls, model_path):
#read saved model #read saved model
import pickle
with open(model_path, 'rb') as f: with open(model_path, 'rb') as f:
model_dic = pickle.load(f) model_dic = pickle.load(f)
@ -97,13 +96,13 @@ class CNN():
return conv_ins return conv_ins
def sig(self,x): def sig(self, x):
return 1 / (1 + np.exp(-1*x)) return 1 / (1 + np.exp(-1*x))
def do_round(self,x): def do_round(self, x):
return round(x, 3) 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 #convolution process
size_conv = convs[0] size_conv = convs[0]
num_conv =convs[1] num_conv =convs[1]
@ -132,7 +131,7 @@ class CNN():
focus_list = np.asarray(focus1_list) focus_list = np.asarray(focus1_list)
return focus_list,data_featuremap 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 #pooling process
size_map = len(featuremaps[0]) size_map = len(featuremaps[0])
size_pooled = int(size_map/size_pooling) size_pooled = int(size_map/size_pooling)
@ -153,7 +152,7 @@ class CNN():
featuremap_pooled.append(map_pooled) featuremap_pooled.append(map_pooled)
return featuremap_pooled return featuremap_pooled
def _expand(self,datas): def _expand(self, datas):
#expanding three dimension data to one dimension list #expanding three dimension data to one dimension list
data_expanded = [] data_expanded = []
for i in range(len(datas)): for i in range(len(datas)):
@ -164,14 +163,14 @@ class CNN():
data_expanded = np.asarray(data_expanded) data_expanded = np.asarray(data_expanded)
return data_expanded return data_expanded
def _expand_mat(self,data_mat): def _expand_mat(self, data_mat):
#expanding matrix to one dimension list #expanding matrix to one dimension list
data_mat = np.asarray(data_mat) data_mat = np.asarray(data_mat)
shapes = np.shape(data_mat) shapes = np.shape(data_mat)
data_expanded = data_mat.reshape(1,shapes[0]*shapes[1]) data_expanded = data_mat.reshape(1,shapes[0]*shapes[1])
return data_expanded 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 calcluate the gradient from the data slice of pool layer
pd_pool: list of matrix pd_pool: list of matrix
@ -190,7 +189,7 @@ class CNN():
pd_all.append(pd_conv2) pd_all.append(pd_conv2)
return pd_all 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 #model traning
print('----------------------Start Training-------------------------') print('----------------------Start Training-------------------------')
print((' - - Shape: Train_Data ',np.shape(datas_train))) print((' - - Shape: Train_Data ',np.shape(datas_train)))
@ -268,7 +267,7 @@ class CNN():
draw_error() draw_error()
return mse return mse
def predict(self,datas_test): def predict(self, datas_test):
#model predict #model predict
produce_out = [] produce_out = []
print('-------------------Start Testing-------------------------') print('-------------------Start Testing-------------------------')
@ -289,7 +288,7 @@ class CNN():
res = [list(map(self.do_round,each)) for each in produce_out] res = [list(map(self.do_round,each)) for each in produce_out]
return np.asarray(res) 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 #return the data of image after convoluting process so we can check it out
data_test = np.asmatrix(data) data_test = np.asmatrix(data)
data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1, data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1,
@ -303,4 +302,4 @@ if __name__ == '__main__':
pass pass
''' '''
I will put the example on other file I will put the example on other file
''' '''