From c8567ae6962095301194e9adb3a666bd3633f7e6 Mon Sep 17 00:00:00 2001 From: shreyakash24 Date: Wed, 15 Oct 2025 18:31:43 +0530 Subject: [PATCH 1/2] added rnn code --- neural_network/recurrent_neural_network.py | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 neural_network/recurrent_neural_network.py diff --git a/neural_network/recurrent_neural_network.py b/neural_network/recurrent_neural_network.py new file mode 100644 index 000000000000..61587aa9c0be --- /dev/null +++ b/neural_network/recurrent_neural_network.py @@ -0,0 +1,95 @@ +""" +Minimal Recurrent Neural Network (RNN) demonstration. + +Forward propagation explanation: +https://towardsdatascience.com/forward-propagation-in-neural-networks-simplified-math-and-code-version-bbcfef6f9250 +RNN fundamentals: +https://towardsdatascience.com/recurrent-neural-networks-d4642c9bc7ce/ +""" + +import math +import random + + +# Sigmoid activation +def sigmoid_function(value: float, deriv: bool = False) -> float: + """Return the sigmoid function of a float. + + >>> round(sigmoid_function(3.5), 4) + 0.9707 + >>> round(sigmoid_function(0.5, True), 4) + 0.25 + """ + if deriv: + return value * (1 - value) + return 1 / (1 + math.exp(-value)) + + +# Initial constants +INITIAL_VALUE = 0.02 # learning rate +SEQUENCE_LENGTH = 5 # time steps in the sequence + + +def forward_propagation_rnn(expected: int, number_propagations: int) -> float: + """Return the value found after RNN forward propagation training. + + >>> res = forward_propagation_rnn(50, 500_000) + >>> res > 45 and res < 55 + True + + >>> res = forward_propagation_rnn(50, 500) + >>> res > 48 and res < 50 + True + """ + random.seed(0) + + # Random weight initialization + W_xh = (random.random() * 2 - 1) # Input to hidden + W_hh = (random.random() * 2 - 1) # Hidden to hidden (recurrent) + W_hy = (random.random() * 2 - 1) # Hidden to output + + # Training loop + for _ in range(number_propagations): + h_prev = 0.0 # hidden state starts at zero + total_error = 0.0 + + # Forward pass through time + for t in range(SEQUENCE_LENGTH): + # Fake input sequence: small constant or could be pattern-based + x_t = INITIAL_VALUE + + # Hidden state update + h_t = sigmoid_function(W_xh * x_t + W_hh * h_prev) + + # Output + y_t = sigmoid_function(W_hy * h_t) + + # Error (target distributed over time steps) + error_t = (expected / 100) - y_t + total_error += abs(error_t) + + # Backpropagation Through Time (simplified) + d_y = error_t * sigmoid_function(y_t, True) + d_h = d_y * W_hy * sigmoid_function(h_t, True) + + # Weight updates + W_hy += INITIAL_VALUE * d_y * h_t + W_xh += INITIAL_VALUE * d_h * x_t + W_hh += INITIAL_VALUE * d_h * h_prev + + # Move to next time step + h_prev = h_t + + # Final output after training + final_output = y_t * 100 + return final_output + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + expected = int(input("Expected value: ")) + number_propagations = int(input("Number of propagations: ")) + print(forward_propagation_rnn(expected, number_propagations)) From 73878629ff26ba22dbaad2670cb4051624ce09c9 Mon Sep 17 00:00:00 2001 From: shreyakash24 Date: Wed, 15 Oct 2025 20:27:36 +0530 Subject: [PATCH 2/2] added readme for divide and conquer folder --- divide_and_conquer/README.md | 23 ++++++ neural_network/recurrent_neural_network.py | 95 ---------------------- 2 files changed, 23 insertions(+), 95 deletions(-) create mode 100644 divide_and_conquer/README.md delete mode 100644 neural_network/recurrent_neural_network.py diff --git a/divide_and_conquer/README.md b/divide_and_conquer/README.md new file mode 100644 index 000000000000..de17760c3fdc --- /dev/null +++ b/divide_and_conquer/README.md @@ -0,0 +1,23 @@ +# Divide and Conquer Algorithms Folder + +This folder contains implementations of **Divide and Conquer algorithms**, which solve problems by: + +1. Dividing the problem into smaller subproblems +2. Solving each subproblem recursively +3. Combining the results to get the final solution + +To ensure high-quality contributions, each algorithm should follow the guidelines in `CONTRIBUTING.md`: + +- Recursive divide-and-conquer logic +- Descriptive function and class names +- Unit tests demonstrating correctness +- Type hints for all functions +- Clear code organization + + +### Notes for Contributors + +- Focus on **divide-and-conquer implementation** +- Include **unit tests** for typical and edge cases +- Keep **functions and classes well-named and typed** +- Maintain **folder structure and file naming consistency** diff --git a/neural_network/recurrent_neural_network.py b/neural_network/recurrent_neural_network.py deleted file mode 100644 index 61587aa9c0be..000000000000 --- a/neural_network/recurrent_neural_network.py +++ /dev/null @@ -1,95 +0,0 @@ -""" -Minimal Recurrent Neural Network (RNN) demonstration. - -Forward propagation explanation: -https://towardsdatascience.com/forward-propagation-in-neural-networks-simplified-math-and-code-version-bbcfef6f9250 -RNN fundamentals: -https://towardsdatascience.com/recurrent-neural-networks-d4642c9bc7ce/ -""" - -import math -import random - - -# Sigmoid activation -def sigmoid_function(value: float, deriv: bool = False) -> float: - """Return the sigmoid function of a float. - - >>> round(sigmoid_function(3.5), 4) - 0.9707 - >>> round(sigmoid_function(0.5, True), 4) - 0.25 - """ - if deriv: - return value * (1 - value) - return 1 / (1 + math.exp(-value)) - - -# Initial constants -INITIAL_VALUE = 0.02 # learning rate -SEQUENCE_LENGTH = 5 # time steps in the sequence - - -def forward_propagation_rnn(expected: int, number_propagations: int) -> float: - """Return the value found after RNN forward propagation training. - - >>> res = forward_propagation_rnn(50, 500_000) - >>> res > 45 and res < 55 - True - - >>> res = forward_propagation_rnn(50, 500) - >>> res > 48 and res < 50 - True - """ - random.seed(0) - - # Random weight initialization - W_xh = (random.random() * 2 - 1) # Input to hidden - W_hh = (random.random() * 2 - 1) # Hidden to hidden (recurrent) - W_hy = (random.random() * 2 - 1) # Hidden to output - - # Training loop - for _ in range(number_propagations): - h_prev = 0.0 # hidden state starts at zero - total_error = 0.0 - - # Forward pass through time - for t in range(SEQUENCE_LENGTH): - # Fake input sequence: small constant or could be pattern-based - x_t = INITIAL_VALUE - - # Hidden state update - h_t = sigmoid_function(W_xh * x_t + W_hh * h_prev) - - # Output - y_t = sigmoid_function(W_hy * h_t) - - # Error (target distributed over time steps) - error_t = (expected / 100) - y_t - total_error += abs(error_t) - - # Backpropagation Through Time (simplified) - d_y = error_t * sigmoid_function(y_t, True) - d_h = d_y * W_hy * sigmoid_function(h_t, True) - - # Weight updates - W_hy += INITIAL_VALUE * d_y * h_t - W_xh += INITIAL_VALUE * d_h * x_t - W_hh += INITIAL_VALUE * d_h * h_prev - - # Move to next time step - h_prev = h_t - - # Final output after training - final_output = y_t * 100 - return final_output - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - - expected = int(input("Expected value: ")) - number_propagations = int(input("Number of propagations: ")) - print(forward_propagation_rnn(expected, number_propagations))