

'''simple docstring'''

from manim import *





class 				_snake_case			(							UpperCAmelCase__    ):
   def      __UpperCamelCase    (			self      :   str				):
     SCREAMING_SNAKE_CASE:Tuple       =       Rectangle(height=0.5  ,width=0.5				)
     SCREAMING_SNAKE_CASE:int       =       Rectangle(height=0.46  ,width=0.46				).set_stroke(width=0				)

     SCREAMING_SNAKE_CASE:Any       =       Rectangle(height=0.25  ,width=0.25				)

     SCREAMING_SNAKE_CASE:List[str]       =       [mem.copy() for i in range(6				)]
     SCREAMING_SNAKE_CASE:Optional[int]       =       [mem.copy() for i in range(6				)]
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:Dict       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:Any       =       VGroup(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:int       =       Text("CPU"  ,font_size=24				)
     SCREAMING_SNAKE_CASE:Any       =       Group(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0.5  ,aligned_edge=__lowerCAmelCase				)
     cpu.move_to([-2.5, -0.5, 0]				)
     self.add(__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:Optional[Any]       =       [mem.copy() for i in range(4				)]
     SCREAMING_SNAKE_CASE:Dict       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:Optional[int]       =       Text("GPU"  ,font_size=24				)
     SCREAMING_SNAKE_CASE:List[str]       =       Group(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0.5  ,aligned_edge=__lowerCAmelCase				)
     gpu.move_to([-1, -1, 0]				)
     self.add(__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:Dict       =       [mem.copy() for i in range(6				)]
     SCREAMING_SNAKE_CASE:Optional[Any]       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)

     SCREAMING_SNAKE_CASE:List[str]       =       Text("Model"  ,font_size=24				)
     SCREAMING_SNAKE_CASE:Any       =       Group(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0.5  ,aligned_edge=__lowerCAmelCase				)
     model.move_to([3, -1.0, 0]				)
     self.add(__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:str       =       []
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       []

     for i, rect in enumerate(__lowerCAmelCase				):
       SCREAMING_SNAKE_CASE:Tuple       =       fill.copy().set_fill(__lowerCAmelCase  ,opacity=0.8				)
       target.move_to(__lowerCAmelCase				)
       model_arr.append(__lowerCAmelCase				)

       SCREAMING_SNAKE_CASE:Optional[Any]       =       Rectangle(height=0.46  ,width=0.46				).set_stroke(width=0.0				).set_fill(__lowerCAmelCase  ,opacity=0.8				)
       cpu_target.move_to(cpu_left_col_base[i]				)
       model_cpu_arr.append(__lowerCAmelCase				)

     self.add(*__lowerCAmelCase  ,*__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:Tuple       =       [meta_mem.copy() for i in range(6				)]
     SCREAMING_SNAKE_CASE:Dict       =       [meta_mem.copy() for i in range(6				)]
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:str       =       VGroup(*__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:List[Any]       =       VGroup(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0				)
     SCREAMING_SNAKE_CASE:int       =       Text("Disk"  ,font_size=24				)
     SCREAMING_SNAKE_CASE:Optional[Any]       =       Group(__lowerCAmelCase  ,__lowerCAmelCase				).arrange(__lowerCAmelCase  ,buff=0.5  ,aligned_edge=__lowerCAmelCase				)
     disk.move_to([-4, -1.25, 0]				)
     self.add(__lowerCAmelCase  ,__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:List[Any]       =       Square(side_length=2.2				)
     key.move_to([-5, 2, 0]				)

     SCREAMING_SNAKE_CASE:Tuple       =       MarkupText(
         F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''  ,font_size=18  ,)

     key_text.move_to([-5, 2.4, 0]				)

     self.add(__lowerCAmelCase  ,__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:Optional[Any]       =       MarkupText(
         F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''  ,font_size=18  ,)

     blue_text.next_to(__lowerCAmelCase  ,DOWN * 2.4  ,aligned_edge=key_text.get_left()				)
     self.add(__lowerCAmelCase				)

     SCREAMING_SNAKE_CASE:List[str]       =       MarkupText(
         F'''Now watch as an input is passed through the model\nand how the memory is utilized and handled.'''  ,font_size=24  ,)
     step_a.move_to([2, 2, 0]				)

     self.play(Write(__lowerCAmelCase				)				)

     SCREAMING_SNAKE_CASE:List[Any]       =       Square(0.3				)
     input.set_fill(__lowerCAmelCase  ,opacity=1.0				)
     input.set_stroke(width=0.0				)
     input.next_to(model_base[0]  ,__lowerCAmelCase  ,buff=0.5				)

     self.play(Write(__lowerCAmelCase				)				)

     input.generate_target()
     input.target.next_to(model_arr[0]  ,direction=__lowerCAmelCase  ,buff=0.02				)
     self.play(MoveToTarget(__lowerCAmelCase				)				)

     self.play(FadeOut(__lowerCAmelCase				)				)

     SCREAMING_SNAKE_CASE:Union[str, Any]       =       Arrow(start=__lowerCAmelCase  ,end=__lowerCAmelCase  ,color=__lowerCAmelCase  ,buff=0.5				)
     a.next_to(model_arr[0].get_left()  ,__lowerCAmelCase  ,buff=0.2				)

     model_cpu_arr[0].generate_target()
     model_cpu_arr[0].target.move_to(gpu_rect[0]				)

     SCREAMING_SNAKE_CASE:List[str]       =       MarkupText(
         F'''As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.'''  ,font_size=24  ,)
     step_a.move_to([2, 2, 0]				)

     self.play(Write(__lowerCAmelCase  ,run_time=3				)				)

     SCREAMING_SNAKE_CASE:Optional[Any]       =       {"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.02}

     self.play(
         Write(__lowerCAmelCase				)  ,Circumscribe(model_arr[0]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(model_cpu_arr[0]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(gpu_rect[0]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,)
     self.play(MoveToTarget(model_cpu_arr[0]				)				)

     SCREAMING_SNAKE_CASE:List[Any]       =       a.copy()
     for i in range(6				):
       a_c.next_to(model_arr[i].get_right() + 0.02  ,__lowerCAmelCase  ,buff=0.2				)

       input.generate_target()
       input.target.move_to(model_arr[i].get_right() + 0.02				)

       SCREAMING_SNAKE_CASE:List[str]       =       AnimationGroup(
           FadeOut(__lowerCAmelCase  ,run_time=0.5				)  ,MoveToTarget(__lowerCAmelCase  ,run_time=0.5				)  ,FadeIn(__lowerCAmelCase  ,run_time=0.5				)  ,lag_ratio=0.2				)

       self.play(__lowerCAmelCase				)

       model_cpu_arr[i].generate_target()
       model_cpu_arr[i].target.move_to(cpu_left_col_base[i]				)

       if i < 5:
         model_cpu_arr[i + 1].generate_target()
         model_cpu_arr[i + 1].target.move_to(gpu_rect[0]				)
         if i >= 1:
           SCREAMING_SNAKE_CASE:str       =       0.7

         self.play(
             Circumscribe(model_arr[i]  ,**__lowerCAmelCase				)  ,Circumscribe(cpu_left_col_base[i]  ,**__lowerCAmelCase				)  ,Circumscribe(cpu_left_col_base[i + 1]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(gpu_rect[0]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(model_arr[i + 1]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,)
         if i < 1:
           self.play(
               MoveToTarget(model_cpu_arr[i]				)  ,MoveToTarget(model_cpu_arr[i + 1]				)  ,)
         else:
           self.play(
               MoveToTarget(model_cpu_arr[i]  ,run_time=0.7				)  ,MoveToTarget(model_cpu_arr[i + 1]  ,run_time=0.7				)  ,)
       else:
         model_cpu_arr[i].generate_target()
         model_cpu_arr[i].target.move_to(cpu_left_col_base[-1]				)
         input.generate_target()
         input.target.next_to(model_arr[-1].get_right()  ,RIGHT + 0.02  ,buff=0.2				)

         self.play(
             Circumscribe(model_arr[-1]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(cpu_left_col_base[-1]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,Circumscribe(gpu_rect[0]  ,color=__lowerCAmelCase  ,**__lowerCAmelCase				)  ,)

         self.play(MoveToTarget(model_cpu_arr[i]				)				)

       SCREAMING_SNAKE_CASE:str       =       a_c
       SCREAMING_SNAKE_CASE:int       =       a_c.copy()

     input.generate_target()
     input.target.next_to(model_base[-1]  ,RIGHT + 0.02  ,buff=0.5				)
     self.play(
         FadeOut(__lowerCAmelCase				)  ,FadeOut(__lowerCAmelCase  ,run_time=0.5				)  ,)

     SCREAMING_SNAKE_CASE:List[Any]       =       MarkupText(F'''Inference on a model too large for GPU memory\nis successfully completed.'''  ,font_size=24				)
     step_a.move_to([2, 2, 0]				)

     self.play(Write(__lowerCAmelCase  ,run_time=3				)  ,MoveToTarget(__lowerCAmelCase				)				)

     self.wait()












'''simple docstring'''


import numpy
class 						a_				:




	def __init__(       self     :  Any       ,		__lowerCAmelCase     :  numpy.ndarray       ,		__lowerCAmelCase     :  numpy.ndarray      ):
							__snake_case						 =   input_array

							# Random initial weights are assigned where first argument is the
							# number of nodes in previous layer and second argument is the
							# number of nodes in the next layer.

							# Random initial weights are assigned.
							# self.input_array.shape[1] is used to represent number of nodes in input layer.
							# First hidden layer consists of 4 nodes.
							__snake_case						 =   numpy.random.rand(
							    self.input_array.shape[1]       ,		4      )

							# Random initial values for the first hidden layer.
							# First hidden layer has 4 nodes.
							# Second hidden layer has 3 nodes.
							__snake_case						 =   numpy.random.rand(
							    4       ,		3      )

							# Random initial values for the second hidden layer.
							# Second hidden layer has 3 nodes.
							# Output layer has 1 node.
							__snake_case						 =   numpy.random.rand(3       ,		1      )

							# Real output values provided.
							__snake_case						 =   output_array

							# Predicted output values by the neural network.
							# Predicted_output array initially consists of zeroes.
							__snake_case						 =   numpy.zeros(output_array.shape      )




	def   lowercase__   (       self     :  Optional[Any]      ):
							__snake_case						 =   sigmoid(
							    numpy.dot(self.input_array       ,		self.input_layer_and_first_hidden_layer_weights      )      )

							# layer_between_first_hidden_layer_and_second_hidden_layer is the layer
							# connecting the first hidden set of nodes with the second hidden set of nodes.
							__snake_case						 =   sigmoid(
							    numpy.dot(
							        self.layer_between_input_and_first_hidden_layer       ,		self.first_hidden_layer_and_second_hidden_layer_weights       ,		)      )

							# layer_between_second_hidden_layer_and_output is the layer connecting
							# second hidden layer with the output node.
							__snake_case						 =   sigmoid(
							    numpy.dot(
							        self.layer_between_first_hidden_layer_and_second_hidden_layer       ,		self.second_hidden_layer_and_output_layer_weights       ,		)      )

							return self.layer_between_second_hidden_layer_and_output




	def   lowercase__   (       self     :  List[str]      ):
							__snake_case						 =   numpy.dot(
							    self.layer_between_first_hidden_layer_and_second_hidden_layer.T       ,		2
							    * (self.output_array - self.predicted_output)
							    * sigmoid_derivative(self.predicted_output      )       ,		)
							__snake_case						 =   numpy.dot(
							    self.layer_between_input_and_first_hidden_layer.T       ,		numpy.dot(
							        2
							        * (self.output_array - self.predicted_output)
							        * sigmoid_derivative(self.predicted_output      )       ,		self.second_hidden_layer_and_output_layer_weights.T       ,		)
							    * sigmoid_derivative(
							        self.layer_between_first_hidden_layer_and_second_hidden_layer      )       ,		)
							__snake_case						 =   numpy.dot(
							    self.input_array.T       ,		numpy.dot(
							        numpy.dot(
							            2
							            * (self.output_array - self.predicted_output)
							            * sigmoid_derivative(self.predicted_output      )       ,		self.second_hidden_layer_and_output_layer_weights.T       ,		)
							        * sigmoid_derivative(
							            self.layer_between_first_hidden_layer_and_second_hidden_layer      )       ,		self.first_hidden_layer_and_second_hidden_layer_weights.T       ,		)
							    * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer      )       ,		)

							self.input_layer_and_first_hidden_layer_weights += (
							    updated_input_layer_and_first_hidden_layer_weights
							)
							self.first_hidden_layer_and_second_hidden_layer_weights += (
							    updated_first_hidden_layer_and_second_hidden_layer_weights
							)
							self.second_hidden_layer_and_output_layer_weights += (
							    updated_second_hidden_layer_and_output_layer_weights
							)




	def   lowercase__   (       self     :  Optional[Any]       ,		__lowerCAmelCase     :  numpy.ndarray       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  bool      ):
							for iteration in range(1       ,		iterations + 1      ):
													__snake_case						 =   self.feedforward()
													self.back_propagation()
													if give_loss:
																			__snake_case						 =   numpy.mean(numpy.square(output - self.feedforward()      )      )
																			print(F'Iteration {iteration} Loss: {loss}'      )




	def   lowercase__   (       self     :  Optional[Any]       ,		__lowerCAmelCase     :  numpy.ndarray      ):
							__snake_case						 =   input_arr

							__snake_case						 =   sigmoid(
							    numpy.dot(self.array       ,		self.input_layer_and_first_hidden_layer_weights      )      )

							__snake_case						 =   sigmoid(
							    numpy.dot(
							        self.layer_between_input_and_first_hidden_layer       ,		self.first_hidden_layer_and_second_hidden_layer_weights       ,		)      )

							__snake_case						 =   sigmoid(
							    numpy.dot(
							        self.layer_between_first_hidden_layer_and_second_hidden_layer       ,		self.second_hidden_layer_and_output_layer_weights       ,		)      )

							return int(self.layer_between_second_hidden_layer_and_output > 0.6      )





def 						lowerCamelCase__   (				a							):
						return 1 / (1 + numpy.exp(-value							))
def 						lowerCamelCase__   (				a							):
						return (value) * (1 - (value))
def 						lowerCamelCase__   (				):
						__snake_case						 =   numpy.array(
						    (
						        [0, 0, 0],
						        [0, 0, 1],
						        [0, 1, 0],
						        [0, 1, 1],
						        [1, 0, 0],
						        [1, 0, 1],
						        [1, 1, 0],
						        [1, 1, 1],
						    )    ,	dtype=numpy.floataa    ,	)

						# True output values for the given input values.
						__snake_case						 =   numpy.array(([0], [1], [1], [0], [1], [0], [0], [1])    ,	dtype=numpy.floataa							)

						# Calling neural network class.
						__snake_case						 =   TwoHiddenLayerNeuralNetwork(
						    input_array=a    ,	output_array=a							)

						# Calling training function.
						# Set give_loss to True if you want to see loss in every iteration.
						neural_network.train(output=a    ,	iterations=10    ,	give_loss=a							)

						return neural_network.predict(numpy.array(([1, 1, 1])    ,	dtype=numpy.floataa							)							)


if __name__ == "__main__":
	example()

