CoolFace
Apppublic

anandhu-pk/Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
BackPropogation.py53 linesDownload Raw Back to root
1import numpy as np2from tqdm import tqdm3 4 5class BackPropogation:6    def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):7        self.bias = 08        self.learning_rate = learning_rate9        self.max_epochs = epochs10        self.activation_function = activation_function11 12 13    def activate(self, x):14        if self.activation_function == 'step':15            return 1 if x >= 0 else 016        elif self.activation_function == 'sigmoid':17            return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 018        elif self.activation_function == 'relu':19            return 1 if max(0,x)>=0.5 else 020 21    def fit(self, X, y):22        error_sum=023        n_features = X.shape[1]24        self.weights = np.zeros((n_features))25        for epoch in tqdm(range(self.max_epochs)):26            for i in range(len(X)):27                inputs = X[i]28                target = y[i]29                weighted_sum = np.dot(inputs, self.weights) + self.bias30                prediction = self.activate(weighted_sum)31                32                # Calculating loss and updating weights.33                error = target - prediction34                self.weights += self.learning_rate * error * inputs35                self.bias += self.learning_rate * error36                37            print(f"Updated Weights after epoch {epoch} with {self.weights}")38        print("Training Completed")39 40    def predict(self, X):41        predictions = []42        for i in range(len(X)):43            inputs = X[i]44            weighted_sum = np.dot(inputs, self.weights) + self.bias45            prediction = self.activate(weighted_sum)46            predictions.append(prediction)47        return predictions48 49 50    51    52    53