




"""simple docstring"""

import numpy as np
from matplotlib import pyplot as plt

def 		lowercase_   (		_UpperCAmelCase						):




	"""simple docstring"""






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

class 							lowercase   :







				def __init__(				self :       List[str]   ,  _lowerCamelCase :       Union[str, Any]   ,  _lowerCamelCase :       Dict=None   ,  _lowerCamelCase :       Optional[int]=None   ,  _lowerCamelCase :       List[str]=False							):



					"""simple docstring"""

					A_	:					Tuple						 =		units
					A_	:					Union[str, Any]						 =		None
					A_	:					Union[str, Any]						 =		None
					A_	:					Tuple						 =		activation
					if learning_rate is None:
						A_	:					Optional[Any]						 =		0.3
					A_	:					Tuple						 =		learning_rate
					A_	:					str						 =		is_input_layer







				def        a_	(				self :       Tuple   ,  _lowerCamelCase :       str							):



					"""simple docstring"""

					A_	:					Optional[Any]						 =		np.asmatrix(np.random.normal(0   ,  0.5   ,  (self.units, back_units)							)							)
					A_	:					int						 =		np.asmatrix(np.random.normal(0   ,  0.5   ,  self.units							)							).T
					if self.activation is None:
						A_	:					List[Any]						 =		sigmoid







				def        a_	(				self :       Optional[int]							):



					"""simple docstring"""

					if self.activation == sigmoid:
						A_	:					Tuple						 =		np.dot(self.output   ,  (1 - self.output).T							)
						A_	:					Tuple						 =		np.diag(np.diag(_lowerCamelCase							)							)
					else:
						A_	:					Any						 =		1
					return gradient_activation







				def        a_	(				self :       List[str]   ,  _lowerCamelCase :       List[str]							):



					"""simple docstring"""

					A_	:					Union[str, Any]						 =		xdata
					if self.is_input_layer:
						# input layer
						A_	:					Optional[int]						 =		xdata
						A_	:					List[Any]						 =		xdata
						return xdata
					else:
						A_	:					Tuple						 =		np.dot(self.weight   ,  self.xdata							) - self.bias
						A_	:					Dict						 =		self.activation(self.wx_plus_b							)
						return self.output







				def        a_	(				self :       Any   ,  _lowerCamelCase :       Union[str, Any]							):



					"""simple docstring"""

					A_	:					Dict						 =		self.cal_gradient()  # i * i 维
					A_	:					int						 =		np.asmatrix(np.dot(gradient.T   ,  _lowerCamelCase							)							)

					A_	:					Dict						 =		np.asmatrix(self.xdata							)
					A_	:					Dict						 =		-1
					A_	:					Any						 =		self.weight

					A_	:					Optional[Any]						 =		np.dot(gradient.T   ,  self._gradient_weight.T							)
					A_	:					Any						 =		gradient * self._gradient_bias
					A_	:					str						 =		np.dot(_lowerCamelCase   ,  self._gradient_x							).T
					# upgrade: the Negative gradient direction
					A_	:					Optional[Any]						 =		self.weight - self.learn_rate * self.gradient_weight
					A_	:					Any						 =		self.bias - self.learn_rate * self.gradient_bias.T
					# updates the weights and bias according to learning rate (0.3 if undefined)
					return self.gradient

class 							lowercase   :







				def __init__(				self :       Any							):



					"""simple docstring"""

					A_	:					Tuple						 =		[]
					A_	:					List[str]						 =		[]
					A_	:					Tuple						 =		plt.figure()
					A_	:					Optional[Any]						 =		self.fig_loss.add_subplot(1   ,  1   ,  1							)







				def        a_	(				self :       List[Any]   ,  _lowerCamelCase :       Optional[Any]							):



					"""simple docstring"""

					self.layers.append(_lowerCamelCase							)







				def        a_	(				self :       Optional[Any]							):



					"""simple docstring"""

					for i, layer in enumerate(self.layers[:]							):
						if i < 1:
							A_	:					Optional[Any]						 =		True
						else:
							layer.initializer(self.layers[i - 1].units							)







				def        a_	(				self :       Tuple							):



					"""simple docstring"""

					for i, layer in enumerate(self.layers[:]							):
						print(F"""------- layer {i} -------"""							)
						print('''weight.shape '''   ,  np.shape(layer.weight							)							)
						print('''bias.shape '''   ,  np.shape(layer.bias							)							)







				def        a_	(				self :       Optional[Any]   ,  _lowerCamelCase :       Dict   ,  _lowerCamelCase :       Tuple   ,  _lowerCamelCase :       Dict   ,  _lowerCamelCase :       Any							):



					"""simple docstring"""

					A_	:					Optional[int]						 =		train_round
					A_	:					Any						 =		accuracy

					self.ax_loss.hlines(self.accuracy   ,  0   ,  self.train_round * 1.1							)

					A_	:					str						 =		np.shape(_lowerCamelCase							)
					for _ in range(_lowerCamelCase							):
						A_	:					List[Any]						 =		0
						for row in range(x_shape[0]							):
							A_	:					Any						 =		np.asmatrix(xdata[row, :]							).T
							A_	:					Union[str, Any]						 =		np.asmatrix(ydata[row, :]							).T

							# forward propagation
							for layer in self.layers:
								A_	:					Optional[int]						 =		layer.forward_propagation(_xdata							)

							A_ ,			A_	:					Optional[Any]						 =		self.cal_loss(_ydata   ,  _xdata							)
							A_	:					List[Any]						 =		all_loss + loss

							# back propagation: the input_layer does not upgrade
							for layer in self.layers[:0:-1]:
								A_	:					Dict						 =		layer.back_propagation(_lowerCamelCase							)

						A_	:					Union[str, Any]						 =		all_loss / x_shape[0]
						self.train_mse.append(_lowerCamelCase							)

						self.plot_loss()

						if mse < self.accuracy:
							print('''----达到精度----'''							)
							return mse
					return None







				def        a_	(				self :       Optional[Any]   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       List[str]							):



					"""simple docstring"""

					A_	:					Optional[int]						 =		np.sum(np.power((ydata - ydata_)   ,  2							)							)
					A_	:					Optional[int]						 =		2 * (ydata_ - ydata)
					# vector (shape is the same as _ydata.shape)
					return self.loss, self.loss_gradient







				def        a_	(				self :       str							):



					"""simple docstring"""

					if self.ax_loss.lines:
						self.ax_loss.lines.remove(self.ax_loss.lines[0]							)
					self.ax_loss.plot(self.train_mse   ,  '''r-'''							)
					plt.ion()
					plt.xlabel('''step'''							)
					plt.ylabel('''loss'''							)
					plt.show()
					plt.pause(0.1							)

def 		lowercase_   (		):




	"""simple docstring"""






	A_	:					List[str]						 =		np.random.randn(10	,   10						)
	A_	:					List[Any]						 =		np.asarray(
	    [
	        [0.8, 0.4],
	        [0.4, 0.3],
	        [0.34, 0.45],
	        [0.67, 0.32],
	        [0.88, 0.67],
	        [0.78, 0.77],
	        [0.55, 0.66],
	        [0.55, 0.43],
	        [0.54, 0.1],
	        [0.1, 0.5],
	    ]						)
	A_	:					List[Any]						 =		BPNN()
	for i in (10, 20, 30, 2):
		model.add_layer(DenseLayer(_UpperCAmelCase						)						)
	model.build()
	model.summary()
	model.train(xdata=_UpperCAmelCase	,   ydata=_UpperCAmelCase	,   train_round=100	,   accuracy=0.01						)


if __name__ == "__main__":
		example()











"""simple docstring"""

from __future__ import annotations

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert (
	    c % greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						) == 0
	)  # greatest_common_divisor(a,b) function implemented below
	((A_) ,			(A_) ,			(A_))	:					List[Any]						 =		extended_gcd(_UpperCAmelCase	,   _UpperCAmelCase						)  # extended_gcd(a,b) function implemented below
	A_	:					int						 =		c / d
	return (r * x, r * y)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase = 2						):




	"""simple docstring"""






	((A_) ,			(A_))	:					Any						 =		diophantine(_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						)  # Initial value
	A_	:					Union[str, Any]						 =		greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						)
	A_	:					List[str]						 =		a // d
	A_	:					str						 =		b // d

	for i in range(_UpperCAmelCase						):
		A_	:					int						 =		xa + i * q
		A_	:					str						 =		ya - i * p
		print(_UpperCAmelCase	,   _UpperCAmelCase						)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	if a < b:
		A_ ,			A_	:					Tuple						 =		b, a

	while a % b != 0:
		A_ ,			A_	:					int						 =		b, a % b

	return b

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert a >= 0 and b >= 0

	if b == 0:
		A_ ,			A_ ,			A_	:					str						 =		a, 1, 0
	else:
		((A_) ,			(A_) ,			(A_))	:					List[str]						 =		extended_gcd(_UpperCAmelCase	,   a % b						)
		A_	:					Optional[int]						 =		q
		A_	:					Tuple						 =		p - q * (a // b)

	assert a % d == 0 and b % d == 0
	assert d == a * x + b * y

	return (d, x, y)


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

		testmod(name='diophantine', verbose=True)
		testmod(name='diophantine_all_soln', verbose=True)
		testmod(name='extended_gcd', verbose=True)
		testmod(name='greatest_common_divisor', verbose=True)





