


from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFFunnelBaseModel,
	    TFFunnelForMaskedLM,
	    TFFunnelForMultipleChoice,
	    TFFunnelForPreTraining,
	    TFFunnelForQuestionAnswering,
	    TFFunnelForSequenceClassification,
	    TFFunnelForTokenClassification,
	    TFFunnelModel,
	)




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=13       ,  lowercase_=7       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=True       ,  lowercase_=99       ,  lowercase_=[1, 1, 2]       ,  lowercase_=1       ,  lowercase_=32       ,  lowercase_=4       ,  lowercase_=8       ,  lowercase_=37       ,  lowercase_="gelu_new"       ,  lowercase_=0.1       ,  lowercase_=0.1       ,  lowercase_=0.0       ,  lowercase_=512       ,  lowercase_=3       ,  lowercase_=0.02       ,  lowercase_=3       ,  lowercase_=4       ,  lowercase_=None       ,  lowercase_=False       ,  )  -> int:
											a__      =parent
											a__      =batch_size
											a__      =seq_length
											a__      =is_training
											a__      =use_input_mask
											a__      =use_token_type_ids
											a__      =use_labels
											a__      =vocab_size
											a__      =block_sizes
											a__      =num_decoder_layers
											a__      =d_model
											a__      =n_head
											a__      =d_head
											a__      =d_inner
											a__      =hidden_act
											a__      =hidden_dropout
											a__      =attention_dropout
											a__      =activation_dropout
											a__      =max_position_embeddings
											a__      =type_vocab_size
											a__      =2
											a__      =num_labels
											a__      =num_choices
											a__      =scope
											a__      =initializer_std

											# Used in the tests to check the size of the first attention layer
											a__      =n_head
											# Used in the tests to check the size of the first hidden state
											a__      =self.d_model
											# Used in the tests to check the number of output hidden states/attentions
											a__      =sum(self.block_sizes) + (0 if base else self.num_decoder_layers)
											# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
											# the last hidden state of the first block (which is the first hidden state of the decoder).
											if not base:
																		a__      =self.num_hidden_layers + 2





				def 						__UpperCamelCase    (				self)  -> Any:
											a__      =ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size)

											a__      =None
											if self.use_input_mask:
																		a__      =random_attention_mask([self.batch_size, self.seq_length])

											a__      =None
											if self.use_token_type_ids:
																		a__      =ids_tensor([self.batch_size, self.seq_length]       ,  self.type_vocab_size)

											a__      =None
											a__      =None
											a__      =None
											if self.use_labels:
																		a__      =ids_tensor([self.batch_size]       ,  self.type_sequence_label_size)
																		a__      =ids_tensor([self.batch_size, self.seq_length]       ,  self.num_labels)
																		a__      =ids_tensor([self.batch_size]       ,  self.num_choices)

											a__      =FunnelConfig(
											    vocab_size=self.vocab_size       ,  block_sizes=self.block_sizes       ,  num_decoder_layers=self.num_decoder_layers       ,  d_model=self.d_model       ,  n_head=self.n_head       ,  d_head=self.d_head       ,  d_inner=self.d_inner       ,  hidden_act=self.hidden_act       ,  hidden_dropout=self.hidden_dropout       ,  attention_dropout=self.attention_dropout       ,  activation_dropout=self.activation_dropout       ,  max_position_embeddings=self.max_position_embeddings       ,  type_vocab_size=self.type_vocab_size       ,  initializer_std=self.initializer_std       ,  )

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_mask,
											    sequence_labels,
											    token_labels,
											    choice_labels,
											)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> int:
											a__      =TFFunnelModel(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)

											a__      =[input_ids, input_mask]
											a__      =model(lowercase_)

											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.d_model))

											a__      =False
											a__      =TFFunnelModel(config=lowercase_)
											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.d_model))

											a__      =False
											a__      =TFFunnelModel(config=lowercase_)
											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.d_model))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> List[str]:
											a__      =TFFunnelBaseModel(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)

											a__      =[input_ids, input_mask]
											a__      =model(lowercase_)

											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, 2, self.d_model))

											a__      =False
											a__      =TFFunnelBaseModel(config=lowercase_)
											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, 3, self.d_model))

											a__      =False
											a__      =TFFunnelBaseModel(config=lowercase_)
											a__      =model(lowercase_)
											self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, 2, self.d_model))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> List[str]:
											a__      =TFFunnelForPreTraining(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> Optional[int]:
											a__      =TFFunnelForMaskedLM(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> Union[str, Any]:
											a__      =self.num_labels
											a__      =TFFunnelForSequenceClassification(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> List[str]:
											a__      =self.num_choices
											a__      =TFFunnelForMultipleChoice(config=lowercase_)
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      =tf.tile(tf.expand_dims(lowercase_       ,  1)       ,  (1, self.num_choices, 1))
											a__      ={
											    'input_ids': multiple_choice_inputs_ids,
											    'attention_mask': multiple_choice_input_mask,
											    'token_type_ids': multiple_choice_token_type_ids,
											}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> Tuple:
											a__      =self.num_labels
											a__      =TFFunnelForTokenClassification(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels))





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  )  -> List[Any]:
											a__      =TFFunnelForQuestionAnswering(config=lowercase_)
											a__      ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											a__      =model(lowercase_)
											self.parent.assertEqual(result.start_logits.shape       ,  (self.batch_size, self.seq_length))
											self.parent.assertEqual(result.end_logits.shape       ,  (self.batch_size, self.seq_length))





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =self.prepare_config_and_inputs()
											(
											    (
											    a__
											)    ,     (
											    a__
											)    ,     (
											    a__
											)    ,     (
											    a__
											)    ,     (
											    a__
											)    ,     (
											    a__
											)    ,     (
											    a__
											)    ,     
											)      =config_and_inputs
											a__      ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
											return config, inputs_dict







@require_tf
class       lowercase_					(lowercase__      ,					lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(
				    (
				        TFFunnelModel,
				        TFFunnelForMaskedLM,
				        TFFunnelForPreTraining,
				        TFFunnelForQuestionAnswering,
				        TFFunnelForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				snake_case	    =(
				    {
				        'feature-extraction': (TFFunnelBaseModel, TFFunnelModel),
				        'fill-mask': TFFunnelForMaskedLM,
				        'question-answering': TFFunnelForQuestionAnswering,
				        'text-classification': TFFunnelForSequenceClassification,
				        'token-classification': TFFunnelForTokenClassification,
				        'zero-shot': TFFunnelForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =TFFunnelModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Tuple:
											self.config_tester.run_common_tests()





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_pretraining(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> int:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*lowercase_)







@require_tf
class       lowercase_					(lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(
				    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
				)
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =TFFunnelModelTester(self       ,  base=lowercase_)
											a__      =ConfigTester(self       ,  config_class=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											self.config_tester.run_common_tests()





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_base_model(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> Any:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*lowercase_)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*lowercase_)








'''simple docstring'''

import pickle

import numpy as np
from matplotlib import pyplot as plt






class   UpperCAmelCase      :
 def __init__(							self :Dict		,			lowercase_ :Union[str, Any]		,			lowercase_ :Tuple		,			lowercase_ :List[Any]		,			lowercase_ :List[str]		,			lowercase_ :Dict		,			lowercase_ :Tuple=0.2		,			lowercase_ :List[str]=0.2				)->				Any:
     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 	UpperCAmelCase_				(							self :Optional[Any]		,			lowercase_ :Optional[Any]				)->				Optional[Any]:
     # save model dict with pickle
     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(lowercase_		,			"wb"				) as f:
         pickle.dump(lowercase_		,			lowercase_				)

     print(F"Model saved： {save_path}"				)
 @classmethod
 def 	UpperCAmelCase_				(							cls :Any		,			lowercase_ :List[Any]				)->				Optional[Any]:
     # read saved model
     with open(lowercase_		,			"rb"				) as f:
         A__							=     pickle.load(lowercase_				)  # 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(lowercase_		,			lowercase_		,			lowercase_		,			lowercase_		,			lowercase_		,			lowercase_		,			lowercase_				)
     # 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 	UpperCAmelCase_				(							self :Dict		,			lowercase_ :Optional[int]				)->				Optional[Any]:
     return 1 / (1 + np.exp(-1 * x				))
 def 	UpperCAmelCase_				(							self :List[Any]		,			lowercase_ :Optional[Any]				)->				Any:
     return round(lowercase_		,			3				)
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :Tuple		,			lowercase_ :List[Any]		,			lowercase_ :Tuple		,			lowercase_ :Union[str, Any]		,			lowercase_ :Dict				)->				str:
     # convolution process
     A__							=     convs[0]
     A__							=     convs[1]
     A__							=     np.shape(lowercase_				)[0]
     # get the data slice of original image data, data_focus
     A__							=     []
     for i_focus in range(0		,			size_data - size_conv + 1		,			lowercase_				):
         for j_focus in range(0		,			size_data - size_conv + 1		,			lowercase_				):
             A__							=     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
     A__							=     []
     A__							=     int((size_data - size_conv) / conv_step + 1				)
     for i_map in range(lowercase_				):
         A__							=     []
         for i_focus in range(len(lowercase_				)				):
             A__							=     (
                 np.sum(np.multiply(data_focus[i_focus]		,			w_convs[i_map]				)				)
                 - thre_convs[i_map]
             )
             featuremap.append(self.sig(lowercase_				)				)
         A__							=     np.asmatrix(lowercase_				).reshape(
             lowercase_		,			lowercase_				)
         data_featuremap.append(lowercase_				)

     # expanding the data slice to One dimenssion
     A__							=     []
     for each_focus in data_focus:
         focusa_list.extend(self.Expand_Mat(lowercase_				)				)
     A__							=     np.asarray(lowercase_				)
     return focus_list, data_featuremap
 def 	UpperCAmelCase_				(							self :Optional[int]		,			lowercase_ :Optional[int]		,			lowercase_ :Union[str, Any]		,			lowercase_ :Optional[Any]="average_pool"				)->				Dict:
     # pooling process
     A__							=     len(featuremaps[0]				)
     A__							=     int(size_map / size_pooling				)
     A__							=     []
     for i_map in range(len(lowercase_				)				):
         A__							=     featuremaps[i_map]
         A__							=     []
         for i_focus in range(0		,			lowercase_		,			lowercase_				):
             for j_focus in range(0		,			lowercase_		,			lowercase_				):
                 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(lowercase_				)				)
                 elif pooling_type == "max_pooling":
                     # max pooling
                     map_pooled.append(np.max(lowercase_				)				)
         A__							=     np.asmatrix(lowercase_				).reshape(lowercase_		,			lowercase_				)
         featuremap_pooled.append(lowercase_				)
     return featuremap_pooled
 def 	UpperCAmelCase_				(							self :str		,			lowercase_ :Any				)->				List[Any]:
     # expanding three dimension data to one dimension list
     A__							=     []
     for i in range(len(lowercase_				)				):
         A__							=     np.shape(data[i]				)
         A__							=     data[i].reshape(1		,			shapes[0] * shapes[1]				)
         A__							=     data_listed.getA().tolist()[0]
         data_expanded.extend(lowercase_				)
     A__							=     np.asarray(lowercase_				)
     return data_expanded
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :Optional[Any]				)->				Dict:
     # expanding matrix to one dimension list
     A__							=     np.asarray(lowercase_				)
     A__							=     np.shape(lowercase_				)
     A__							=     data_mat.reshape(1		,			shapes[0] * shapes[1]				)
     return data_expanded
 def 	UpperCAmelCase_				(							self :Union[str, Any]		,			lowercase_ :List[str]		,			lowercase_ :Any		,			lowercase_ :str		,			lowercase_ :Any		,			lowercase_ :Union[str, Any]				)->				Any:
     A__							=     []
     A__							=     0
     for i_map in range(lowercase_				):
         A__							=     np.ones((size_map, size_map)				)
         for i in range(0		,			lowercase_		,			lowercase_				):
             for j in range(0		,			lowercase_		,			lowercase_				):
                 A__							=     pd_pool[
                     i_pool
                 ]
                 A__							=     i_pool + 1
         A__							=     np.multiply(
             lowercase_		,			np.multiply(out_map[i_map]		,			(1 - out_map[i_map])				)				)
         pd_all.append(lowercase_				)
     return pd_all
 def 	UpperCAmelCase_				(							self :List[str]		,			lowercase_ :Optional[Any]		,			lowercase_ :Optional[Any]		,			lowercase_ :Union[str, Any]		,			lowercase_ :List[Any]		,			lowercase_ :Union[str, Any]		,			lowercase_ :str=bool				)->				Tuple:
     # model traning
     print("----------------------Start Training-------------------------"				)
     print((" - - Shape: Train_Data  ", np.shape(lowercase_				))				)
     print((" - - Shape: Teach_Data  ", np.shape(lowercase_				))				)
     A__							=     0
     A__							=     []
     A__							=     1_00_00
     while rp < n_repeat and mse >= error_accuracy:
         A__							=     0
         print(F"-------------Learning Time {rp}--------------"				)
         for p in range(len(lowercase_				)				):
             # print('------------Learning Image: %d--------------'%p)
             A__							=     np.asmatrix(datas_train[p]				)
             A__							=     np.asarray(datas_teach[p]				)
             A__,      A__							=     self.convolute(
                 lowercase_		,			self.conva		,			self.w_conva		,			self.thre_conva		,			conv_step=self.step_conva		,			)
             A__							=     self.pooling(lowercase_		,			self.size_poolinga				)
             A__							=     np.shape(lowercase_				)
             A__							=     self._expand(lowercase_				)
             A__							=     data_bp_input

             A__							=     np.dot(lowercase_		,			self.vji.T				) - self.thre_bpa
             A__							=     self.sig(lowercase_				)
             A__							=     np.dot(lowercase_		,			self.wkj.T				) - self.thre_bpa
             A__							=     self.sig(lowercase_				)

             # --------------Model Leaning ------------------------
             # calculate error and gradient---------------
             A__							=     np.multiply(
                 (data_teach - bp_outa)		,			np.multiply(lowercase_		,			(1 - bp_outa)				)				)
             A__							=     np.multiply(
                 np.dot(lowercase_		,			self.wkj				)		,			np.multiply(lowercase_		,			(1 - bp_outa)				)				)
             A__							=     np.dot(lowercase_		,			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(
                 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]				):
                 A__							=     self._expand_mat(pd_conva_all[k_conv]				)
                 A__							=     self.rate_weight * np.dot(lowercase_		,			lowercase_				)

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

     def draw_error():
         A__							=     [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 	UpperCAmelCase_				(							self :Dict		,			lowercase_ :List[Any]				)->				Optional[Any]:
     # model predict
     A__							=     []
     print("-------------------Start Testing-------------------------"				)
     print((" - - Shape: Test_Data  ", np.shape(lowercase_				))				)
     for p in range(len(lowercase_				)				):
         A__							=     np.asmatrix(datas_test[p]				)
         A__,      A__							=     self.convolute(
             lowercase_		,			self.conva		,			self.w_conva		,			self.thre_conva		,			conv_step=self.step_conva		,			)
         A__							=     self.pooling(lowercase_		,			self.size_poolinga				)
         A__							=     self._expand(lowercase_				)

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



 def 	UpperCAmelCase_				(							self :Dict		,			lowercase_ :Optional[int]				)->				List[str]:
     # return the data of image after convoluting process so we can check it out
     A__							=     np.asmatrix(lowercase_				)
     A__,      A__							=     self.convolute(
         lowercase_		,			self.conva		,			self.w_conva		,			self.thre_conva		,			conv_step=self.step_conva		,			)
     A__							=     self.pooling(lowercase_		,			self.size_poolinga				)

     return data_conveda, data_pooleda


if __name__ == "__main__":
  pass



