0% found this document useful (0 votes)
18 views

Neural Network

A neural network is loosely based on how the human brain works, consisting of artificial neurons and synapses that pass information through weighted connections. The network learns by optimizing weights through backpropagation to map inputs to outputs. This example creates a basic neural network from scratch in Python using NumPy and SciPy, defining the network class and methods for initialization, running, and training.

Uploaded by

Mohamed Aymen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Neural Network

A neural network is loosely based on how the human brain works, consisting of artificial neurons and synapses that pass information through weighted connections. The network learns by optimizing weights through backpropagation to map inputs to outputs. This example creates a basic neural network from scratch in Python using NumPy and SciPy, defining the network class and methods for initialization, running, and training.

Uploaded by

Mohamed Aymen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is a Neural Network?

A neural network is loosely based on how the human brain works: many neurons

connected to other neurons, passing information through their connections and firing

when the input to a neuron surpasses a certain threshold. Our artificial neural network

will consist of artificial neurons and synapses with information being passed between
them. The synapses, or connections, will be weighted according to the neuron’s strength

of influence on determining the output. These synaptic weights will go through an

optimization process called backpropagation. For each iteration during the training

process, backpropagation will be used to go back through the layers of the network and

adjusts the weights according to their contribution to the neural net’s error.

Neural networks are essentially self-optimizing functions that map inputs to the correct

outputs. We can then place a new input into the function, where it will predict an output

based on the function it created with the training data.


Neural Net’s Goal

This neural network, like all neural networks, will have to learn what the important

features are in the data to produce the output. In particular, this neural net will be given

an input matrix with six samples, each with three feature columns consisting of solely

zeros and ones. For example, one sample in the training set may be [0, 1, 1]. The output

to each sample will be a single one or zero. The output will be determined by the number
in the first feature column of the data samples. Using the example given before, the

output for [0, 1, 1] would be 0, because the first column contains a zero. An example

chart will be given below to demonstrate the output for each input sample.

How To Create a Neural Network In Python

No matter which method you choose, working with a neural network to make a
prediction is essentially the same:
1. Import the libraries. For example: import numpy as np
2. Define/create input data. For example, use numpy to create a dataset and an
array of data values.
3. Add weights and bias (if applicable) to input features. These are learnable
parameters, meaning that they can be adjusted during training.
 Weights = input parameters that influences output
 Bias = an extra threshold value added to the output
4. Train the network against known, good data in order to find the correct values
for the weights and biases.
5. Test the Network against a set of test data to see how it performs.
6. Fit the model with hyperparameters (parameters whose values are used to
control the learning process), calculate accuracy, and make a prediction.
Create a Neural Network from Scratch
In this example, I’ll use Python code and the numpy and scipy libraries to create a
simple neural network with two nodes.
# Import python libraries required in this example:

import numpy as np

from scipy.special import expit as activation_function

from scipy.stats import truncnorm

# DEFINE THE NETWORK

# Generate random numbers within a truncated (bounded)

# normal distribution:

def truncated_normal(mean=0, sd=1, low=0, upp=10):

return truncnorm(

(low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)

# Create the ‘Nnetwork’ class and define its arguments:

# Set the number of neurons/nodes for each layer

# and initialize the weight matrices:

class Nnetwork:

def __init__(self,

no_of_in_nodes,

no_of_out_nodes,

no_of_hidden_nodes,

learning_rate):

self.no_of_in_nodes = no_of_in_nodes

self.no_of_out_nodes = no_of_out_nodes

self.no_of_hidden_nodes = no_of_hidden_nodes

self.learning_rate = learning_rate

self.create_weight_matrices()

def create_weight_matrices(self):

""" A method to initialize the weight matrices of the neural network"""

rad = 1 / np.sqrt(self.no_of_in_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)

self.weights_in_hidden = X.rvs((self.no_of_hidden_nodes,

self.no_of_in_nodes))

rad = 1 / np.sqrt(self.no_of_hidden_nodes)

X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)

self.weights_hidden_out = X.rvs((self.no_of_out_nodes,

self.no_of_hidden_nodes))

def train(self, input_vector, target_vector):

pass # More work is needed to train the network

def run(self, input_vector):

"""

running the network with an input vector 'input_vector'.

'input_vector' can be tuple, list or ndarray

"""

# Turn the input vector into a column vector:

input_vector = np.array(input_vector, ndmin=2).T

# activation_function() implements the expit function,

# which is an implementation of the sigmoid function:

input_hidden = activation_function(self.weights_in_hidden @ input_vector)

output_vector = activation_function(self.weights_hidden_out @ input_hidden)

return output_vector

# RUN THE NETWORK AND GET A RESULT

# Initialize an instance of the class:

simple_network = Nnetwork(no_of_in_nodes=2,

no_of_out_nodes=2,

no_of_hidden_nodes=4,

learning_rate=0.6)

# Run simple_network for arrays, lists and tuples with shape (2):

# and get a result:

simple_network.run([(3, 4)])
neural
network

mohamed aymen safy

‫محمد ايمن صافي حسن‬

You might also like