

'''simple docstring'''
import pickle

import numpy as np
from matplotlib import pyplot as plt







class 							A__   :





		def __init__(		self				:     Tuple							, _a				:     Dict							, _a				:     int							, _a				:     Union[str, Any]							, _a				:     List[Any]							, _a				:     Union[str, Any]							, _a				:     List[Any]=0.2							, _a				:     Tuple=0.2			)     ->   str:





							'''simple docstring'''






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





		def 						A  (		self				:     Optional[Any]							, _a				:     Optional[int]			)     ->   Optional[Any]:





							'''simple docstring'''






							_SCREAMING_SNAKE_CASE							   ={
							    '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(_a							, 'wb'			) as f:
												pickle.dump(_a							, _a			)

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





		@classmethod
		def 						A  (		cls				:     Optional[int]							, _a				:     Union[str, Any]			)     ->   str:





							'''simple docstring'''






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

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





		def 						A  (		self				:     Optional[int]							, _a				:     Dict			)     ->   Optional[int]:





							'''simple docstring'''






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





		def 						A  (		self				:     Tuple							, _a				:     str			)     ->   Dict:





							'''simple docstring'''






							return round(_a							, 3			)





		def 						A  (		self				:     Any							, _a				:     int							, _a				:     List[Any]							, _a				:     Union[str, Any]							, _a				:     Optional[Any]							, _a				:     List[Any]			)     ->   Tuple:





							'''simple docstring'''






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

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





		def 						A  (		self				:     int							, _a				:     Dict							, _a				:     Tuple							, _a				:     List[Any]="average_pool"			)     ->   List[str]:





							'''simple docstring'''






							_SCREAMING_SNAKE_CASE							   =len(featuremaps[0]			)
							_SCREAMING_SNAKE_CASE							   =int(size_map / size_pooling			)
							_SCREAMING_SNAKE_CASE							   =[]
							for i_map in range(len(_a			)			):
												_SCREAMING_SNAKE_CASE							   =featuremaps[i_map]
												_SCREAMING_SNAKE_CASE							   =[]
												for i_focus in range(0							, _a							, _a			):
																	for j_focus in range(0							, _a							, _a			):
																						_SCREAMING_SNAKE_CASE							   =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(_a			)			)
																						elif pooling_type == "max_pooling":
																											# max pooling
																											map_pooled.append(np.max(_a			)			)
												_SCREAMING_SNAKE_CASE							   =np.asmatrix(_a			).reshape(_a							, _a			)
												featuremap_pooled.append(_a			)
							return featuremap_pooled





		def 						A  (		self				:     Tuple							, _a				:     Dict			)     ->   Any:





							'''simple docstring'''






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





		def 						A  (		self				:     Optional[int]							, _a				:     Any			)     ->   Any:





							'''simple docstring'''






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





		def 						A  (		self				:     List[Any]							, _a				:     Any							, _a				:     Union[str, Any]							, _a				:     Optional[Any]							, _a				:     int							, _a				:     List[str]			)     ->   int:





							'''simple docstring'''






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





		def 						A  (		self				:     Optional[Any]							, _a				:     Any							, _a				:     Dict							, _a				:     List[Any]							, _a				:     Union[str, Any]							, _a				:     int							, _a				:     str=bool			)     ->   Optional[Any]:





							'''simple docstring'''






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

																	_SCREAMING_SNAKE_CASE							   =np.dot(_a							, self.vji.T			) - self.thre_bpa
																	_SCREAMING_SNAKE_CASE							   =self.sig(_a			)
																	_SCREAMING_SNAKE_CASE							   =np.dot(_a							, self.wkj.T			) - self.thre_bpa
																	_SCREAMING_SNAKE_CASE							   =self.sig(_a			)

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

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

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

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

							def draw_error():
												_SCREAMING_SNAKE_CASE							   =[error_accuracy for i in range(int(n_repeat * 1.2			)			)]
												plt.plot(_a							, '+-'			)
												plt.plot(_a							, 'r--'			)
												plt.xlabel('Learning Times'			)
												plt.ylabel('All_mse'			)
												plt.grid(_a							, 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				:     int							, _a				:     Optional[Any]			)     ->   Tuple:





							'''simple docstring'''






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

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





		def 						A  (		self				:     Optional[int]							, _a				:     Optional[Any]			)     ->   Union[str, Any]:





							'''simple docstring'''






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

							return data_conveda, data_pooleda


if __name__ == "__main__":
					pass



'''simple docstring'''
from __future__ import annotations

from fractions import Fraction
from math import gcd, sqrt


def 	_lowerCAmelCase (						_UpperCamelCase						:					int    )				->      bool:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =int(number**0.5    )
     return number == sq * sq


def 	_lowerCAmelCase (						_UpperCamelCase						:					int							,			_UpperCamelCase						:					int							,			_UpperCamelCase						:					int							,			_UpperCamelCase						:					int							,			_UpperCamelCase						:					int							,			_UpperCamelCase						:					int    )				->      tuple[int, int]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den
     _SCREAMING_SNAKE_CASE							   =x_den * y_den * z_den
     _SCREAMING_SNAKE_CASE							   =gcd(_UpperCamelCase							,			_UpperCamelCase    )
     top //= hcf
     bottom //= hcf
     return top, bottom


def 	_lowerCAmelCase (						_UpperCamelCase						:					int = 35    )				->      int:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =set()
     _SCREAMING_SNAKE_CASE							   =42
     _SCREAMING_SNAKE_CASE							   =Fraction(0    )
     _SCREAMING_SNAKE_CASE							   =42

     for x_num in range(1							,			order + 1    ):
          for x_den in range(x_num + 1							,			order + 1    ):
               for y_num in range(1							,			order + 1    ):
                    for y_den in range(y_num + 1							,			order + 1    ):
                         # n=1
                         _SCREAMING_SNAKE_CASE							   =x_num * y_den + x_den * y_num
                         _SCREAMING_SNAKE_CASE							   =x_den * y_den
                         _SCREAMING_SNAKE_CASE							   =gcd(_UpperCamelCase							,			_UpperCamelCase    )
                         z_num //= hcf
                         z_den //= hcf
                         if 0 < z_num < z_den <= order:
                              _SCREAMING_SNAKE_CASE							   =add_three(
                                  _UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase    )
                              unique_s.add(_UpperCamelCase    )

                         # n=2
                         _SCREAMING_SNAKE_CASE							   =(
                             x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num
                         )
                         _SCREAMING_SNAKE_CASE							   =x_den * x_den * y_den * y_den
                         if is_sq(_UpperCamelCase    ) and is_sq(_UpperCamelCase    ):
                              _SCREAMING_SNAKE_CASE							   =int(sqrt(_UpperCamelCase    )    )
                              _SCREAMING_SNAKE_CASE							   =int(sqrt(_UpperCamelCase    )    )
                              _SCREAMING_SNAKE_CASE							   =gcd(_UpperCamelCase							,			_UpperCamelCase    )
                              z_num //= hcf
                              z_den //= hcf
                              if 0 < z_num < z_den <= order:
                                   _SCREAMING_SNAKE_CASE							   =add_three(
                                       _UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase    )
                                   unique_s.add(_UpperCamelCase    )

                    # n=-1
                         _SCREAMING_SNAKE_CASE							   =x_num * y_num
                         _SCREAMING_SNAKE_CASE							   =x_den * y_num + x_num * y_den
                         _SCREAMING_SNAKE_CASE							   =gcd(_UpperCamelCase							,			_UpperCamelCase    )
                         z_num //= hcf
                         z_den //= hcf
                         if 0 < z_num < z_den <= order:
                              _SCREAMING_SNAKE_CASE							   =add_three(
                                  _UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase    )
                              unique_s.add(_UpperCamelCase    )

                         # n=2
                         _SCREAMING_SNAKE_CASE							   =x_num * x_num * y_num * y_num
                         _SCREAMING_SNAKE_CASE							   =(
                             x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den
                         )
                         if is_sq(_UpperCamelCase    ) and is_sq(_UpperCamelCase    ):
                              _SCREAMING_SNAKE_CASE							   =int(sqrt(_UpperCamelCase    )    )
                              _SCREAMING_SNAKE_CASE							   =int(sqrt(_UpperCamelCase    )    )
                              _SCREAMING_SNAKE_CASE							   =gcd(_UpperCamelCase							,			_UpperCamelCase    )
                              z_num //= hcf
                              z_den //= hcf
                              if 0 < z_num < z_den <= order:
                                   _SCREAMING_SNAKE_CASE							   =add_three(
                                       _UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase    )
                                   unique_s.add(_UpperCamelCase    )

     for num, den in unique_s:
          total += Fraction(_UpperCamelCase							,			_UpperCamelCase    )

     return total.denominator + total.numerator


if __name__ == "__main__":
     print(f'''{solution() = }''')
