






'''simple docstring'''







import pickle

import numpy as np
from matplotlib import pyplot as plt




class 		UpperCamelCase__    :




		'''simple docstring'''





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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								# save model dict with pickle
								lowerCamelCase__         =  {
								    """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(_lowerCAmelCase   ,"""wb"""       ) as f:
														pickle.dump(_lowerCAmelCase   ,_lowerCAmelCase       )

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






		@classmethod
		def 			UpperCamelCase_					(      cls   ,_lowerCAmelCase       ):
								# read saved model
								with open(_lowerCAmelCase   ,"""rb"""       ) as f:
														lowerCamelCase__         =  pickle.load(_lowerCAmelCase       )  # noqa: S301

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






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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								return round(_lowerCAmelCase   ,3       )






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

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






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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								# expanding three dimension data to one dimension list
								lowerCamelCase__         =  []
								for i in range(len(_lowerCAmelCase       )       ):
														lowerCamelCase__         =  np.shape(data[i]       )
														lowerCamelCase__         =  data[i].reshape(1   ,shapes[0] * shapes[1]       )
														lowerCamelCase__         =  data_listed.getA().tolist()[0]
														data_expanded.extend(_lowerCAmelCase       )
								lowerCamelCase__         =  np.asarray(_lowerCAmelCase       )
								return data_expanded






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






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






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

																				lowerCamelCase__         =  np.dot(_lowerCAmelCase   ,self.vji.T       ) - self.thre_bpa
																				lowerCamelCase__         =  self.sig(_lowerCAmelCase       )
																				lowerCamelCase__         =  np.dot(_lowerCAmelCase   ,self.wkj.T       ) - self.thre_bpa
																				lowerCamelCase__         =  self.sig(_lowerCAmelCase       )

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

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

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

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

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

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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								# model predict
								lowerCamelCase__         =  []
								print("""-------------------Start Testing-------------------------"""       )
								print((""" - - Shape: Test_Data  """, np.shape(_lowerCAmelCase       ))       )
								for p in range(len(_lowerCAmelCase       )       ):
														lowerCamelCase__         =  np.asmatrix(datas_test[p]       )
														lowerCamelCase__       ,			lowerCamelCase__         =  self.convolute(
														    _lowerCAmelCase   ,self.conva   ,self.w_conva   ,self.thre_conva   ,conv_step=self.step_conva   ,)
														lowerCamelCase__         =  self.pooling(_lowerCAmelCase   ,self.size_poolinga       )
														lowerCamelCase__         =  self._expand(_lowerCAmelCase       )

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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								# return the data of image after convoluting process so we can check it out
								lowerCamelCase__         =  np.asmatrix(_lowerCAmelCase       )
								lowerCamelCase__       ,			lowerCamelCase__         =  self.convolute(
								    _lowerCAmelCase   ,self.conva   ,self.w_conva   ,self.thre_conva   ,conv_step=self.step_conva   ,)
								lowerCamelCase__         =  self.pooling(_lowerCAmelCase   ,self.size_poolinga       )

								return data_conveda, data_pooleda


if __name__ == "__main__":
							pass









'''simple docstring'''







from __future__ import annotations

import math
def 						A__			(				__lowerCAmelCase		:			int						):

						if 1 < number < 4:
												# 2 and 3 are primes
												return True
						elif number < 2 or number % 2 == 0 or number % 3 == 0:
												# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
												return False

						# All primes number are in format of 6k +/- 1
						for i in range(5   ,    int(math.sqrt(__lowerCAmelCase						) + 1						)   ,    6						):
												if number % i == 0 or number % (i + 2) == 0:
																		return False
						return True


UpperCamelCase    :      str  			=		[num for num in range(3, 10_00_01, 2) if not is_prime(num)]
def 						A__			(				__lowerCAmelCase		:			int						):
						if not isinstance(__lowerCAmelCase   ,    __lowerCAmelCase						):
												raise ValueError("""n must be an integer"""						)
						if n <= 0:
												raise ValueError("""n must be >= 0"""						)

						lowerCamelCase__         =  []
						for num in range(len(__lowerCAmelCase						)						):
												lowerCamelCase__         =  0
												while 2 * i * i <= odd_composites[num]:
																		lowerCamelCase__         =  odd_composites[num] - 2 * i * i
																		if is_prime(__lowerCAmelCase						):
																								break
																		i += 1
												else:
																		list_nums.append(odd_composites[num]						)
																		if len(__lowerCAmelCase						) == n:
																								return list_nums

						return []
def 						A__			(				):
						return compute_nums(1						)[0]


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

