Code
source("../dsan-globals/_globals.r")
set.seed(5300)
DSAN 5300: Statistical Learning
Spring 2025, Georgetown University
Today’s Planned Schedule:
Start | End | Topic | |
---|---|---|---|
Lecture | 6:30pm | 7:00pm | Single Layer Neural Networks → |
7:00pm | 7:20pm | Max-Margin Classifiers → | |
7:20pm | 8:00pm | Support Vector Classifiers → | |
Break! | 8:00pm | 8:10pm | |
8:10pm | 9:00pm | Fancier Neural Networks → |
source("../dsan-globals/_globals.r")
set.seed(5300)
library(tidyverse) |> suppressPackageStartupMessages()
library(latex2exp) |> suppressPackageStartupMessages()
<- tribble(
xor_df ~x1, ~x2, ~label,
0, 0, 0,
0, 1, 1,
1, 0, 1,
1, 1, 0
|>
) mutate(
h1 = (x1 - x2)^2,
label = factor(label)
)|> ggplot(aes(x=x1, y=x2, label=label)) +
xor_df geom_point(
aes(color=label, shape=label),
size=g_pointsize * 2,
stroke=6
+
) geom_point(aes(fill=label), color='black', shape=21, size=g_pointsize * 2.5, stroke=0.75, alpha=0.4) +
scale_x_continuous(breaks=c(0, 1)) +
scale_y_continuous(breaks=c(0, 1)) +
expand_limits(y=c(-0.1,1.1)) +
# 45 is minus sign, 95 is em-dash
scale_shape_manual(values=c(95, 43)) +
theme_dsan(base_size=32) +
remove_legend_title() +
labs(
x=TeX("$x_1$"),
y=TeX("$x_2$"),
title="XOR Problem: Original Features"
)
library(tidyverse)
<- tribble(
xor_df ~x1, ~x2, ~label,
0, 0, 0,
0, 1, 1,
1, 0, 1,
1, 1, 0
|>
) mutate(
h1 = (x1 - x2)^2,
h2 = (x1 + x2)^2,
h2 = ifelse(h1 > 0.5 & x2==0, h2 + 0.5, h2),
label = factor(label)
)|> ggplot(aes(x=h1, y=h2, label=label)) +
xor_df geom_vline(xintercept=0.5, linetype="dashed", linewidth=1) +
# Negative space
geom_rect(xmin=-Inf, xmax=0.5, ymin=-Inf, ymax=Inf, fill=cb_palette[1], alpha=0.15) +
# Positive space
geom_rect(xmin=0.5, xmax=Inf, ymin=-Inf, ymax=Inf, fill=cb_palette[2], alpha=0.15) +
geom_point(
aes(color=label, shape=label),
size=g_pointsize * 2,
stroke=6
+
) geom_point(aes(fill=label), color='black', shape=21, size=g_pointsize*2.5, stroke=0.75, alpha=0.4) +
expand_limits(y=c(-0.2,4.2)) +
# 45 is minus sign, 95 is em-dash
scale_shape_manual(values=c(95, 43)) +
theme_dsan(base_size=32) +
remove_legend_title() +
labs(
title="NN-Learned Feature Space",
x=TeX("$h_1(x_1, x_2)$"),
y=TeX("$h_2(x_1, x_2)$")
)
library(tidyverse)
<- seq(from=0, to=1, by=0.0075)
x1_vals <- seq(from=0, to=1, by=0.0075)
x2_vals <- expand.grid(x1=x1_vals, x2=x2_vals) |>
grid_df as_tibble() |>
mutate(
label=factor(as.numeric((x1-x2)^2 > 0.5))
)ggplot() +
geom_point(
data=grid_df,
aes(x=x1, y=x2, color=label),
alpha=0.4
+
) geom_point(
data=xor_df,
aes(x=x1, y=x2, color=label, shape=label),
size=g_pointsize * 2,
stroke=6
+
) geom_point(
data=xor_df,
aes(x=x1, y=x2, fill=label),
color='black', shape=21, size=g_pointsize*2.5, stroke=0.75, alpha=0.4
+
) geom_abline(slope=1, intercept=0.7, linetype="dashed", linewidth=1) +
geom_abline(slope=1, intercept=-0.7, linetype="dashed", linewidth=1) +
scale_shape_manual(values=c(95, 43)) +
theme_dsan(base_size=32) +
remove_legend_title() +
labs(
title="XOR Problem: Inverted NN Features",
x=TeX("$X_1$"), y=TeX("$X_2$")
)
…More next week, tbh
(Full NN playlist here)