Sztuczne_sieci_perceptron

Majkowska Agata

biblioteki

library(ggplot2)

PRZYGOTOAWANIE DANYCH

set.seed(123)

n <- 100

# Klasa 0
x1_0 <- rnorm(n, mean = 0)
x2_0 <- rnorm(n, mean = 0)

# Klasa 1
x1_1 <- rnorm(n, mean = 3)
x2_1 <- rnorm(n, mean = 3)

# Dane
X <- rbind(
  cbind(x1_0, x2_0),
  cbind(x1_1, x2_1)
)

y <- c(rep(0, n), rep(1, n))

data <- data.frame(x1 = X[,1], x2 = X[,2], y = y)

data$y <- as.factor(data$y)

WIZUALIZACJA DANYCH

ggplot(data, aes(x = x1, y = x2, color = y)) +
  geom_point(size = 2) +
  scale_color_manual(values = c("blue", "red"),
                     labels = c("Klasa 0", "Klasa 1")) +
  labs(
    title = "Dane do klasyfikacji",
    x = "x1",
    y = "x2",
    color = "Klasa"
  ) +
  theme_minimal()

TWORZENIE FUNKCJI AKTYWACJI

# KLASYCZNY PERCEPTRON FUNKCJA PROGOWA
activation <- function(z) {
  ifelse(z >= 0, 1, 0)
}

PRZYGOTOWANIE PARAMTERÓW: INICJALIZACJA WAG, LICZBY EPOK I KROKU UCZENIA

set.seed(123)
# runif(n, min, max)
w1 <- runif(1, -1, 1)
w2 <- runif(1, -1, 1)
b  <- runif(1, -1, 1)

learning_rate <- 0.1
epochs <- 20

data$y <- as.integer(y)

FUNKCJA PREDYCKJI

predict_perceptron <- function(x1, x2, w1, w2, b) {
  z <- w1 * x1 + w2 * x2 + b
  activation(z)
}

UCZENIE PERCEPTRONU

errors_history <- c()

for (epoch in 1:epochs) {
  
  total_error <- 0
  
  for (i in 1:nrow(data)) {
    
    x1_i <- data$x1[i]
    x2_i <- data$x2[i]
    y_true <- data$y[i]
    
    # Predykcja
    y_pred <- predict_perceptron(x1_i, x2_i, w1, w2, b)
    
    # Błąd
    error <- y_true - y_pred
    
    # Aktualizacja wag (KLUCZ!)
    w1 <- w1 + learning_rate * error * x1_i
    w2 <- w2 + learning_rate * error * x2_i
    b  <- b  + learning_rate * error
    
    total_error <- total_error + abs(error)
  }
  
  errors_history <- c(errors_history, total_error)
  
  cat("Epoka:", epoch, "Błąd:", total_error, "\n")
}
## Epoka: 1 Błąd: 6 
## Epoka: 2 Błąd: 2 
## Epoka: 3 Błąd: 3 
## Epoka: 4 Błąd: 3 
## Epoka: 5 Błąd: 4 
## Epoka: 6 Błąd: 2 
## Epoka: 7 Błąd: 3 
## Epoka: 8 Błąd: 2 
## Epoka: 9 Błąd: 2 
## Epoka: 10 Błąd: 3 
## Epoka: 11 Błąd: 2 
## Epoka: 12 Błąd: 2 
## Epoka: 13 Błąd: 3 
## Epoka: 14 Błąd: 2 
## Epoka: 15 Błąd: 2 
## Epoka: 16 Błąd: 3 
## Epoka: 17 Błąd: 2 
## Epoka: 18 Błąd: 2 
## Epoka: 19 Błąd: 2 
## Epoka: 20 Błąd: 2

BŁAD UCZENIA

df_errors <- data.frame(
  epoch = 1:length(errors_history),
  error = errors_history
)

ggplot(df_errors, aes(x = epoch, y = error)) +
  geom_line() +
  geom_point() +
  scale_y_continuous(limits = c(0, max(df_errors$error))) + 
  labs(
    title = "Błąd w kolejnych epokach",
    x = "Epoka",
    y = "Błąd"
  ) +
  theme_minimal()

WIZUALIZACJA WYNIKU

data$y <- as.factor(data$y)
# granica decyzji 
# w1*x1 + w2*x2 + b = 0

ggplot(data, aes(x = x1, y = x2, color = y)) +
  geom_point(size = 2) +
  
  # granica decyzyjna
  geom_abline(
    intercept = -b / w2,
    slope = -w1 / w2,
    color = "darkgrey",
    linewidth = 1
  ) +
  
  scale_color_manual(
    values = c("blue", "red"),
    labels = c("Klasa 0", "Klasa 1")
  ) +
  
  labs(
    title = "Perceptron - granica decyzyjna",
    x = "x1",
    y = "x2",
    color = "Klasa"
  ) +
  
  theme_minimal()