Edit model card

Priyo_Neuralnetwork

class Priyo_NeuralNetwork:

def __init__(self, input_size, hidden_size, output_size):
    # Inisialisasi bobot dan bias secara acak
    self.weights1 = np.random.randn(input_size, hidden_size)
    self.bias1 = np.zeros(hidden_size)
    self.weights2 = np.random.randn(hidden_size, output_size)
    self.bias2 = np.zeros(output_size)

def sigmoid(self, x):
    clipped_x = np.clip(x, -5, 5)  # Clip values between -5 and 5
    return 1 / (1 + np.exp(-clipped_x))

def forward(self, X):
    # Perhitungan forward propagation
    self.z1 = np.dot(X, self.weights1) + self.bias1
    self.a1 = self.sigmoid(self.z1)
    self.z2 = np.dot(self.a1, self.weights2) + self.bias2
    self.a2 = self.sigmoid(self.z2)
    return self.a2

def backward(self, X, y, learning_rate):
    m = X.shape[0]
    dZ2 = self.a2 - y
    dW2 = 1/m * np.dot(self.a1.T, dZ2)
    db2 = 1/m * np.sum(dZ2, axis=0)
    dZ1 = np.dot(dZ2, self.weights2.T) * (1 - self.a1) * self.a1
    dW1 = 1/m * np.dot(X.T, dZ1)
    db1 = 1/m * np.sum(dZ1, axis=0)

    return dW1, db1, dW2, db2

def train(self, X, y, epochs, learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-8):
    m = X.shape[0]

    # Initialize moments for Adam
    v_dw1, v_db1, v_dw2, v_db2 = np.zeros_like(self.weights1), np.zeros_like(self.bias1), np.zeros_like(self.weights2), np.zeros_like(self.bias2)
    s_dw1, s_db1, s_dw2, s_db2 = np.zeros_like(self.weights1), np.zeros_like(self.bias1), np.zeros_like(self.weights2), np.zeros_like(self.bias2)
    t = 0

    for epoch in range(epochs):
        self.forward(X)
        dW1, db1, dW2, db2 = self.backward(X, y, learning_rate)

        # Update weights and biases using Adam
        t += 1
        
        # Update biased first moment estimate
        v_dw1 = beta1 * v_dw1 + (1 - beta1) * dW1
        v_db1 = beta1 * v_db1 + (1 - beta1) * db1
        v_dw2 = beta1 * v_dw2 + (1 - beta1) * dW2
        v_db2 = beta1 * v_db2 + (1 - beta1) * db2

        # Update biased second raw moment estimate
        s_dw1 = beta2 * s_dw1 + (1 - beta2) * np.square(dW1)
        s_db1 = beta2 * s_db1 + (1 - beta2) * np.square(db1)
        s_dw2 = beta2 * s_dw2 + (1 - beta2) * np.square(dW2)
        s_db2 = beta2 * s_db2 + (1 - beta2) * np.square(db2)
        
        # Compute bias-corrected first moment estimate
        v_dw1_corrected = v_dw1 / (1 - beta1**t)
        v_db1_corrected = v_db1 / (1 - beta1**t)
        v_dw2_corrected = v_dw2 / (1 - beta1**t)
        v_db2_corrected = v_db2 / (1 - beta1**t)
        
        # Compute bias-corrected second raw moment estimate
        s_dw1_corrected = s_dw1 / (1 - beta2**t)
        s_db1_corrected = s_db1 / (1 - beta2**t)
        s_dw2_corrected = s_dw2 / (1 - beta2**t)
        s_db2_corrected = s_db2 / (1 - beta2**t)
        
        # Update weights and biases
        self.weights1 -= learning_rate * v_dw1_corrected / (np.sqrt(s_dw1_corrected) + epsilon)
        self.bias1 -= learning_rate * v_db1_corrected / (np.sqrt(s_db1_corrected) + epsilon)
        self.weights2 -= learning_rate * v_dw2_corrected / (np.sqrt(s_dw2_corrected) + epsilon)
        self.bias2 -= learning_rate * v_db2_corrected / (np.sqrt(s_db2_corrected) + epsilon)

        if (epoch+1) % 100 == 0:
            print(f'Epoch {epoch+1}/{epochs}, loss: {self.loss(y, self.a2)}')

def loss(self, y_true, y_pred):
    # Fungsi loss (misalnya binary cross-entropy)
    return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))

def predict(self, X):
    # Prediksi menggunakan forward propagation
    y_pred = self.forward(X)
    # Rounding untuk klasifikasi biner
    y_pred = np.round(y_pred)
    return y_pred

def accuracy(self, X, y):
    # Hitung akurasi
    y_pred = self.predict(X)
    accuracy = np.mean(y_pred == y)
    return accuracy
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference API
Unable to determine this model's library. Check the docs .

Dataset used to train priyovamr/Priyo_Neuralnetwork