





"""simple docstring"""




import pickle

import numpy as np
from matplotlib import pyplot as plt

class  _UpperCAmelCase  :



      '''simple docstring'''

      def __init__(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_=0.2					,      snake_case_=0.2  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  bp_numa
        A_  :       Union[str, Any]										=  bp_numa
        A_  :       Optional[int]										=  bp_numa
        A_  :       str										=  conva_get[:2]
        A_  :       str										=  conva_get[2]
        A_  :       Optional[int]										=  size_pa
        A_  :       int										=  rate_w
        A_  :       Union[str, Any]										=  rate_t
        A_  :       Optional[int]										=  [
            np.mat(-1 * np.random.rand(self.conva[0]					,      self.conva[0]  ) + 0.5  )
            for i in range(self.conva[1]  )
        ]
        A_  :       List[Any]										=  np.mat(-1 * np.random.rand(self.num_bpa					,      self.num_bpa  ) + 0.5  )
        A_  :       str										=  np.mat(-1 * np.random.rand(self.num_bpa					,      self.num_bpa  ) + 0.5  )
        A_  :       int										=  -2 * np.random.rand(self.conva[1]  ) + 1
        A_  :       Dict										=  -2 * np.random.rand(self.num_bpa  ) + 1
        A_  :       int										=  -2 * np.random.rand(self.num_bpa  ) + 1

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        A_  :       Optional[int]										=  {
            '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 					lowerCamelCase_			(     cls					,      snake_case_  ):

        """simple docstring"""

        with open(snake_case_					,      'rb'  ) as f:
          A_  :       Union[str, Any]										=  pickle.load(snake_case_  )  # noqa: S301

        A_  :       Union[str, Any]										=  model_dic.get('conv1'  )
        conv_get.append(model_dic.get('step_conv1'  )  )
        A_  :       int										=  model_dic.get('size_pooling1'  )
        A_  :       Dict										=  model_dic.get('num_bp1'  )
        A_  :       List[Any]										=  model_dic.get('num_bp2'  )
        A_  :       List[Any]										=  model_dic.get('num_bp3'  )
        A_  :       Optional[Any]										=  model_dic.get('rate_weight'  )
        A_  :       Any										=  model_dic.get('rate_thre'  )
        # create model instance
        A_  :       int										=  CNN(snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  )
        # modify model parameter
        A_  :       Optional[int]										=  model_dic.get('w_conv1'  )
        A_  :       List[Any]										=  model_dic.get('wkj'  )
        A_  :       str										=  model_dic.get('vji'  )
        A_  :       Optional[int]										=  model_dic.get('thre_conv1'  )
        A_  :       Optional[int]										=  model_dic.get('thre_bp2'  )
        A_  :       Tuple										=  model_dic.get('thre_bp3'  )
        return conv_ins

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

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

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        return round(snake_case_					,      3  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       List[Any]										=  convs[0]
        A_  :       Dict										=  convs[1]
        A_  :       Union[str, Any]										=  np.shape(snake_case_  )[0]
        # get the data slice of original image data, data_focus
        A_  :       Dict										=  []
        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_  :       Union[str, Any]										=  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_  :       Tuple										=  []
        A_  :       Tuple										=  int((size_data - size_conv) / conv_step + 1  )
        for i_map in range(snake_case_  ):
          A_  :       Tuple										=  []
          for i_focus in range(len(snake_case_  )  ):
            A_  :       str										=  (
                np.sum(np.multiply(data_focus[i_focus]					,      w_convs[i_map]  )  )
                - thre_convs[i_map]
            )
            featuremap.append(self.sig(snake_case_  )  )
          A_  :       Optional[int]										=  np.asmatrix(snake_case_  ).reshape(
              snake_case_					,      snake_case_  )
          data_featuremap.append(snake_case_  )

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

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_="average_pool"  ):

        """simple docstring"""

        A_  :       List[str]										=  len(featuremaps[0]  )
        A_  :       Tuple										=  int(size_map / size_pooling  )
        A_  :       Tuple										=  []
        for i_map in range(len(snake_case_  )  ):
          A_  :       Union[str, Any]										=  featuremaps[i_map]
          A_  :       str										=  []
          for i_focus in range(0					,      snake_case_					,      snake_case_  ):
            for j_focus in range(0					,      snake_case_					,      snake_case_  ):
              A_  :       Dict										=  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_  :       Optional[int]										=  np.asmatrix(snake_case_  ).reshape(snake_case_					,      snake_case_  )
          featuremap_pooled.append(snake_case_  )
        return featuremap_pooled

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        A_  :       Optional[Any]										=  []
        for i in range(len(snake_case_  )  ):
          A_  :       Tuple										=  np.shape(data[i]  )
          A_  :       Any										=  data[i].reshape(1					,      shapes[0] * shapes[1]  )
          A_  :       Optional[int]										=  data_listed.getA().tolist()[0]
          data_expanded.extend(snake_case_  )
        A_  :       Optional[Any]										=  np.asarray(snake_case_  )
        return data_expanded

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        A_  :       Optional[Any]										=  np.asarray(snake_case_  )
        A_  :       Optional[int]										=  np.shape(snake_case_  )
        A_  :       Dict										=  data_mat.reshape(1					,      shapes[0] * shapes[1]  )
        return data_expanded

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       Optional[Any]										=  []
        A_  :       Union[str, Any]										=  0
        for i_map in range(snake_case_  ):
          A_  :       Tuple										=  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_  :       Any										=  pd_pool[
                  i_pool
              ]
              A_  :       Any										=  i_pool + 1
          A_  :       int										=  np.multiply(
              snake_case_					,      np.multiply(out_map[i_map]					,      (1 - out_map[i_map])  )  )
          pd_all.append(snake_case_  )
        return pd_all

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_=bool  ):

        """simple docstring"""

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

            A_  :       str										=  np.dot(snake_case_					,      self.vji.T  ) - self.thre_bpa
            A_  :       str										=  self.sig(snake_case_  )
            A_  :       Dict										=  np.dot(snake_case_					,      self.wkj.T  ) - self.thre_bpa
            A_  :       List[str]										=  self.sig(snake_case_  )

            # --------------Model Leaning ------------------------
            # calculate error and gradient---------------
            A_  :       Optional[int]										=  np.multiply(
                (data_teach - bp_outa)					,      np.multiply(snake_case_					,      (1 - bp_outa)  )  )
            A_  :       Any										=  np.multiply(
                np.dot(snake_case_					,      self.wkj  )					,      np.multiply(snake_case_					,      (1 - bp_outa)  )  )
            A_  :       List[Any]										=  np.dot(snake_case_					,      self.vji  )

            A_  :       str										=  pd_i_all / (self.size_poolinga * self.size_poolinga)
            A_  :       Dict										=  pd_conva_pooled.T.getA().tolist()
            A_  :       str										=  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_  :       Optional[int]										=  self._expand_mat(pd_conva_all[k_conv]  )
              A_  :       Union[str, Any]										=  self.rate_weight * np.dot(snake_case_					,      snake_case_  )

              A_  :       List[str]										=  self.w_conva[k_conv] + delta_w.reshape(
                  (self.conva[0], self.conva[0])  )

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

        def draw_error():
          A_  :       List[Any]										=  [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 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        A_  :       List[str]										=  []
        print('-------------------Start Testing-------------------------'  )
        print((' - - Shape: Test_Data  ', np.shape(snake_case_  ))  )
        for p in range(len(snake_case_  )  ):
          A_  :       Optional[int]										=  np.asmatrix(datas_test[p]  )
          A_						,       A_  :       Any										=  self.convolute(
              snake_case_					,      self.conva					,      self.w_conva					,      self.thre_conva					,      conv_step=self.step_conva					,      )
          A_  :       Tuple										=  self.pooling(snake_case_					,      self.size_poolinga  )
          A_  :       Any										=  self._expand(snake_case_  )

          A_  :       Optional[int]										=  data_bp_input
          A_  :       List[str]										=  bp_outa * self.vji.T - self.thre_bpa
          A_  :       Optional[int]										=  self.sig(snake_case_  )
          A_  :       Dict										=  bp_outa * self.wkj.T - self.thre_bpa
          A_  :       Optional[Any]										=  self.sig(snake_case_  )
          produce_out.extend(bp_outa.getA().tolist()  )
        A_  :       Any										=  [list(map(self.do_round					,      snake_case_  )  ) for each in produce_out]
        return np.asarray(snake_case_  )

      def 					lowerCamelCase_			(     self					,      snake_case_  ):

        """simple docstring"""

        A_  :       int										=  np.asmatrix(snake_case_  )
        A_						,       A_  :       Union[str, Any]										=  self.convolute(
            snake_case_					,      self.conva					,      self.w_conva					,      self.thre_conva					,      conv_step=self.step_conva					,      )
        A_  :       int										=  self.pooling(snake_case_					,      self.size_poolinga  )

        return data_conveda, data_pooleda


if __name__ == "__main__":
   pass






"""simple docstring"""




import os

# Precomputes a list of the 100 first triangular numbers
lowerCamelCase_     :						List[str]						=					[int(0.5 * n * (n + 1)) for n in range(1, 1_01)]





def 						UpperCAmelCase__		(     ):






  """simple docstring"""





  A_  :       Union[str, Any]										=  os.path.dirname(os.path.realpath(_UpperCAmelCase					)					)
  A_  :       Tuple										=  os.path.join(_UpperCAmelCase ,					'words.txt'					)

  A_  :       List[Any]										=  ''
  with open(_UpperCAmelCase					) as f:
    A_  :       int										=  f.readline()

  A_  :       Optional[Any]										=  [word.strip('"'					) for word in words.strip('\r\n'					).split(','					)]
  A_  :       Dict										=  [
      word
      for word in [sum(ord(_UpperCAmelCase					) - 64 for x in word					) for word in words]
      if word in TRIANGULAR_NUMBERS
  ]
  return len(_UpperCAmelCase					)


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