
import numpy

class 							_UpperCamelCase					:






			'''simple docstring'''

			def __init__(							self			:							Union[str, Any]    ,						snake_case_			:							numpy.ndarray    ,						snake_case_			:							numpy.ndarray   ):
							UpperCamelCase_:				Optional[int]						=    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.
							UpperCamelCase_:				Union[str, Any]						=    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.
							UpperCamelCase_:				List[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.
							UpperCamelCase_:				List[str]						=    numpy.random.rand(3    ,						1   )

							# Real output values provided.
							UpperCamelCase_:				Tuple						=    output_array

							# Predicted output values by the neural network.
							# Predicted_output array initially consists of zeroes.
							UpperCamelCase_:				Union[str, Any]						=    numpy.zeros(output_array.shape   )

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




			def    lowerCAmelCase__						(							self			:							List[str]    ,						snake_case_			:							numpy.ndarray   ):
							UpperCamelCase_:				List[Any]						=    input_arr

							UpperCamelCase_:				Dict						=    sigmoid(
							    numpy.dot(self.array    ,						self.input_layer_and_first_hidden_layer_weights   )   )

							UpperCamelCase_:				Union[str, Any]						=    sigmoid(
							    numpy.dot(
							        self.layer_between_input_and_first_hidden_layer    ,						self.first_hidden_layer_and_second_hidden_layer_weights    ,						)   )

							UpperCamelCase_:				List[Any]						=    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 							A__       (    lowerCamelCase    )	->						numpy.ndarray:
				return 1 / (1 + numpy.exp(-value    ))







def 							A__       (    lowerCamelCase    )	->						numpy.ndarray:
				return (value) * (1 - (value))







def 							A__       (    )	->						int:
				UpperCamelCase_:				List[str]						=    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.
				UpperCamelCase_:				List[str]						=    numpy.array(([0], [1], [1], [0], [1], [0], [0], [1])  ,     dtype=numpy.floataa    )

				# Calling neural network class.
				UpperCamelCase_:				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 subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch

class 							_UpperCamelCase					(       _A	):






			'''simple docstring'''

			@require_torch
			def    lowerCAmelCase__						(							self			:							List[str]   ):
							# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
							# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
							# while running an external program

							# python one-liner segments

							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				Optional[Any]						=    """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

							UpperCamelCase_:				List[Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task=\"fill-mask\", model=mname)
print(\"success\")
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\")
socket.socket = offline_socket
        """

							# Force fetching the files so that we can use the cache
							UpperCamelCase_:				int						=    """hf-internal-testing/tiny-random-bert"""
							BertConfig.from_pretrained(snake_case_   )
							BertModel.from_pretrained(snake_case_   )
							BertTokenizer.from_pretrained(snake_case_   )
							pipeline(task="""fill-mask"""    ,						model=snake_case_   )

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Tuple						=    [sys.executable, """-c""", """\n""".join([load, run, mock]   )]

							# should succeed
							UpperCamelCase_:				Optional[int]						=    self.get_env()
							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				int						=    """1"""
							UpperCamelCase_:				Optional[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							Any   ):
							# python one-liner segments
							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				str						=    """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

							UpperCamelCase_:				Any						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task=\"fill-mask\", model=mname)
print(\"success\")
        """

							UpperCamelCase_:				Optional[int]						=    """
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\")
socket.socket = offline_socket
        """

							# Force fetching the files so that we can use the cache
							UpperCamelCase_:				Optional[int]						=    """hf-internal-testing/tiny-random-bert"""
							BertConfig.from_pretrained(snake_case_   )
							BertModel.from_pretrained(snake_case_   )
							BertTokenizer.from_pretrained(snake_case_   )
							pipeline(task="""fill-mask"""    ,						model=snake_case_   )

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, run, mock]   )]

							# should succeed
							UpperCamelCase_:				List[str]						=    self.get_env()
							UpperCamelCase_:				List[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							int   ):
							# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
							# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
							# while running an external program

							# python one-liner segments

							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				Dict						=    """
from transformers import BertConfig, BertModel, BertTokenizer
        """

							UpperCamelCase_:				Optional[Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert-sharded\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print(\"success\")
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\")
socket.socket = offline_socket
        """

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Union[str, Any]						=    [sys.executable, """-c""", """\n""".join([load, run]   )]

							# should succeed
							UpperCamelCase_:				Union[str, Any]						=    self.get_env()
							UpperCamelCase_:				List[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

							# next emulate no network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, mock, run]   )]

							# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
							# env["TRANSFORMERS_OFFLINE"] = "0"
							# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
							# self.assertEqual(result.returncode, 1, result.stderr)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				Union[str, Any]						=    """1"""
							UpperCamelCase_:				int						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							List[str]   ):
							UpperCamelCase_:				Dict						=    """
from transformers import pipeline
        """
							UpperCamelCase_:				Union[str, Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
pipe = pipeline(model=mname)
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")
socket.socket = offline_socket
        """
							UpperCamelCase_:				Optional[Any]						=    self.get_env()
							UpperCamelCase_:				int						=    """1"""
							UpperCamelCase_:				int						=    [sys.executable, """-c""", """\n""".join([load, mock, run]   )]
							UpperCamelCase_:				Any						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						1    ,						result.stderr   )
							self.assertIn(
							    """You cannot infer task automatically within `pipeline` when using offline mode"""    ,						result.stderr.decode().replace("""\n"""    ,						""""""   )    ,						)




			@require_torch
			def    lowerCAmelCase__						(							self			:							Optional[int]   ):
							UpperCamelCase_:				str						=    """
from transformers import AutoModel
        """
							UpperCamelCase_:				Dict						=    """
mname = \"hf-internal-testing/test_dynamic_model\"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print(\"success\")
        """

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, run]   )]

							# should succeed
							UpperCamelCase_:				Optional[Any]						=    self.get_env()
							UpperCamelCase_:				int						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				Union[str, Any]						=    """1"""
							UpperCamelCase_:				List[str]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )






