Created
August 1, 2023 10:14
-
-
Save galihboy/9c89ea84d2cef4333641824160acc895 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Demo JST -> Backpropagation | |
# Operasi logika AND | |
import numpy as np | |
# Fungsi aktivasi sigmoid | |
def sigmoid(x): | |
return 1 / (1 + np.exp(-x)) | |
# Fungsi turunan dari sigmoid | |
def sigmoid_derivative(x): | |
return x * (1 - x) | |
# Data pelatihan | |
input_data = np.array([[0, 0], | |
[0, 1], | |
[1, 0], | |
[1, 1]]) | |
# Kelas target (output yang diharapkan) | |
output_data = np.array([[0], | |
[0], | |
[0], | |
[1]]) | |
# Inisialisasi bobot dan bias secara acak untuk lapisan tersembunyi dan output | |
np.random.seed(1) | |
hidden_layer_weights = 2 * np.random.random((2, 2)) - 1 | |
output_layer_weights = 2 * np.random.random((2, 1)) - 1 | |
hidden_layer_bias = 2 * np.random.random((1, 2)) - 1 | |
output_layer_bias = 2 * np.random.random((1, 1)) - 1 | |
# Jumlah iterasi pelatihan | |
epochs = 10000 | |
# Tingkat pembelajaran (learning rate) | |
learning_rate = 0.1 | |
# Pelatihan JST | |
for epoch in range(epochs): | |
# Forward propagation | |
hidden_layer_output = sigmoid(np.dot(input_data, hidden_layer_weights) + hidden_layer_bias) | |
output = sigmoid(np.dot(hidden_layer_output, output_layer_weights) + output_layer_bias) | |
# Menghitung galat (error) | |
error = output_data - output | |
# Backpropagation | |
output_delta = error * sigmoid_derivative(output) | |
hidden_layer_error = output_delta.dot(output_layer_weights.T) | |
hidden_layer_delta = hidden_layer_error * sigmoid_derivative(hidden_layer_output) | |
# Update bobot dan bias | |
output_layer_weights += hidden_layer_output.T.dot(output_delta) * learning_rate | |
output_layer_bias += np.sum(output_delta, axis=0, keepdims=True) * learning_rate | |
hidden_layer_weights += input_data.T.dot(hidden_layer_delta) * learning_rate | |
hidden_layer_bias += np.sum(hidden_layer_delta, axis=0, keepdims=True) * learning_rate | |
# Hasil setelah pelatihan | |
print("Hasil setelah pelatihan:") | |
print(output) | |
# Prediksi untuk input baru | |
new_input = np.array([[1, 1]]) | |
hidden_layer_output = sigmoid(np.dot(new_input, hidden_layer_weights) + hidden_layer_bias) | |
new_output = sigmoid(np.dot(hidden_layer_output, output_layer_weights) + output_layer_bias) | |
print("Prediksi untuk input baru [1, 1]:") | |
print(new_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment