





'''simple docstring'''



import pickle

import numpy as np
from matplotlib import pyplot as plt






class 							__lowercase    :







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase=0.2       ,_lowerCamelCase=0.2	)							->							str:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Dict:







				'''simple docstring'''





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

				print(f"Model saved： {save_path}"	)


	@classmethod
	def   _UpperCAmelCase       (cls       ,_lowerCamelCase	)							->							Dict:







				'''simple docstring'''





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

				__lowercase			  =							model_dic.get('''conv1'''	)
				conv_get.append(model_dic.get('''step_conv1'''	)	)
				__lowercase			  =							model_dic.get('''size_pooling1'''	)
				__lowercase			  =							model_dic.get('''num_bp1'''	)
				__lowercase			  =							model_dic.get('''num_bp2'''	)
				__lowercase			  =							model_dic.get('''num_bp3'''	)
				__lowercase			  =							model_dic.get('''rate_weight'''	)
				__lowercase			  =							model_dic.get('''rate_thre'''	)
				# create model instance
				__lowercase			  =							CNN(_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)
				# modify model parameter
				__lowercase			  =							model_dic.get('''w_conv1'''	)
				__lowercase			  =							model_dic.get('''wkj'''	)
				__lowercase			  =							model_dic.get('''vji'''	)
				__lowercase			  =							model_dic.get('''thre_conv1'''	)
				__lowercase			  =							model_dic.get('''thre_bp2'''	)
				__lowercase			  =							model_dic.get('''thre_bp3'''	)
				return conv_ins


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Optional[int]:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Union[str, Any]:







				'''simple docstring'''





				return round(_lowerCamelCase       ,3	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							int:







				'''simple docstring'''





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

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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase="average_pool"	)							->							Union[str, Any]:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Dict:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Union[str, Any]:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							str:







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase=bool	)							->							List[Any]:







				'''simple docstring'''





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

										__lowercase			  =							np.dot(_lowerCamelCase       ,self.vji.T	) - self.thre_bpa
										__lowercase			  =							self.sig(_lowerCamelCase	)
										__lowercase			  =							np.dot(_lowerCamelCase       ,self.wkj.T	) - self.thre_bpa
										__lowercase			  =							self.sig(_lowerCamelCase	)

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

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

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

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

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







				'''simple docstring'''





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

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


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							List[str]:







				'''simple docstring'''





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

				return data_conveda, data_pooleda


if __name__ == "__main__":
	pass












'''simple docstring'''



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


_SCREAMING_SNAKE_CASE   =      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE   =      {
    '''s-JoL/Open-Llama-V1''': '''https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json''',
}






class 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	a      :			Dict     =    "open-llama"


	def __init__(self       ,_lowerCamelCase=100000       ,_lowerCamelCase=4096       ,_lowerCamelCase=11008       ,_lowerCamelCase=32       ,_lowerCamelCase=32       ,_lowerCamelCase="silu"       ,_lowerCamelCase=2048       ,_lowerCamelCase=0.0_2       ,_lowerCamelCase=1E-6       ,_lowerCamelCase=True       ,_lowerCamelCase=0       ,_lowerCamelCase=1       ,_lowerCamelCase=2       ,_lowerCamelCase=False       ,_lowerCamelCase=True       ,_lowerCamelCase=0.1       ,_lowerCamelCase=0.1       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=None       ,**_lowerCamelCase       ,)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							vocab_size
				__lowercase			  =							max_position_embeddings
				__lowercase			  =							hidden_size
				__lowercase			  =							intermediate_size
				__lowercase			  =							num_hidden_layers
				__lowercase			  =							num_attention_heads
				__lowercase			  =							hidden_act
				__lowercase			  =							initializer_range
				__lowercase			  =							rms_norm_eps
				__lowercase			  =							use_cache
				__lowercase			  =							kwargs.pop(
				    '''use_memorry_efficient_attention'''       ,_lowerCamelCase	)
				__lowercase			  =							hidden_dropout_prob
				__lowercase			  =							attention_dropout_prob
				__lowercase			  =							use_stable_embedding
				__lowercase			  =							shared_input_output_embedding
				__lowercase			  =							rope_scaling
				self._rope_scaling_validation()

				super().__init__(
				    pad_token_id=_lowerCamelCase       ,bos_token_id=_lowerCamelCase       ,eos_token_id=_lowerCamelCase       ,tie_word_embeddings=_lowerCamelCase       ,**_lowerCamelCase       ,)


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				if self.rope_scaling is None:
							return

				if not isinstance(self.rope_scaling       ,_lowerCamelCase	) or len(self.rope_scaling	) != 2:
							raise ValueError(
							    '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
							    f"got {self.rope_scaling}"	)
				__lowercase			  =							self.rope_scaling.get('''type'''       ,_lowerCamelCase	)
				__lowercase			  =							self.rope_scaling.get('''factor'''       ,_lowerCamelCase	)
				if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
							raise ValueError(
							    f"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"	)
				if rope_scaling_factor is None or not isinstance(_lowerCamelCase       ,_lowerCamelCase	) or rope_scaling_factor <= 1.0:
							raise ValueError(f"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"	)





