Neural Networks: in C Script
This documentation provides a comprehensive guide for using two Python scripts: neural_network.py for training a neural network model, and run_inference.py for running inference with a trained model.
git clone https://github.com/mdriyadkhan585/neural_network_pythonrequirements.txt
This file lists all the Python packages your project depends on. You can create it with the following content:
numpy
After cloning the repository, navigate into the project directory and install the dependencies listed in requirements.txt:
cd neural_network_python
pip install -r requirements.txtneural_network.py is responsible for defining, training, and saving a neural network model. This script includes:
- Model architecture and activation functions
 - Weight initialization
 - Forward pass and backpropagation algorithms
 - Training loop with loss calculation and early stopping
 - Model saving
 
- 
initialize_weights(size)- Purpose: Initializes weights for the neural network layers.
 - Input: 
size(int) - Number of weights to initialize. - Output: 
np.ndarray- Array of initialized weights. 
 - 
relu(x)- Purpose: Applies the ReLU activation function.
 - Input: 
x(float) - Input value. - Output: 
float- Activated value. 
 - 
elu(x)- Purpose: Applies the ELU activation function.
 - Input: 
x(float) - Input value. - Output: 
float- Activated value. 
 - 
sigmoid(x)- Purpose: Applies the sigmoid activation function.
 - Input: 
x(float) - Input value. - Output: 
float- Activated value. 
 - 
swish(x)- Purpose: Applies the Swish activation function.
 - Input: 
x(float) - Input value. - Output: 
float- Activated value. 
 - 
softmax(output)- Purpose: Applies the softmax function to the output layer.
 - Input: 
output(np.ndarray) - Array of logits. - Output: 
np.ndarray- Softmax probabilities. 
 
forward_pass(input, weights_hidden1, weights_hidden2, weights_hidden3, weights_output, biases_hidden1, biases_hidden2, biases_hidden3, biases_output)- Purpose: Performs the forward pass of the network.
 - Inputs:
input(np.ndarray) - Input data.weights_hidden1,weights_hidden2,weights_hidden3,weights_output(np.ndarray) - Weights for each layer.biases_hidden1,biases_hidden2,biases_hidden3,biases_output(np.ndarray) - Biases for each layer.
 - Outputs:
hidden1_output,hidden2_output,hidden3_output,final_output(np.ndarray) - Output of each layer.
 
backpropagate(input, hidden1_output, hidden2_output, hidden3_output, final_output, actual_output, weights_hidden1, weights_hidden2, weights_hidden3, weights_output, biases_hidden1, biases_hidden2, biases_hidden3, biases_output, learning_rate)- Purpose: Updates weights and biases through backpropagation.
 - Inputs:
input,hidden1_output,hidden2_output,hidden3_output,final_output(np.ndarray) - Layer outputs.actual_output(np.ndarray) - True output values.weights_hidden1,weights_hidden2,weights_hidden3,weights_output(np.ndarray) - Weights to be updated.biases_hidden1,biases_hidden2,biases_hidden3,biases_output(np.ndarray) - Biases to be updated.learning_rate(float) - Learning rate for updates.
 - Output: None.
 
    train(inputs, targets, num_samples)- Purpose: Trains the neural network model.
 - Inputs:
inputs(np.ndarray) - Input data.targets(np.ndarray) - Target output data.num_samples(int) - Number of training samples.
 - Output: None (Trains and saves the model).
 
- Prepare Data: Organize your input data and target labels.
 - Configure Parameters: Adjust the hyperparameters and model architecture if needed.
 - Run Training:
This will train the model on the provided data and save it to
python neural_network.py
trained_model.pkl. 
Here's an example of the data preparation and training:
import numpy as np
# Sample data
inputs = np.random.rand(10, INPUT_SIZE)  # Replace with actual data
targets = np.eye(OUTPUT_SIZE)[np.random.randint(0, OUTPUT_SIZE, 10)]  # Example one-hot encoded targets
# Train the model
train(inputs, targets, len(inputs))run_inference.py is used to load a pre-trained model and perform inference on new data. This script includes:
- Model loading from a file
 - Forward pass for inference
 
load_model(weights_hidden1, weights_hidden2, weights_hidden3, weights_output, biases_hidden1, biases_hidden2, biases_hidden3, biases_output)- Purpose: Loads model weights and biases from a file.
 - Inputs:
weights_hidden1,weights_hidden2,weights_hidden3,weights_output(np.ndarray) - Arrays to store weights.biases_hidden1,biases_hidden2,biases_hidden3,biases_output(np.ndarray) - Arrays to store biases.
 - Output: None.
 
forward_pass(input, weights_hidden1, weights_hidden2, weights_hidden3, weights_output, biases_hidden1, biases_hidden2, biases_hidden3, biases_output, hidden1_output, hidden2_output, hidden3_output, final_output)- Purpose: Performs inference by running a forward pass with the provided input.
 - Inputs:
input(np.ndarray) - Input data for inference.weights_hidden1,weights_hidden2,weights_hidden3,weights_output(np.ndarray) - Loaded weights.biases_hidden1,biases_hidden2,biases_hidden3,biases_output(np.ndarray) - Loaded biases.
 - Outputs:
hidden1_output,hidden2_output,hidden3_output,final_output(np.ndarray) - Output of each layer.
 
- 
Load the Model: Ensure that
trained_model.pklis available in the same directory. - 
Run Inference:
python run_inference.py
 
Here’s how to use the run_inference.py script to perform inference:
import numpy as np
# Load the trained model and perform inference
test_input = np.random.rand(INPUT_SIZE)  # Replace with actual test data
forward_pass(test_input, weights_hidden1, weights_hidden2, weights_hidden3, weights_output,
             biases_hidden1, biases_hidden2, biases_hidden3, biases_output,
             hidden1_output, hidden2_output, hidden3_output, final_output)
print("Inference result:")
for i in range(OUTPUT_SIZE):
    print(f"Class {i}: {final_output[i]:.4f}")neural_network.py: For training the model and saving it.run_inference.py: For loading the trained model and running inference.
Ensure that all dependencies (e.g., NumPy) are installed and the model file (trained_model.pkl) is available when running inference.