






import numpy


class     _a							:






  """simple docstring"""





  def __init__(  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			None:
     UpperCamelCase_						=  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_						=  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_						=  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_						=  numpy.random.rand(3		,		1			)

     # Real output values provided.
     UpperCamelCase_						=  output_array

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




  def  _UpperCAmelCase  (  self			)      ->			numpy.ndarray:
     UpperCamelCase_						=  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_						=  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_						=  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  _UpperCAmelCase  (  self			)      ->			None:
     UpperCamelCase_						=  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_						=  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_						=  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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			None:
     for iteration in range(1		,		iterations + 1			):
        UpperCamelCase_						=  self.feedforward()
        self.back_propagation()
        if give_loss:
           UpperCamelCase_						=  numpy.mean(numpy.square(output - self.feedforward()			)			)
           print(f"""Iteration {iteration} Loss: {loss}"""			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase			)      ->			int:
     UpperCamelCase_						=  input_arr

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

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

     UpperCamelCase_						=  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      _snake_case     (__lowercase):
   return 1 / (1 + numpy.exp(-value))






def      _snake_case     (__lowercase):
   return (value) * (1 - (value))






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

   # Calling neural network class.
   UpperCamelCase_						=  TwoHiddenLayerNeuralNetwork(
       input_array=__lowercase     ,   output_array=__lowercase)

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

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


if __name__ == "__main__":
 example()













import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam


class     _a							(	datasets.BeamBasedBuilder     ):






  """simple docstring"""





  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     return datasets.DatasetInfo(
         features=datasets.Features({'content': datasets.Value('string'			)}			)		,		supervised_keys=_UpperCAmelCase		,		)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Optional[Any]:
     return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,		gen_kwargs={'examples': get_test_dummy_examples()}			)]




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Tuple:
     import apache_beam as beam

     return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase			)




class     _a							(	datasets.BeamBasedBuilder     ):






  """simple docstring"""





  def  _UpperCAmelCase  (  self			)      ->			Any:
     return datasets.DatasetInfo(
         features=datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string'			)}			)}			)		,		supervised_keys=_UpperCAmelCase		,		)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Tuple:
     return [
         datasets.SplitGenerator(name=datasets.Split.TRAIN		,		gen_kwargs={'examples': get_test_nested_examples()}			)
     ]




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Union[str, Any]:
     import apache_beam as beam

     return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase			)






def      _snake_case     ():
   return [(i, {"content": content}) for i, content in enumerate(['foo', 'bar', 'foobar'])]






def      _snake_case     ():
   return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['foo', 'bar', 'foobar'])]


class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_						=  len(get_test_dummy_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train.arrow"""			)			)			)
        self.assertDictEqual(builder.info.features		,		datasets.Features({'content': datasets.Value('string'			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        self.assertDictEqual(dset['train'][0]		,		get_test_dummy_examples()[0][1]			)
        self.assertDictEqual(
            dset['train'][expected_num_examples - 1]		,		get_test_dummy_examples()[expected_num_examples - 1][1]			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     import apache_beam as beam

     UpperCamelCase_						=  beam.io.parquetio.WriteToParquet

     UpperCamelCase_						=  len(get_test_dummy_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        with patch('apache_beam.io.parquetio.WriteToParquet'			) as write_parquet_mock:
           UpperCamelCase_						=  partial(_UpperCAmelCase		,		num_shards=2			)
           builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    _UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train-00000-of-00002.arrow"""			)			)			)
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    _UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train-00000-of-00002.arrow"""			)			)			)
        self.assertDictEqual(builder.info.features		,		datasets.Features({'content': datasets.Value('string'			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        # Order is not preserved when sharding, so we just check that all the elements are there
        self.assertListEqual(sorted(dset['train']['content']			)		,		sorted(['foo', 'bar', 'foobar']			)			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Any:
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  DummyBeamDataset(cache_dir=_UpperCAmelCase			)
        self.assertRaises(datasets.builder.MissingBeamOptions		,		builder.download_and_prepare			)




  @require_beam
  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     UpperCamelCase_						=  len(get_test_nested_examples()			)
     with tempfile.TemporaryDirectory() as tmp_cache_dir:
        UpperCamelCase_						=  NestedBeamDataset(cache_dir=_UpperCAmelCase		,		beam_runner='DirectRunner'			)
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		f"""{builder.name}-train.arrow"""			)			)			)
        self.assertDictEqual(
            builder.info.features		,		datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string'			)}			)}			)			)
        UpperCamelCase_						=  builder.as_dataset()
        self.assertEqual(dset['train'].num_rows		,		_UpperCAmelCase			)
        self.assertEqual(dset['train'].info.splits['train'].num_examples		,		_UpperCAmelCase			)
        self.assertDictEqual(dset['train'][0]		,		get_test_nested_examples()[0][1]			)
        self.assertDictEqual(
            dset['train'][expected_num_examples - 1]		,		get_test_nested_examples()[expected_num_examples - 1][1]			)
        self.assertTrue(
            os.path.exists(os.path.join(_UpperCAmelCase		,		builder.name		,		'default'		,		'0.0.0'		,		'dataset_info.json'			)			)			)
        del dset





