





import numpy






class 						UpperCamelCase_  :



					def __init__(		self     :     Optional[int]       ,      lowerCamelCase     :     numpy.ndarray       ,      lowerCamelCase     :     numpy.ndarray						):
							lowerCamelCase_	:     Dict 						=      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.
							lowerCamelCase_	:     str 						=      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.
							lowerCamelCase_	:     str 						=      numpy.random.rand(
							    4       ,      3						)

							# Random initial values for the second hidden layer.
							# Second hidden layer has 3 nodes.
							# Output layer has 1 node.
							lowerCamelCase_	:     List[str] 						=      numpy.random.rand(3       ,      1						)

							# Real output values provided.
							lowerCamelCase_	:     List[str] 						=      output_array

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



					def   __a     (		self     :     Any						):
							lowerCamelCase_	:     Tuple 						=      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.
							lowerCamelCase_	:     List[str] 						=      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.
							lowerCamelCase_	:     int 						=      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   __a     (		self     :     Optional[int]						):
							lowerCamelCase_	:     int 						=      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						)       ,      )
							lowerCamelCase_	:     Tuple 						=      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						)       ,      )
							lowerCamelCase_	:     List[str] 						=      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   __a     (		self     :     List[Any]       ,      lowerCamelCase     :     numpy.ndarray       ,      lowerCamelCase     :     int       ,      lowerCamelCase     :     bool						):
							for iteration in range(1       ,      iterations + 1						):
									lowerCamelCase_	:     Union[str, Any] 						=      self.feedforward()
									self.back_propagation()
									if give_loss:
											lowerCamelCase_	:     Tuple 						=      numpy.mean(numpy.square(output - self.feedforward()						)						)
											print(F"Iteration {iteration} Loss: {loss}"						)





					def   __a     (		self     :     int       ,      lowerCamelCase     :     numpy.ndarray						):
							lowerCamelCase_	:     Optional[int] 						=      input_arr

							lowerCamelCase_	:     List[Any] 						=      sigmoid(
							    numpy.dot(self.array       ,      self.input_layer_and_first_hidden_layer_weights						)						)

							lowerCamelCase_	:     Tuple 						=      sigmoid(
							    numpy.dot(
							        self.layer_between_input_and_first_hidden_layer       ,      self.first_hidden_layer_and_second_hidden_layer_weights       ,      )						)

							lowerCamelCase_	:     str 						=      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 		_SCREAMING_SNAKE_CASE     ( lowerCAmelCase__						):
		return 1 / (1 + numpy.exp(-value						))


def 		_SCREAMING_SNAKE_CASE     ( lowerCAmelCase__						):
		return (value) * (1 - (value))


def 		_SCREAMING_SNAKE_CASE     ( ):
		lowerCamelCase_	:     Any 						=      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.
		lowerCamelCase_	:     Any 						=      numpy.array(([0], [1], [1], [0], [1], [0], [0], [1])							,dtype=numpy.floataa						)

		# Calling neural network class.
		lowerCamelCase_	:     List[Any] 						=      TwoHiddenLayerNeuralNetwork(
		    input_array=lowerCAmelCase__							,output_array=lowerCAmelCase__						)

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

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


if __name__ == "__main__":
		example()







import torch

from diffusers import DPMSolverSDEScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import require_torchsde

from .test_schedulers import SchedulerCommonTest






@require_torchsde
class 						UpperCamelCase_  (  snake_case__							):
					_a      :						Union[str, Any]						    =					(DPMSolverSDEScheduler,)
					_a      :						List[Any]						    =					1_0



					def   __a     (		self     :     Any       ,      **lowerCamelCase     :     str						):
							lowerCamelCase_	:     Any 						=      {
							    'num_train_timesteps': 11_00,
							    'beta_start': 0.0_001,
							    'beta_end': 0.02,
							    'beta_schedule': 'linear',
							    'noise_sampler_seed': 0,
							}

							config.update(**lowerCamelCase						)
							return config



					def   __a     (		self     :     Tuple						):
							for timesteps in [10, 50, 1_00, 10_00]:
									self.check_over_configs(num_train_timesteps=lowerCamelCase						)



					def   __a     (		self     :     int						):
							for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]       ,      [0.0_002, 0.002, 0.02]						):
									self.check_over_configs(beta_start=lowerCamelCase       ,      beta_end=lowerCamelCase						)



					def   __a     (		self     :     Any						):
							for schedule in ["linear", "scaled_linear"]:
									self.check_over_configs(beta_schedule=lowerCamelCase						)



					def   __a     (		self     :     Optional[int]						):
							for prediction_type in ["epsilon", "v_prediction"]:
									self.check_over_configs(prediction_type=lowerCamelCase						)



					def   __a     (		self     :     Optional[int]						):
							lowerCamelCase_	:     int 						=      self.scheduler_classes[0]
							lowerCamelCase_	:     str 						=      self.get_scheduler_config()
							lowerCamelCase_	:     Optional[Any] 						=      scheduler_class(**lowerCamelCase						)

							scheduler.set_timesteps(self.num_inference_steps						)

							lowerCamelCase_	:     Optional[Any] 						=      self.dummy_model()
							lowerCamelCase_	:     Optional[Any] 						=      self.dummy_sample_deter * scheduler.init_noise_sigma
							lowerCamelCase_	:     Union[str, Any] 						=      sample.to(lowerCamelCase						)

							for i, t in enumerate(scheduler.timesteps						):
									lowerCamelCase_	:     str 						=      scheduler.scale_model_input(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Tuple 						=      model(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Optional[int] 						=      scheduler.step(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase						)
									lowerCamelCase_	:     Tuple 						=      output.prev_sample

							lowerCamelCase_	:     List[str] 						=      torch.sum(torch.abs(lowerCamelCase						)						)
							lowerCamelCase_	:     Optional[Any] 						=      torch.mean(torch.abs(lowerCamelCase						)						)

							if torch_device in ["mps"]:
									assert abs(result_sum.item() - 167.47_821_044_921_875						) < 1E-2
									assert abs(result_mean.item() - 0.2_178_705_964_565_277						) < 1E-3
							elif torch_device in ["cuda"]:
									assert abs(result_sum.item() - 171.59_352_111_816_406						) < 1E-2
									assert abs(result_mean.item() - 0.22_342_906_892_299_652						) < 1E-3
							else:
									assert abs(result_sum.item() - 162.52_383_422_851_562						) < 1E-2
									assert abs(result_mean.item() - 0.211_619_570_851_326						) < 1E-3



					def   __a     (		self     :     Tuple						):
							lowerCamelCase_	:     List[str] 						=      self.scheduler_classes[0]
							lowerCamelCase_	:     Any 						=      self.get_scheduler_config(prediction_type='v_prediction'						)
							lowerCamelCase_	:     Tuple 						=      scheduler_class(**lowerCamelCase						)

							scheduler.set_timesteps(self.num_inference_steps						)

							lowerCamelCase_	:     Any 						=      self.dummy_model()
							lowerCamelCase_	:     Tuple 						=      self.dummy_sample_deter * scheduler.init_noise_sigma
							lowerCamelCase_	:     List[Any] 						=      sample.to(lowerCamelCase						)

							for i, t in enumerate(scheduler.timesteps						):
									lowerCamelCase_	:     Dict 						=      scheduler.scale_model_input(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Optional[int] 						=      model(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Union[str, Any] 						=      scheduler.step(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase						)
									lowerCamelCase_	:     Dict 						=      output.prev_sample

							lowerCamelCase_	:     Optional[Any] 						=      torch.sum(torch.abs(lowerCamelCase						)						)
							lowerCamelCase_	:     List[Any] 						=      torch.mean(torch.abs(lowerCamelCase						)						)

							if torch_device in ["mps"]:
									assert abs(result_sum.item() - 124.77_149_200_439_453						) < 1E-2
									assert abs(result_mean.item() - 0.16_226_289_014_816_284						) < 1E-3
							elif torch_device in ["cuda"]:
									assert abs(result_sum.item() - 128.1_663_360_595_703						) < 1E-2
									assert abs(result_mean.item() - 0.16_688_326_001_167_297						) < 1E-3
							else:
									assert abs(result_sum.item() - 119.8_487_548_828_125						) < 1E-2
									assert abs(result_mean.item() - 0.1_560_530_662_536_621						) < 1E-3



					def   __a     (		self     :     Optional[Any]						):
							lowerCamelCase_	:     Union[str, Any] 						=      self.scheduler_classes[0]
							lowerCamelCase_	:     List[Any] 						=      self.get_scheduler_config()
							lowerCamelCase_	:     List[str] 						=      scheduler_class(**lowerCamelCase						)

							scheduler.set_timesteps(self.num_inference_steps       ,      device=lowerCamelCase						)

							lowerCamelCase_	:     Dict 						=      self.dummy_model()
							lowerCamelCase_	:     Dict 						=      self.dummy_sample_deter.to(lowerCamelCase						) * scheduler.init_noise_sigma

							for t in scheduler.timesteps:
									lowerCamelCase_	:     Tuple 						=      scheduler.scale_model_input(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     List[str] 						=      model(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     int 						=      scheduler.step(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase						)
									lowerCamelCase_	:     Optional[int] 						=      output.prev_sample

							lowerCamelCase_	:     List[Any] 						=      torch.sum(torch.abs(lowerCamelCase						)						)
							lowerCamelCase_	:     str 						=      torch.mean(torch.abs(lowerCamelCase						)						)

							if torch_device in ["mps"]:
									assert abs(result_sum.item() - 167.46_957_397_460_938						) < 1E-2
									assert abs(result_mean.item() - 0.21_805_934_607_982_635						) < 1E-3
							elif torch_device in ["cuda"]:
									assert abs(result_sum.item() - 171.59_353_637_695_312						) < 1E-2
									assert abs(result_mean.item() - 0.22_342_908_382_415_771						) < 1E-3
							else:
									assert abs(result_sum.item() - 162.52_383_422_851_562						) < 1E-2
									assert abs(result_mean.item() - 0.211_619_570_851_326						) < 1E-3





					def   __a     (		self     :     Any						):
							lowerCamelCase_	:     Dict 						=      self.scheduler_classes[0]
							lowerCamelCase_	:     List[Any] 						=      self.get_scheduler_config()
							lowerCamelCase_	:     Union[str, Any] 						=      scheduler_class(**lowerCamelCase       ,      use_karras_sigmas=lowerCamelCase						)

							scheduler.set_timesteps(self.num_inference_steps       ,      device=lowerCamelCase						)

							lowerCamelCase_	:     int 						=      self.dummy_model()
							lowerCamelCase_	:     str 						=      self.dummy_sample_deter.to(lowerCamelCase						) * scheduler.init_noise_sigma
							lowerCamelCase_	:     Dict 						=      sample.to(lowerCamelCase						)

							for t in scheduler.timesteps:
									lowerCamelCase_	:     Optional[Any] 						=      scheduler.scale_model_input(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Dict 						=      model(lowerCamelCase       ,      lowerCamelCase						)

									lowerCamelCase_	:     Dict 						=      scheduler.step(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase						)
									lowerCamelCase_	:     Any 						=      output.prev_sample

							lowerCamelCase_	:     Optional[int] 						=      torch.sum(torch.abs(lowerCamelCase						)						)
							lowerCamelCase_	:     Union[str, Any] 						=      torch.mean(torch.abs(lowerCamelCase						)						)

							if torch_device in ["mps"]:
									assert abs(result_sum.item() - 176.66_974_135_742_188						) < 1E-2
									assert abs(result_mean.item() - 0.23_003_872_730_981_811						) < 1E-2
							elif torch_device in ["cuda"]:
									assert abs(result_sum.item() - 177.63_653_564_453_125						) < 1E-2
									assert abs(result_mean.item() - 0.23_003_872_730_981_811						) < 1E-2
							else:
									assert abs(result_sum.item() - 170.3_135_223_388_672						) < 1E-2
									assert abs(result_mean.item() - 0.23_003_872_730_981_811						) < 1E-2
