
import pickle

import numpy as np
from matplotlib import pyplot as plt






class        SCREAMING_SNAKE_CASE__    :

							"""simple docstring"""






							def __init__(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=0.2     ,	SCREAMING_SNAKE_CASE_=0.2			)->   Any:





													'''simple docstring'''






													__UpperCamelCase       =			bp_numa
													__UpperCamelCase       =			bp_numa
													__UpperCamelCase       =			bp_numa
													__UpperCamelCase       =			conva_get[:2]
													__UpperCamelCase       =			conva_get[2]
													__UpperCamelCase       =			size_pa
													__UpperCamelCase       =			rate_w
													__UpperCamelCase       =			rate_t
													__UpperCamelCase       =			[
													    np.mat(-1 * np.random.rand(self.conva[0]     ,	self.conva[0]			) + 0.5			)
													    for i in range(self.conva[1]			)
													]
													__UpperCamelCase       =			np.mat(-1 * np.random.rand(self.num_bpa     ,	self.num_bpa			) + 0.5			)
													__UpperCamelCase       =			np.mat(-1 * np.random.rand(self.num_bpa     ,	self.num_bpa			) + 0.5			)
													__UpperCamelCase       =			-2 * np.random.rand(self.conva[1]			) + 1
													__UpperCamelCase       =			-2 * np.random.rand(self.num_bpa			) + 1
													__UpperCamelCase       =			-2 * np.random.rand(self.num_bpa			) + 1



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   List[str]:





													'''simple docstring'''






													__UpperCamelCase       =			{
													    '''num_bp1''': self.num_bpa,
													    '''num_bp2''': self.num_bpa,
													    '''num_bp3''': self.num_bpa,
													    '''conv1''': self.conva,
													    '''step_conv1''': self.step_conva,
													    '''size_pooling1''': self.size_poolinga,
													    '''rate_weight''': self.rate_weight,
													    '''rate_thre''': self.rate_thre,
													    '''w_conv1''': self.w_conva,
													    '''wkj''': self.wkj,
													    '''vji''': self.vji,
													    '''thre_conv1''': self.thre_conva,
													    '''thre_bp2''': self.thre_bpa,
													    '''thre_bp3''': self.thre_bpa,
													}
													with open(SCREAMING_SNAKE_CASE_     ,	'''wb'''			) as f:
																			pickle.dump(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)

													print(F"Model saved： {save_path}"			)



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_			)->   List[str]:





													'''simple docstring'''






													with open(SCREAMING_SNAKE_CASE_     ,	'''rb'''			) as f:
																			__UpperCamelCase       =			pickle.load(SCREAMING_SNAKE_CASE_			)  # noqa: S301

													__UpperCamelCase       =			model_dic.get('''conv1'''			)
													conv_get.append(model_dic.get('''step_conv1'''			)			)
													__UpperCamelCase       =			model_dic.get('''size_pooling1'''			)
													__UpperCamelCase       =			model_dic.get('''num_bp1'''			)
													__UpperCamelCase       =			model_dic.get('''num_bp2'''			)
													__UpperCamelCase       =			model_dic.get('''num_bp3'''			)
													__UpperCamelCase       =			model_dic.get('''rate_weight'''			)
													__UpperCamelCase       =			model_dic.get('''rate_thre'''			)
													# create model instance
													__UpperCamelCase       =			CNN(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)
													# modify model parameter
													__UpperCamelCase       =			model_dic.get('''w_conv1'''			)
													__UpperCamelCase       =			model_dic.get('''wkj'''			)
													__UpperCamelCase       =			model_dic.get('''vji'''			)
													__UpperCamelCase       =			model_dic.get('''thre_conv1'''			)
													__UpperCamelCase       =			model_dic.get('''thre_bp2'''			)
													__UpperCamelCase       =			model_dic.get('''thre_bp3'''			)
													return conv_ins



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   Any:





													'''simple docstring'''






													return 1 / (1 + np.exp(-1 * x			))



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   int:





													'''simple docstring'''






													return round(SCREAMING_SNAKE_CASE_     ,	3			)



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)->   Any:





													'''simple docstring'''






													__UpperCamelCase       =			convs[0]
													__UpperCamelCase       =			convs[1]
													__UpperCamelCase       =			np.shape(SCREAMING_SNAKE_CASE_			)[0]
													# get the data slice of original image data, data_focus
													__UpperCamelCase       =			[]
													for i_focus in range(0     ,	size_data - size_conv + 1     ,	SCREAMING_SNAKE_CASE_			):
																			for j_focus in range(0     ,	size_data - size_conv + 1     ,	SCREAMING_SNAKE_CASE_			):
																									__UpperCamelCase       =			data[
																									    i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
																									]
																									data_focus.append(SCREAMING_SNAKE_CASE_			)
        # calculate the feature map of every single kernel, and saved as list of matrix
													__UpperCamelCase       =			[]
													__UpperCamelCase       =			int((size_data - size_conv) / conv_step + 1			)
													for i_map in range(SCREAMING_SNAKE_CASE_			):
																			__UpperCamelCase       =			[]
																			for i_focus in range(len(SCREAMING_SNAKE_CASE_			)			):
																									__UpperCamelCase       =			(
																									    np.sum(np.multiply(data_focus[i_focus]     ,	w_convs[i_map]			)			)
																									    - thre_convs[i_map]
																									)
																									featuremap.append(self.sig(SCREAMING_SNAKE_CASE_			)			)
																			__UpperCamelCase       =			np.asmatrix(SCREAMING_SNAKE_CASE_			).reshape(
																			    SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)
																			data_featuremap.append(SCREAMING_SNAKE_CASE_			)

													# expanding the data slice to One dimenssion
													__UpperCamelCase       =			[]
													for each_focus in data_focus:
																			focusa_list.extend(self.Expand_Mat(SCREAMING_SNAKE_CASE_			)			)
													__UpperCamelCase       =			np.asarray(SCREAMING_SNAKE_CASE_			)
													return focus_list, data_featuremap



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_="average_pool"			)->   Any:





													'''simple docstring'''






													__UpperCamelCase       =			len(featuremaps[0]			)
													__UpperCamelCase       =			int(size_map / size_pooling			)
													__UpperCamelCase       =			[]
													for i_map in range(len(SCREAMING_SNAKE_CASE_			)			):
																			__UpperCamelCase       =			featuremaps[i_map]
																			__UpperCamelCase       =			[]
																			for i_focus in range(0     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			):
																									for j_focus in range(0     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			):
																															__UpperCamelCase       =			feature_map[
																															    i_focus : i_focus + size_pooling,
																															    j_focus : j_focus + size_pooling,
																															]
																															if pooling_type == "average_pool":
																																					# average pooling
																																					map_pooled.append(np.average(SCREAMING_SNAKE_CASE_			)			)
																															elif pooling_type == "max_pooling":
																																					# max pooling
																																					map_pooled.append(np.max(SCREAMING_SNAKE_CASE_			)			)
																			__UpperCamelCase       =			np.asmatrix(SCREAMING_SNAKE_CASE_			).reshape(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)
																			featuremap_pooled.append(SCREAMING_SNAKE_CASE_			)
													return featuremap_pooled



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       =			[]
													for i in range(len(SCREAMING_SNAKE_CASE_			)			):
																			__UpperCamelCase       =			np.shape(data[i]			)
																			__UpperCamelCase       =			data[i].reshape(1     ,	shapes[0] * shapes[1]			)
																			__UpperCamelCase       =			data_listed.getA().tolist()[0]
																			data_expanded.extend(SCREAMING_SNAKE_CASE_			)
													__UpperCamelCase       =			np.asarray(SCREAMING_SNAKE_CASE_			)
													return data_expanded



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase       =			np.asarray(SCREAMING_SNAKE_CASE_			)
													__UpperCamelCase       =			np.shape(SCREAMING_SNAKE_CASE_			)
													__UpperCamelCase       =			data_mat.reshape(1     ,	shapes[0] * shapes[1]			)
													return data_expanded



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)->   str:





													'''simple docstring'''






													__UpperCamelCase       =			[]
													__UpperCamelCase       =			0
													for i_map in range(SCREAMING_SNAKE_CASE_			):
																			__UpperCamelCase       =			np.ones((size_map, size_map)			)
																			for i in range(0     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			):
																									for j in range(0     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			):
																															__UpperCamelCase       =			pd_pool[
																															    i_pool
																															]
																															__UpperCamelCase       =			i_pool + 1
																			__UpperCamelCase       =			np.multiply(
																			    SCREAMING_SNAKE_CASE_     ,	np.multiply(out_map[i_map]     ,	(1 - out_map[i_map])			)			)
																			pd_all.append(SCREAMING_SNAKE_CASE_			)
													return pd_all



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=bool			)->   Tuple:





													'''simple docstring'''






													print('''----------------------Start Training-------------------------'''			)
													print((''' - - Shape: Train_Data  ''', np.shape(SCREAMING_SNAKE_CASE_			))			)
													print((''' - - Shape: Teach_Data  ''', np.shape(SCREAMING_SNAKE_CASE_			))			)
													__UpperCamelCase       =			0
													__UpperCamelCase       =			[]
													__UpperCamelCase       =			10000
													while rp < n_repeat and mse >= error_accuracy:
																			__UpperCamelCase       =			0
																			print(F"-------------Learning Time {rp}--------------"			)
																			for p in range(len(SCREAMING_SNAKE_CASE_			)			):
																									# print('------------Learning Image: %d--------------'%p)
																									__UpperCamelCase       =			np.asmatrix(datas_train[p]			)
																									__UpperCamelCase       =			np.asarray(datas_teach[p]			)
																									__UpperCamelCase  ,							__UpperCamelCase       =			self.convolute(
																									    SCREAMING_SNAKE_CASE_     ,	self.conva     ,	self.w_conva     ,	self.thre_conva     ,	conv_step=self.step_conva     ,	)
																									__UpperCamelCase       =			self.pooling(SCREAMING_SNAKE_CASE_     ,	self.size_poolinga			)
																									__UpperCamelCase       =			np.shape(SCREAMING_SNAKE_CASE_			)
																									__UpperCamelCase       =			self._expand(SCREAMING_SNAKE_CASE_			)
																									__UpperCamelCase       =			data_bp_input

																									__UpperCamelCase       =			np.dot(SCREAMING_SNAKE_CASE_     ,	self.vji.T			) - self.thre_bpa
																									__UpperCamelCase       =			self.sig(SCREAMING_SNAKE_CASE_			)
																									__UpperCamelCase       =			np.dot(SCREAMING_SNAKE_CASE_     ,	self.wkj.T			) - self.thre_bpa
																									__UpperCamelCase       =			self.sig(SCREAMING_SNAKE_CASE_			)

																									# --------------Model Leaning ------------------------
																									# calculate error and gradient---------------
																									__UpperCamelCase       =			np.multiply(
																									    (data_teach - bp_outa)     ,	np.multiply(SCREAMING_SNAKE_CASE_     ,	(1 - bp_outa)			)			)
																									__UpperCamelCase       =			np.multiply(
																									    np.dot(SCREAMING_SNAKE_CASE_     ,	self.wkj			)     ,	np.multiply(SCREAMING_SNAKE_CASE_     ,	(1 - bp_outa)			)			)
																									__UpperCamelCase       =			np.dot(SCREAMING_SNAKE_CASE_     ,	self.vji			)

																									__UpperCamelCase       =			pd_i_all / (self.size_poolinga * self.size_poolinga)
																									__UpperCamelCase       =			pd_conva_pooled.T.getA().tolist()
																									__UpperCamelCase       =			self._calculate_gradient_from_pool(
																									    SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	shape_featuremapa[0]     ,	shape_featuremapa[1]     ,	self.size_poolinga     ,	)
																									# weight and threshold learning process---------
																									# convolution layer
																									for k_conv in range(self.conva[1]			):
																															__UpperCamelCase       =			self._expand_mat(pd_conva_all[k_conv]			)
																															__UpperCamelCase       =			self.rate_weight * np.dot(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)

																															__UpperCamelCase       =			self.w_conva[k_conv] + delta_w.reshape(
																															    (self.conva[0], self.conva[0])			)

																															__UpperCamelCase       =			(
																															    self.thre_conva[k_conv]
																															    - np.sum(pd_conva_all[k_conv]			) * self.rate_thre
																															)
																									# all connected layer
																									__UpperCamelCase       =			self.wkj + pd_k_all.T * bp_outa * self.rate_weight
																									__UpperCamelCase       =			self.vji + pd_j_all.T * bp_outa * self.rate_weight
																									__UpperCamelCase       =			self.thre_bpa - pd_k_all * self.rate_thre
																									__UpperCamelCase       =			self.thre_bpa - pd_j_all * self.rate_thre
																									# calculate the sum error of all single image
																									__UpperCamelCase       =			np.sum(abs(data_teach - bp_outa			)			)
																									error_count += errors
																									# print('   ----Teach      ',data_teach)
																									# print('   ----BP_output  ',bp_out3)
																			__UpperCamelCase       =			rp + 1
																			__UpperCamelCase       =			error_count / patterns
																			all_mse.append(SCREAMING_SNAKE_CASE_			)

													def draw_error():
																			__UpperCamelCase       =			[error_accuracy for i in range(int(n_repeat * 1.2			)			)]
																			plt.plot(SCREAMING_SNAKE_CASE_     ,	'''+-'''			)
																			plt.plot(SCREAMING_SNAKE_CASE_     ,	'''r--'''			)
																			plt.xlabel('''Learning Times'''			)
																			plt.ylabel('''All_mse'''			)
																			plt.grid(SCREAMING_SNAKE_CASE_     ,	alpha=0.5			)
																			plt.show()

													print('''------------------Training Complished---------------------'''			)
													print((''' - - Training epoch: ''', rp, F"     - - Mse: {mse:.6f}")			)
													if draw_e:
																			draw_error()
													return mse



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   List[str]:





													'''simple docstring'''






													__UpperCamelCase       =			[]
													print('''-------------------Start Testing-------------------------'''			)
													print((''' - - Shape: Test_Data  ''', np.shape(SCREAMING_SNAKE_CASE_			))			)
													for p in range(len(SCREAMING_SNAKE_CASE_			)			):
																			__UpperCamelCase       =			np.asmatrix(datas_test[p]			)
																			__UpperCamelCase  ,							__UpperCamelCase       =			self.convolute(
																			    SCREAMING_SNAKE_CASE_     ,	self.conva     ,	self.w_conva     ,	self.thre_conva     ,	conv_step=self.step_conva     ,	)
																			__UpperCamelCase       =			self.pooling(SCREAMING_SNAKE_CASE_     ,	self.size_poolinga			)
																			__UpperCamelCase       =			self._expand(SCREAMING_SNAKE_CASE_			)

																			__UpperCamelCase       =			data_bp_input
																			__UpperCamelCase       =			bp_outa * self.vji.T - self.thre_bpa
																			__UpperCamelCase       =			self.sig(SCREAMING_SNAKE_CASE_			)
																			__UpperCamelCase       =			bp_outa * self.wkj.T - self.thre_bpa
																			__UpperCamelCase       =			self.sig(SCREAMING_SNAKE_CASE_			)
																			produce_out.extend(bp_outa.getA().tolist()			)
													__UpperCamelCase       =			[list(map(self.do_round     ,	SCREAMING_SNAKE_CASE_			)			) for each in produce_out]
													return np.asarray(SCREAMING_SNAKE_CASE_			)



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   Tuple:





													'''simple docstring'''






													__UpperCamelCase       =			np.asmatrix(SCREAMING_SNAKE_CASE_			)
													__UpperCamelCase  ,							__UpperCamelCase       =			self.convolute(
													    SCREAMING_SNAKE_CASE_     ,	self.conva     ,	self.w_conva     ,	self.thre_conva     ,	conv_step=self.step_conva     ,	)
													__UpperCamelCase       =			self.pooling(SCREAMING_SNAKE_CASE_     ,	self.size_poolinga			)

													return data_conveda, data_pooleda


if __name__ == "__main__":
				pass






def 	A_ (							snake_case							:   float )      ->    float:



						'''simple docstring'''

						if edge <= 0 or not isinstance(snake_case ,      snake_case ):
												raise ValueError('''Length must be a positive.''' )
						return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)




def 	A_ (							snake_case							:   float )      ->    float:



						'''simple docstring'''

						if edge <= 0 or not isinstance(snake_case ,      snake_case ):
												raise ValueError('''Length must be a positive.''' )
						return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)


if __name__ == "__main__":
				import doctest

				doctest.testmod()




