






"""simple docstring"""







import pickle

import numpy as np
from matplotlib import pyplot as plt







class        _SCREAMING_SNAKE_CASE						:





							"""simple docstring"""





							def __init__(   self:     str				,		__A:     Union[str, Any]				,		__A:     Tuple				,		__A:     Dict				,		__A:     Optional[Any]				,		__A:     List[Any]				,		__A:     Optional[Any]=0.2				,		__A:     int=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  lowercase		(   self:     List[str]				,		__A:     str    ):

												'''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(__A				,		'''wb'''    ) as f:
																	pickle.dump(__A				,		__A    )

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




							@classmethod
							def  lowercase		(   cls:     Dict				,		__A:     Union[str, Any]    ):

												'''simple docstring'''

												with open(__A				,		'''rb'''    ) as f:
																	a__          =  pickle.load(__A    )  # 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(__A				,		__A				,		__A				,		__A				,		__A				,		__A				,		__A    )
												# 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  lowercase		(   self:     List[Any]				,		__A:     List[str]    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Dict				,		__A:     List[Any]    ):

												'''simple docstring'''

												return round(__A				,		3    )




							def  lowercase		(   self:     List[Any]				,		__A:     Optional[int]				,		__A:     List[str]				,		__A:     List[Any]				,		__A:     Union[str, Any]				,		__A:     List[str]    ):

												'''simple docstring'''

												a__          =  convs[0]
												a__          =  convs[1]
												a__          =  np.shape(__A    )[0]
												# get the data slice of original image data, data_focus
												a__          =  []
												for i_focus in range(0				,		size_data - size_conv + 1				,		__A    ):
																	for j_focus in range(0				,		size_data - size_conv + 1				,		__A    ):
																						a__          =  data[
																						    i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
																						]
																						data_focus.append(__A    )
        # 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(__A    ):
																	a__          =  []
																	for i_focus in range(len(__A    )    ):
																						a__          =  (
																						    np.sum(np.multiply(data_focus[i_focus]				,		w_convs[i_map]    )    )
																						    - thre_convs[i_map]
																						)
																						featuremap.append(self.sig(__A    )    )
																	a__          =  np.asmatrix(__A    ).reshape(
																	    __A				,		__A    )
																	data_featuremap.append(__A    )

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




							def  lowercase		(   self:     Any				,		__A:     Union[str, Any]				,		__A:     str				,		__A:     Tuple="average_pool"    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Tuple				,		__A:     Optional[Any]    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Optional[int]				,		__A:     Any    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Optional[int]				,		__A:     Any				,		__A:     Tuple				,		__A:     Tuple				,		__A:     Dict				,		__A:     List[str]    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     List[Any]				,		__A:     Union[str, Any]				,		__A:     List[str]				,		__A:     Optional[Any]				,		__A:     Any				,		__A:     Optional[Any]				,		__A:     Any=bool    ):

												'''simple docstring'''

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

																						a__          =  np.dot(__A				,		self.vji.T    ) - self.thre_bpa
																						a__          =  self.sig(__A    )
																						a__          =  np.dot(__A				,		self.wkj.T    ) - self.thre_bpa
																						a__          =  self.sig(__A    )

																						# --------------Model Leaning ------------------------
																						# calculate error and gradient---------------
																						a__          =  np.multiply(
																						    (data_teach - bp_outa)				,		np.multiply(__A				,		(1 - bp_outa)    )    )
																						a__          =  np.multiply(
																						    np.dot(__A				,		self.wkj    )				,		np.multiply(__A				,		(1 - bp_outa)    )    )
																						a__          =  np.dot(__A				,		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(
																						    __A				,		__A				,		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(__A				,		__A    )

																											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(__A    )

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

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




							def  lowercase		(   self:     Optional[int]				,		__A:     int    ):

												'''simple docstring'''

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

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





							def  lowercase		(   self:     List[str]				,		__A:     Union[str, Any]    ):

												'''simple docstring'''

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

												return data_conveda, data_pooleda


if __name__ == "__main__":
	pass










"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__a       :	str             = logging.get_logger(__name__)

__a       :	Dict             = {
    'xlm-mlm-en-2048': 'https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json',
    'xlm-mlm-ende-1024': 'https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json',
    'xlm-mlm-enfr-1024': 'https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json',
    'xlm-mlm-enro-1024': 'https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json',
    'xlm-mlm-tlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json',
    'xlm-mlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json',
    'xlm-clm-enfr-1024': 'https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json',
    'xlm-clm-ende-1024': 'https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json',
    'xlm-mlm-17-1280': 'https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json',
    'xlm-mlm-100-1280': 'https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json',
}







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='xlm'
							_SCREAMING_SNAKE_CASE    ={
							    'hidden_size': 'emb_dim',
							    'num_attention_heads': 'n_heads',
							    'num_hidden_layers': 'n_layers',
							    'n_words': 'vocab_size',  # For backward compatibility
							}




							def __init__(   self:     Optional[Any]				,		__A:     str=30145				,		__A:     Union[str, Any]=2048				,		__A:     Union[str, Any]=12				,		__A:     List[Any]=16				,		__A:     List[Any]=0.1				,		__A:     int=0.1				,		__A:     str=True				,		__A:     int=False				,		__A:     Optional[int]=False				,		__A:     Union[str, Any]=False				,		__A:     List[Any]=1				,		__A:     Dict=True				,		__A:     List[Any]=512				,		__A:     Optional[int]=2048**-0.5				,		__A:     Tuple=1e-12				,		__A:     Optional[int]=0.0_2				,		__A:     Optional[int]=0				,		__A:     Union[str, Any]=1				,		__A:     List[Any]=2				,		__A:     Union[str, Any]=3				,		__A:     Tuple=5				,		__A:     Tuple=True				,		__A:     Dict="first"				,		__A:     Optional[int]=True				,		__A:     int=None				,		__A:     Optional[Any]=True				,		__A:     Dict=0.1				,		__A:     Tuple=5				,		__A:     Union[str, Any]=5				,		__A:     Tuple=0				,		__A:     List[str]=0				,		__A:     int=2				,		__A:     Optional[int]=0				,		**__A:     Union[str, Any]				,		):

												'''simple docstring'''

												a__          =  vocab_size
												a__          =  emb_dim
												a__          =  n_layers
												a__          =  n_heads
												a__          =  dropout
												a__          =  attention_dropout
												a__          =  gelu_activation
												a__          =  sinusoidal_embeddings
												a__          =  causal
												a__          =  asm
												a__          =  n_langs
												a__          =  use_lang_emb
												a__          =  layer_norm_eps
												a__          =  bos_index
												a__          =  eos_index
												a__          =  pad_index
												a__          =  unk_index
												a__          =  mask_index
												a__          =  is_encoder
												a__          =  max_position_embeddings
												a__          =  embed_init_std
												a__          =  init_std
												a__          =  summary_type
												a__          =  summary_use_proj
												a__          =  summary_activation
												a__          =  summary_proj_to_labels
												a__          =  summary_first_dropout
												a__          =  start_n_top
												a__          =  end_n_top
												a__          =  mask_token_id
												a__          =  lang_id

												if "n_words" in kwargs:
																	a__          =  kwargs['''n_words''']

												super().__init__(pad_token_id=__A				,		bos_token_id=__A				,		**__A    )







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							@property
							def  lowercase		(   self:     int    ):

												'''simple docstring'''

												if self.task == "multiple-choice":
																	a__          =  {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
												else:
																	a__          =  {0: '''batch''', 1: '''sequence'''}
												return OrderedDict(
												    [
												        ('''input_ids''', dynamic_axis),
												        ('''attention_mask''', dynamic_axis),
												        ('''token_type_ids''', dynamic_axis),
												    ]    )


