



import pickle

import numpy as np
from matplotlib import pyplot as plt


class   __lowerCAmelCase  :
					"""simple docstring"""






					def __init__(  self       :							int					,       _snake_case       :							Optional[Any]					,       _snake_case       :							Tuple					,       _snake_case       :							int					,       _snake_case       :							List[Any]					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							List[str]=0.2					,       _snake_case       :							Any=0.2       ):






								"""simple docstring"""


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



					def      _a				(  self       :							Optional[int]					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


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

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



					@classmethod
					def      _a				(  cls       :							Union[str, Any]					,       _snake_case       :							int       ):






								"""simple docstring"""


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

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



					def      _a				(  self       :							int					,       _snake_case       :							List[Any]       ):






								"""simple docstring"""


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



					def      _a				(  self       :							int					,       _snake_case       :							str       ):






								"""simple docstring"""


								return round(_snake_case					,       3       )



					def      _a				(  self       :							Any					,       _snake_case       :							str					,       _snake_case       :							List[str]					,       _snake_case       :							Optional[int]					,       _snake_case       :							Any					,       _snake_case       :							List[Any]       ):






								"""simple docstring"""


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

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



					def      _a				(  self       :							Optional[int]					,       _snake_case       :							List[str]					,       _snake_case       :							Tuple					,       _snake_case       :							int="average_pool"       ):






								"""simple docstring"""


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



					def      _a				(  self       :							str					,       _snake_case       :							List[Any]       ):






								"""simple docstring"""


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



					def      _a				(  self       :							Optional[Any]					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


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



					def      _a				(  self       :							List[str]					,       _snake_case       :							List[str]					,       _snake_case       :							List[str]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[Any]					,       _snake_case       :							Optional[int]       ):






								"""simple docstring"""


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



					def      _a				(  self       :							List[str]					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							List[Any]					,       _snake_case       :							str					,       _snake_case       :							Optional[int]					,       _snake_case       :							List[str]					,       _snake_case       :							List[Any]=bool       ):






								"""simple docstring"""


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

														A__	  =     np.dot(_snake_case					,       self.vji.T       ) - self.thre_bpa
														A__	  =     self.sig(_snake_case       )
														A__	  =     np.dot(_snake_case					,       self.wkj.T       ) - self.thre_bpa
														A__	  =     self.sig(_snake_case       )

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

														A__	  =     pd_i_all / (self.size_poolinga * self.size_poolinga)
														A__	  =     pd_conva_pooled.T.getA().tolist()
														A__	  =     self._calculate_gradient_from_pool(
														    _snake_case					,       _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]       ):
																	A__	  =     self._expand_mat(pd_conva_all[k_conv]       )
																	A__	  =     self.rate_weight * np.dot(_snake_case					,       _snake_case       )

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

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

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






								"""simple docstring"""


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

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



					def      _a				(  self       :							Any					,       _snake_case       :							Any       ):






								"""simple docstring"""


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

								return data_conveda, data_pooleda


if __name__ == "__main__":
							pass









from __future__ import annotations

from fractions import Fraction


def A    (							__UpperCamelCase			,						__UpperCamelCase       )    ->   bool:
			return (
			    num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den
			)


def A    (							__UpperCamelCase       )    ->   list[str]:
			A__	  =     []
			A__	  =     11
			A__	  =     int('1' + '0' * digit_len       )
			for num in range(__UpperCamelCase			,						__UpperCamelCase       ):
						while den <= 99:
									if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
												if is_digit_cancelling(__UpperCamelCase			,						__UpperCamelCase       ):
															solutions.append(f'''{num}/{den}'''       )
									den += 1
						num += 1
						A__	  =     10
			return solutions


def A    (							__UpperCamelCase = 2       )    ->   int:
			A__	  =     1.0
			for fraction in fraction_list(__UpperCamelCase       ):
						A__	  =     Fraction(__UpperCamelCase       )
						result *= frac.denominator / frac.numerator
			return int(__UpperCamelCase       )


if __name__ == "__main__":
							print(solution())




