


'''simple docstring'''
import pickle

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

		"""simple docstring"""

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

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					{
							    "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        lowerCAmelCase__							(						cls       ,  UpperCamelCase_				):

							'''simple docstring'''


							with open(UpperCamelCase_       ,  '''rb'''				) as f:
												UpperCamelCase__  :str				   =					pickle.load(UpperCamelCase_				)  # noqa: S301

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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							return round(UpperCamelCase_       ,  3				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


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

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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_="average_pool"				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					np.asarray(UpperCamelCase_				)
							UpperCamelCase__  :Optional[Any]				   =					np.shape(UpperCamelCase_				)
							UpperCamelCase__  :Tuple				   =					data_mat.reshape(1       ,  shapes[0] * shapes[1]				)
							return data_expanded




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					[]
							UpperCamelCase__  :Tuple				   =					0
							for i_map in range(UpperCamelCase_				):
												UpperCamelCase__  :Optional[int]				   =					np.ones((size_map, size_map)				)
												for i in range(0       ,  UpperCamelCase_       ,  UpperCamelCase_				):
																	for j in range(0       ,  UpperCamelCase_       ,  UpperCamelCase_				):
																						UpperCamelCase__  :List[Any]				   =					pd_pool[
																						    i_pool
																						]
																						UpperCamelCase__  :Optional[int]				   =					i_pool + 1
												UpperCamelCase__  :Optional[Any]				   =					np.multiply(
												    UpperCamelCase_       ,  np.multiply(out_map[i_map]       ,  (1 - out_map[i_map])				)				)
												pd_all.append(UpperCamelCase_				)
							return pd_all




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_=bool				):

							'''simple docstring'''


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

																	UpperCamelCase__  :List[Any]				   =					np.dot(UpperCamelCase_       ,  self.vji.T				) - self.thre_bpa
																	UpperCamelCase__  :str				   =					self.sig(UpperCamelCase_				)
																	UpperCamelCase__  :List[str]				   =					np.dot(UpperCamelCase_       ,  self.wkj.T				) - self.thre_bpa
																	UpperCamelCase__  :Tuple				   =					self.sig(UpperCamelCase_				)

																	# --------------Model Leaning ------------------------
																	# calculate error and gradient---------------
																	UpperCamelCase__  :Optional[Any]				   =					np.multiply(
																	    (data_teach - bp_outa)       ,  np.multiply(UpperCamelCase_       ,  (1 - bp_outa)				)				)
																	UpperCamelCase__  :Any				   =					np.multiply(
																	    np.dot(UpperCamelCase_       ,  self.wkj				)       ,  np.multiply(UpperCamelCase_       ,  (1 - bp_outa)				)				)
																	UpperCamelCase__  :str				   =					np.dot(UpperCamelCase_       ,  self.vji				)

																	UpperCamelCase__  :List[Any]				   =					pd_i_all / (self.size_poolinga * self.size_poolinga)
																	UpperCamelCase__  :List[Any]				   =					pd_conva_pooled.T.getA().tolist()
																	UpperCamelCase__  :int				   =					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]				):
																						UpperCamelCase__  :Tuple				   =					self._expand_mat(pd_conva_all[k_conv]				)
																						UpperCamelCase__  :List[Any]				   =					self.rate_weight * np.dot(UpperCamelCase_       ,  UpperCamelCase_				)

																						UpperCamelCase__  :Any				   =					self.w_conva[k_conv] + delta_w.reshape(
																						    (self.conva[0], self.conva[0])				)

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

							def draw_error():
												UpperCamelCase__  :Tuple				   =					[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        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


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

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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					np.asmatrix(UpperCamelCase_				)
							UpperCamelCase__  :List[str]				   =					self.convolute(
							    UpperCamelCase_       ,  self.conva       ,  self.w_conva       ,  self.thre_conva       ,  conv_step=self.step_conva       ,  )
							UpperCamelCase__  :List[Any]				   =					self.pooling(UpperCamelCase_       ,  self.size_poolinga				)

							return data_conveda, data_pooleda


if __name__ == "__main__":
		pass







'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase__			=      {
    'configuration_funnel': ['FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FunnelConfig'],
    'convert_funnel_original_tf_checkpoint_to_pytorch': [],
    'tokenization_funnel': ['FunnelTokenizer'],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['FunnelTokenizerFast']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'FunnelBaseModel',
     'FunnelForMaskedLM',
     'FunnelForMultipleChoice',
     'FunnelForPreTraining',
     'FunnelForQuestionAnswering',
     'FunnelForSequenceClassification',
     'FunnelForTokenClassification',
     'FunnelModel',
     'FunnelPreTrainedModel',
     'load_tf_weights_in_funnel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFFunnelBaseModel',
     'TFFunnelForMaskedLM',
     'TFFunnelForMultipleChoice',
     'TFFunnelForPreTraining',
     'TFFunnelForQuestionAnswering',
     'TFFunnelForSequenceClassification',
     'TFFunnelForTokenClassification',
     'TFFunnelModel',
     'TFFunnelPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
 from .tokenization_funnel import FunnelTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_funnel_fast import FunnelTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_funnel import (
      FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
      FunnelBaseModel,
      FunnelForMaskedLM,
      FunnelForMultipleChoice,
      FunnelForPreTraining,
      FunnelForQuestionAnswering,
      FunnelForSequenceClassification,
      FunnelForTokenClassification,
      FunnelModel,
      FunnelPreTrainedModel,
      load_tf_weights_in_funnel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_funnel import (
      TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFFunnelBaseModel,
      TFFunnelForMaskedLM,
      TFFunnelForMultipleChoice,
      TFFunnelForPreTraining,
      TFFunnelForQuestionAnswering,
      TFFunnelForSequenceClassification,
      TFFunnelForTokenClassification,
      TFFunnelModel,
      TFFunnelPreTrainedModel,
  )

else:
 import sys

 UpperCamelCase__			=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




