


"""simple docstring"""







from collections.abc import Sequence
def _A						(						lowercase       ,    lowercase = False		):







    """simple docstring"""
    if not arr:
        return 0

    a 		=0 if allow_empty_subarrays else float('''-inf'''		)
    a 		=0.0
    for num in arr:
        a 		=max(0 if allow_empty_subarrays else num       ,    curr_sum + num		)
        a 		=max(lowercase       ,    lowercase		)

    return max_sum


if __name__ == "__main__":
       from doctest import testmod

       testmod()

       lowerCamelCase_   :   List[str]									=  [-2, 1, -3, 4, -1, 2, 1, -5, 4]
       print(F'{max_subarray_sum(nums) = }')


"""simple docstring"""


import pickle

import numpy as np
from matplotlib import pyplot as plt





class     snake_case	:

				def __init__(	self							:				int     ,  UpperCamelCase__							:				Optional[Any]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				Dict     ,  UpperCamelCase__							:				Union[str, Any]=0.2     ,  UpperCamelCase__							:				Any=0.2)->   Optional[int]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Optional[int]     ,  UpperCamelCase__							:				int)->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       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 							lowercase_     (	cls							:				Dict     ,  UpperCamelCase__							:				Union[str, Any])->   List[Any]:



						'''simple docstring'''


						with open(UpperCamelCase__     ,  "rb") as f:
								__lowerCAmelCase:       Dict          =			pickle.load(UpperCamelCase__)  # noqa: S301

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

				def 							lowercase_     (	self							:				Dict     ,  UpperCamelCase__							:				List[Any])->   List[Any]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Dict     ,  UpperCamelCase__							:				List[Any])->   Optional[Any]:



						'''simple docstring'''


						return round(UpperCamelCase__     ,  3)

				def 							lowercase_     (	self							:				Optional[int]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Any     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				Tuple     ,  UpperCamelCase__							:				int)->   Dict:



						'''simple docstring'''


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

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

				def 							lowercase_     (	self							:				Any     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				List[Any]     ,  UpperCamelCase__							:				Optional[Any]="average_pool")->   str:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Union[str, Any]     ,  UpperCamelCase__							:				str)->   int:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Union[str, Any]     ,  UpperCamelCase__							:				Union[str, Any])->   Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			np.asarray(UpperCamelCase__)
						__lowerCAmelCase:       Optional[int]          =			np.shape(UpperCamelCase__)
						__lowerCAmelCase:       Optional[int]          =			data_mat.reshape(1     ,  shapes[0] * shapes[1])
						return data_expanded

				def 							lowercase_     (	self							:				Dict     ,  UpperCamelCase__							:				Union[str, Any]     ,  UpperCamelCase__							:				Dict     ,  UpperCamelCase__							:				Optional[Any]     ,  UpperCamelCase__							:				List[Any]     ,  UpperCamelCase__							:				Dict)->   List[Any]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				str     ,  UpperCamelCase__							:				Union[str, Any]     ,  UpperCamelCase__							:				int     ,  UpperCamelCase__							:				Dict     ,  UpperCamelCase__							:				List[Any]     ,  UpperCamelCase__							:				Any     ,  UpperCamelCase__							:				str=bool)->   List[str]:



						'''simple docstring'''


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

										__lowerCAmelCase:       int          =			np.dot(UpperCamelCase__     ,  self.vji.T) - self.thre_bpa
										__lowerCAmelCase:       int          =			self.sig(UpperCamelCase__)
										__lowerCAmelCase:       Optional[Any]          =			np.dot(UpperCamelCase__     ,  self.wkj.T) - self.thre_bpa
										__lowerCAmelCase:       str          =			self.sig(UpperCamelCase__)

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

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

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

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

						def draw_error():
								__lowerCAmelCase:       Dict          =			[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 							lowercase_     (	self							:				Union[str, Any]     ,  UpperCamelCase__							:				Tuple)->   List[str]:



						'''simple docstring'''


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

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




				def 							lowercase_     (	self							:				int     ,  UpperCamelCase__							:				Any)->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			np.asmatrix(UpperCamelCase__)
						__lowerCAmelCase      , __lowerCAmelCase:       Optional[Any]          =			self.convolute(
						    UpperCamelCase__     ,  self.conva     ,  self.w_conva     ,  self.thre_conva     ,  conv_step=self.step_conva     ,  )
						__lowerCAmelCase:       Any          =			self.pooling(UpperCamelCase__     ,  self.size_poolinga)

						return data_conveda, data_pooleda


if __name__ == "__main__":
			pass


