




'''simple docstring'''







import pickle

import numpy as np
from matplotlib import pyplot as plt


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



           '''simple docstring'''

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



           '''simple docstring'''

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

           print(f"Model saved： {save_path}"				)
       @classmethod
       def     A      (	cls						:	Union[str, Any]   ,				lowercase						:	Union[str, Any]				):



           '''simple docstring'''

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

           UpperCAmelCase			       =		model_dic.get('''conv1'''				)
           conv_get.append(model_dic.get('''step_conv1'''				)				)
           UpperCAmelCase			       =		model_dic.get('''size_pooling1'''				)
           UpperCAmelCase			       =		model_dic.get('''num_bp1'''				)
           UpperCAmelCase			       =		model_dic.get('''num_bp2'''				)
           UpperCAmelCase			       =		model_dic.get('''num_bp3'''				)
           UpperCAmelCase			       =		model_dic.get('''rate_weight'''				)
           UpperCAmelCase			       =		model_dic.get('''rate_thre'''				)
           # create model instance
           UpperCAmelCase			       =		CNN(lowercase   ,				lowercase   ,				lowercase   ,				lowercase   ,				lowercase   ,				lowercase   ,				lowercase				)
           # modify model parameter
           UpperCAmelCase			       =		model_dic.get('''w_conv1'''				)
           UpperCAmelCase			       =		model_dic.get('''wkj'''				)
           UpperCAmelCase			       =		model_dic.get('''vji'''				)
           UpperCAmelCase			       =		model_dic.get('''thre_conv1'''				)
           UpperCAmelCase			       =		model_dic.get('''thre_bp2'''				)
           UpperCAmelCase			       =		model_dic.get('''thre_bp3'''				)
           return conv_ins
       def     A      (	self						:	Dict   ,				lowercase						:	Union[str, Any]				):



           '''simple docstring'''

           return 1 / (1 + np.exp(-1 * x				))
       def     A      (	self						:	List[str]   ,				lowercase						:	str				):



           '''simple docstring'''

           return round(lowercase   ,				3				)
       def     A      (	self						:	Tuple   ,				lowercase						:	Dict   ,				lowercase						:	Any   ,				lowercase						:	Tuple   ,				lowercase						:	Dict   ,				lowercase						:	Tuple				):



           '''simple docstring'''

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

           # expanding the data slice to One dimenssion
           UpperCAmelCase			       =		[]
           for each_focus in data_focus:
               focusa_list.extend(self.Expand_Mat(lowercase				)				)
           UpperCAmelCase			       =		np.asarray(lowercase				)
           return focus_list, data_featuremap
       def     A      (	self						:	Any   ,				lowercase						:	str   ,				lowercase						:	Optional[int]   ,				lowercase						:	List[Any]="average_pool"				):



           '''simple docstring'''

           UpperCAmelCase			       =		len(featuremaps[0]				)
           UpperCAmelCase			       =		int(size_map / size_pooling				)
           UpperCAmelCase			       =		[]
           for i_map in range(len(lowercase				)				):
               UpperCAmelCase			       =		featuremaps[i_map]
               UpperCAmelCase			       =		[]
               for i_focus in range(0   ,				lowercase   ,				lowercase				):
                   for j_focus in range(0   ,				lowercase   ,				lowercase				):
                       UpperCAmelCase			       =		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(lowercase				)				)
                       elif pooling_type == "max_pooling":
                           # max pooling
                           map_pooled.append(np.max(lowercase				)				)
               UpperCAmelCase			       =		np.asmatrix(lowercase				).reshape(lowercase   ,				lowercase				)
               featuremap_pooled.append(lowercase				)
           return featuremap_pooled
       def     A      (	self						:	Union[str, Any]   ,				lowercase						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		[]
           for i in range(len(lowercase				)				):
               UpperCAmelCase			       =		np.shape(data[i]				)
               UpperCAmelCase			       =		data[i].reshape(1   ,				shapes[0] * shapes[1]				)
               UpperCAmelCase			       =		data_listed.getA().tolist()[0]
               data_expanded.extend(lowercase				)
           UpperCAmelCase			       =		np.asarray(lowercase				)
           return data_expanded
       def     A      (	self						:	int   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		np.asarray(lowercase				)
           UpperCAmelCase			       =		np.shape(lowercase				)
           UpperCAmelCase			       =		data_mat.reshape(1   ,				shapes[0] * shapes[1]				)
           return data_expanded
       def     A      (	self						:	Any   ,				lowercase						:	int   ,				lowercase						:	List[Any]   ,				lowercase						:	int   ,				lowercase						:	Any   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		[]
           UpperCAmelCase			       =		0
           for i_map in range(lowercase				):
               UpperCAmelCase			       =		np.ones((size_map, size_map)				)
               for i in range(0   ,				lowercase   ,				lowercase				):
                   for j in range(0   ,				lowercase   ,				lowercase				):
                       UpperCAmelCase			       =		pd_pool[
                           i_pool
                       ]
                       UpperCAmelCase			       =		i_pool + 1
               UpperCAmelCase			       =		np.multiply(
                   lowercase   ,				np.multiply(out_map[i_map]   ,				(1 - out_map[i_map])				)				)
               pd_all.append(lowercase				)
           return pd_all
       def     A      (	self						:	Optional[Any]   ,				lowercase						:	str   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Optional[int]   ,				lowercase						:	Optional[int]   ,				lowercase						:	List[Any]   ,				lowercase						:	str=bool				):



           '''simple docstring'''

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

                   UpperCAmelCase			       =		np.dot(lowercase   ,				self.vji.T				) - self.thre_bpa
                   UpperCAmelCase			       =		self.sig(lowercase				)
                   UpperCAmelCase			       =		np.dot(lowercase   ,				self.wkj.T				) - self.thre_bpa
                   UpperCAmelCase			       =		self.sig(lowercase				)

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

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

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

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

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



           '''simple docstring'''

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

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







       def     A      (	self						:	Tuple   ,				lowercase						:	Dict				):



           '''simple docstring'''

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

           return data_conveda, data_pooleda


if __name__ == "__main__":
   pass








'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase							:   Dict  						=	logging.get_logger(__name__)

lowerCamelCase							:   Union[str, Any]  						=	{
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     List[str]  					=							"""megatron-bert"""



      def __init__(self    :    Tuple   ,						UpperCamelCase    :    Optional[int]=29056   ,						UpperCamelCase    :    Optional[Any]=1024   ,						UpperCamelCase    :    Any=24   ,						UpperCamelCase    :    int=16   ,						UpperCamelCase    :    Optional[int]=4096   ,						UpperCamelCase    :    int="gelu"   ,						UpperCamelCase    :    int=0.1   ,						UpperCamelCase    :    Tuple=0.1   ,						UpperCamelCase    :    Any=512   ,						UpperCamelCase    :    int=2   ,						UpperCamelCase    :    Dict=0.02   ,						UpperCamelCase    :    Dict=1E-12   ,						UpperCamelCase    :    List[Any]=0   ,						UpperCamelCase    :    Optional[int]="absolute"   ,						UpperCamelCase    :    List[Any]=True   ,						**UpperCamelCase    :    str   ,						):

             '''simple docstring'''







             super().__init__(pad_token_id=UpperCamelCase   ,						**UpperCamelCase )

             lowercase__      			= vocab_size
             lowercase__      			= hidden_size
             lowercase__      			= num_hidden_layers
             lowercase__      			= num_attention_heads
             lowercase__      			= hidden_act
             lowercase__      			= intermediate_size
             lowercase__      			= hidden_dropout_prob
             lowercase__      			= attention_probs_dropout_prob
             lowercase__      			= max_position_embeddings
             lowercase__      			= type_vocab_size
             lowercase__      			= initializer_range
             lowercase__      			= layer_norm_eps
             lowercase__      			= position_embedding_type
             lowercase__      			= use_cache




