10 KiB
10 KiB
In [1]:
import numpy as np
weight_value = np.array(0.5)
expected_value = np.array(0.8)
input = np.array(2)
alpha = 0.1
n = 1
def error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, x):
for i in range(x):
prediction = weight_value * input
delta = (np.outer(((2 / n) * (prediction - expected_value)),
input))
weight_value = weight_value - (alpha * delta)
return prediction, deltaIn [2]:
pred_5, err_5 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 5)
print(f"Wynik dla 5 epok: {pred_5.item():.5f}, Błąd: {err_5.item():.10f}\n", )Wynik dla 5 epok: 0.80032, Błąd: 0.0012800000
In [3]:
pred_20, err_20 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 20)
print(f"Wynik dla 20 epok: {pred_20.item():.5f}, Błąd: {err_20.item():.10f}\n", )Wynik dla 20 epok: 0.80000, Błąd: 0.0000000000
In [4]:
new_alpha = 1
pred_alp_1, err_alp_1 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 5)
print(f"Wynik alpha = 1: {pred_alp_1.item():.5f}, Błąd: {err_alp_1.item():.10f}\n", )
# Nigdy nei osiagnie celu przez zbyt wysoka alphe przez co przeskakujemy wartosc najblizsza.Wynik alpha = 1: 0.80032, Błąd: 0.0012800000
In [5]:
new_input = np.array(0.1)
pred_alp_1, err_alp_1 = error_prediction_n_epoch(weight_value, expected_value, new_input, alpha, n, 20000)
print(f"Wynik input = 0.1: {pred_alp_1.item():.5f}, Błąd: {err_alp_1.item():.10f}\n", )
# Osiagnie cel dopiero po 20k epok.Wynik input = 0.1: 0.80000, Błąd: -0.0000000000
In [6]:
input_data = np.array([
[0.5, 0.1, 0.2, 0.8],
[0.75, 0.3, 0.1, 0.9],
[0.1, 0.7, 0.6, 0.2]
])
expected_results = np.array([
[0.1, 0.5, 0.1, 0.7],
[1.0, 0.2, 0.3, 0.6],
[0.1, -0.5, 0.2, 0.2],
[0.0, 0.3, 0.9, -0.1],
[-0.1, 0.7, 0.1, 0.8]
])
weights = np.array([
[0.1, 0.1, -0.3],
[0.1, 0.2, 0.0],
[0.0, 0.7, 0.1],
[0.2, 0.4, 0.0],
[-0.3, 0.5, 0.1]
])
epochs = 1000
alpha = 0.01
def neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs):
w = weights.copy()
num_series = input_data.shape[1]
num_neurons = expected_results.shape[0]
for epoch in range(epochs):
epoch_error = 0
for i in range(num_series):
x_i = input_data[:, i:i + 1]
y_i = expected_results[:, i:i + 1]
pred_i = np.dot(w, x_i)
error_i = (1 / num_neurons) * np.sum((pred_i - y_i) ** 2)
epoch_error += error_i
delta_i = (2 / num_neurons) * np.dot((pred_i - y_i), x_i.T)
w = w - alpha * delta_i
# print(f"\nEpoch {epoch + 1}:")
# print(f"Serie: {i + 1}:\n")
# print("\nOutput: \n", pred_i)
# print("\nWeights: \n", w)
# print(f"\nError: {error_i:.6f}\n")
return epoch_errorIn [7]:
epoch_err = neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs)In [8]:
print(f"Blad ostatniej epoki: {epoch_err:.6f}\n")Blad ostatniej epoki: 0.258218
In [9]:
w = np.random.uniform(-1, 1, (4, 3))
data = np.loadtxt('training_colors.txt')
input = data[:, :3]
input_info = data[:, 3]
goal = np.zeros((1, 4))
color_map = {
1: [1.0, 0.0, 0.0, 0.0], # Czerwony
2: [0.0, 1.0, 0.0, 0.0], # Zielony
3: [0.0, 0.0, 1.0, 0.0], # Niebieski
4: [0.0, 0.0, 0.0, 1.0], # Żółty
}
goal_list = [color_map[int(label)] for label in input_info]
goal = np.array(goal_list)
goal = goal.T
input = input.T
alpha = 0.01
epochs = 100
def neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs):
w = weights.copy()
num_series = input_data.shape[1]
num_neurons = expected_results.shape[0]
for epoch in range(epochs):
epoch_error = 0
for i in range(num_series):
x_i = input_data[:, i:i + 1]
y_i = expected_results[:, i:i + 1]
pred_i = np.dot(w, x_i)
error_i = (1 / num_neurons) * np.sum((pred_i - y_i) ** 2)
epoch_error += error_i
delta_i = (2 / num_neurons) * np.dot((pred_i - y_i), x_i.T)
w = w - alpha * delta_i
return w
w = neuron_network_learning_per_series(w, input, goal, alpha, epochs)
test_data = np.loadtxt('test_colors.txt')
X_test = test_data[:, :3].T
y_test_info = test_data[:, 3]
predictions_test = np.dot(w, X_test)
y_pred_test = np.argmax(predictions_test, axis=0) + 1
correct_predictions = np.sum(y_pred_test == y_test_info)
total_samples = len(y_test_info)
accuracy = (correct_predictions / total_samples) * 100.0
print(f"Poprawnie rozpoznane na zbiorze testowym: {correct_predictions} / {total_samples}")
print(f"Skuteczność sieci: {accuracy:.2f}%")
Poprawnie rozpoznane na zbiorze testowym: 130 / 130 Skuteczność sieci: 100.00%