


'''simple docstring'''

import pickle

import numpy as np
from matplotlib import pyplot as plt
class   __lowerCAmelCase						:





      '''simple docstring'''






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

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    List[str] ):

             '''simple docstring'''







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

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



      @classmethod
      def       UpperCamelCase__		(cls    :    Union[str, Any]   ,						UpperCamelCase    :    Union[str, Any] ):

             '''simple docstring'''







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

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



      def       UpperCamelCase__		(self    :    Dict   ,						UpperCamelCase    :    Union[str, Any] ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    str ):

             '''simple docstring'''







             return round(UpperCamelCase   ,						3 )



      def       UpperCamelCase__		(self    :    Tuple   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Tuple ):

             '''simple docstring'''







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

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



      def       UpperCamelCase__		(self    :    Any   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    List[Any]="average_pool" ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Union[str, Any]   ,						UpperCamelCase    :    List[str] ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    int   ,						UpperCamelCase    :    Dict ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Any   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    str ):

             '''simple docstring'''







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



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

             '''simple docstring'''







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

                           lowercase__      			= np.dot(UpperCamelCase   ,						self.vji.T ) - self.thre_bpa
                           lowercase__      			= self.sig(UpperCamelCase )
                           lowercase__      			= np.dot(UpperCamelCase   ,						self.wkj.T ) - self.thre_bpa
                           lowercase__      			= self.sig(UpperCamelCase )

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

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

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

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

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

             '''simple docstring'''







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

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






      def       UpperCamelCase__		(self    :    Tuple   ,						UpperCamelCase    :    Dict ):

             '''simple docstring'''







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

             return data_conveda, data_pooleda


if __name__ == "__main__":
 pass








'''simple docstring'''

from __future__ import annotations

def _SCREAMING_SNAKE_CASE      (A  , A       )       ->       list[list[int]]:






       """simple docstring"""







       lowercase__      			= []
       create_all_state(1  , A  , A  , []  , A       )
       return result

def _SCREAMING_SNAKE_CASE      (A  , A  , A  , A  , A  , )       ->       None:






       """simple docstring"""







       if level == 0:
              total_list.append(current_list[:]       )
              return

       for i in range(A  , total_number - level + 2       ):
              current_list.append(A       )
              create_all_state(i + 1  , A  , level - 1  , A  , A       )
              current_list.pop()

def _SCREAMING_SNAKE_CASE      (A       )       ->       None:






       """simple docstring"""







       for i in total_list:
              print(*A       )


if __name__ == "__main__":
 lowerCamelCase							:   Tuple  						=	4
 lowerCamelCase							:   Union[str, Any]  						=	2
 lowerCamelCase							:   Dict  						=	generate_all_combinations(n, k)
 print_all_state(total_list)




