Tuesday, 6 June 2023

Cannot implement Resilient Backpropagation (RPROP)

I would like to implement the Resilient propagation (RProp) to this code, how could I do it? Actually, this code is working fine, but I can't find a way to add the Resilient propagation algorithm in this.

If someone can help me with this, I'm having an hard time figuring this out and I can't find a way out. Thanks in advance to everyone.

import numpy as np
from keras.datasets import mnist

class NeuralNetwork:
    def __init__(self, input_size, hidden_sizes, output_size):
        self.input_size = input_size
        self.hidden_sizes = hidden_sizes
        self.output_size = output_size
        self.weights = []
        self.biases = []
        self.activations = []
        self.deltas = []

        # Initialize weights and biases for each layer
        sizes = [input_size] + hidden_sizes + [output_size]
        for i in range(len(sizes) - 1):
            self.weights.append(np.random.randn(sizes[i], sizes[i+1]))
            self.biases.append(np.zeros((1, sizes[i+1])))

        # Initialize activations and deltas for each layer
        for i in range(len(sizes)):
            self.activations.append(np.zeros((1, sizes[i])))
            self.deltas.append(np.zeros((1, sizes[i])))

    def forward_propagation(self, x):
        self.activations[0] = x
        for i in range(len(self.weights)):
            weighted_sum = np.dot(self.activations[i], self.weights[i]) + self.biases[i]
            self.activations[i+1] = self.sigmoid(weighted_sum)

        return self.activations[-1]

    def backward_propagation(self, x, y, learning_rate):
        output = self.activations[-1]
        target = np.zeros_like(output)
        target[np.arange(len(output)), y] = 1  # Convert target labels to one-hot encoded format
    
        self.deltas[-1] = (output - target) * self.sigmoid_derivative(output)

        for i in range(len(self.weights)-1, -1, -1):
            weight_delta = np.dot(self.activations[i].T, self.deltas[i+1])
            bias_delta = np.sum(self.deltas[i+1], axis=0, keepdims=True)

            self.weights[i] -= learning_rate * weight_delta
            self.biases[i] -= learning_rate * bias_delta

            self.deltas[i] = np.dot(self.deltas[i+1], self.weights[i].T) * self.sigmoid_derivative(self.activations[i])

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return x * (1 - x)

    def train(self, x, y, learning_rate, epochs, batch_size):
        num_samples = x.shape[0]
        num_batches = num_samples // batch_size

        for epoch in range(1, epochs + 1):
            for batch in range(num_batches):
                start = batch * batch_size
                end = start + batch_size
                x_batch = x[start:end]
                y_batch = y[start:end]

                self.forward_propagation(x_batch)
                self.backward_propagation(x_batch, y_batch, learning_rate)

            if epoch % 10 == 0:
                print(f"Epoch {epoch}/{epochs} completed.")

    def predict(self, x):
        return np.argmax(self.forward_propagation(x), axis=1)
    
    def error_entropy_with_softmax(self, predictions, targets):
        # Apply softmax to predictions
        softmax_preds = np.exp(predictions) / np.sum(np.exp(predictions), axis=1, keepdims=True)
    
        # Clip the softmax predictions to avoid numerical instability
        epsilon = 1e-10
        softmax_preds = np.clip(softmax_preds, epsilon, 1.0 - epsilon)
    
        # Convert targets to one-hot encoded format
        num_samples = predictions.shape[0]
        target = np.zeros_like(softmax_preds)
        target[np.arange(num_samples), targets] = 1
    
        # Calculate the error entropy
        error_entropy = -np.sum(target * np.log(softmax_preds)) / num_samples
    
        return error_entropy



def main():
    # Load the MNIST dataset
    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    # Normalize pixel values to range between 0 and 1
    x_train = x_train.astype('float32') / 255.0
    x_test = x_test.astype('float32') / 255.0

    # Flatten the images into a 1D array
    x_train = x_train.reshape(x_train.shape[0], -1)
    x_test = x_test.reshape(x_test.shape[0], -1)

    # Split the dataset into training, validation, and test sets
    train_size = 5000
    val_size = 2500
    test_size = 2500

    x_val = x_train[train_size:train_size + val_size]
    y_val = y_train[train_size:train_size + val_size]

    x_train = x_train[:train_size]
    y_train = y_train[:train_size]

    x_test = x_test[:test_size]
    y_test = y_test[:test_size]

    # Define the neural network architecture
    input_size = x_train.shape[1]
    hidden_sizes = [128, 64, 32]
    output_size = 10

    # Create an instance of the NeuralNetwork class
    network = NeuralNetwork(input_size, hidden_sizes, output_size)

    # Train the neural network
    learning_rate = 0.01
    epochs = 100
    batch_size = 32

    for epoch in range(1, epochs + 1):
        network.train(x_train, y_train, learning_rate, 1, batch_size)
        if epoch % 10 == 0:
            print(f"Epoch {epoch}/{epochs} completed.")

            # Calculate the error on the training set
            train_predictions = network.forward_propagation(x_train)
            train_error = network.error_entropy_with_softmax(train_predictions, y_train)
            print(f"Training Error with softmax: {train_error:.4f}")

            # Calculate the error on the validation set
            val_predictions = network.forward_propagation(x_val)
            val_error = network.error_entropy_with_softmax(val_predictions, y_val)
            print(f"Validation Error with softmax: {val_error:.4f}")

            # Test the neural network
            predictions = network.predict(x_test)
            accuracy = np.mean(predictions == y_test) * 100
            print(f"Test Accuracy: {accuracy:.2f}%\n")

    


if __name__ == '__main__':
    main()


from Cannot implement Resilient Backpropagation (RPROP)

No comments:

Post a Comment