CoolFace
Apppublic

Sreevidya25/Deep_Learning

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
Perceptron.py46 linesDownload Raw Back to root
1import numpy as np2from tqdm import tqdm3 4 5class Perceptron:6    7    def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):8        self.bias = 09        self.learning_rate = learning_rate10        self.max_epochs = epochs11        self.activation_function = activation_function12 13 14    def activate(self, x):15        if self.activation_function == 'step':16            return 1 if x >= 0 else 017        elif self.activation_function == 'sigmoid':18            return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 019        elif self.activation_function == 'relu':20            return 1 if max(0,x)>=0.5 else 021 22    def fit(self, X, y):23        n_features = X.shape[1]24        self.weights = np.random.randint(n_features, size=(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        print("Training Completed")32 33    def predict(self, X):34        predictions = []35        for i in range(len(X)):36            inputs = X[i]37            weighted_sum = np.dot(inputs, self.weights) + self.bias38            prediction = self.activate(weighted_sum)39            predictions.append(prediction)40        return predictions41 42 43    44    45    46