Skip to content
Snippets Groups Projects
Commit 3571d338 authored by Felix Ramnelöv's avatar Felix Ramnelöv
Browse files

Lab 2: Fixed figure for assignment 3

parent ea966921
No related branches found
No related tags found
No related merge requests found
......@@ -98,47 +98,22 @@ test_errors <- c()
cost_function <- function(theta) {
train_error <- MSE(y_train, as.matrix(X_train) %*% theta)
return(train_error)
}
theta <- rep(0, ncol(X_train))
# res <- optim(
# par = theta,
# fn = cost_function,
# method = "BFGS",
# control = list(trace = 1, maxit = 200, REPORT = 1),
# )
for (i in 1:5000) {
print(i)
res <- optim(
par = theta,
fn = cost_function,
method = "BFGS",
control = list(maxit = 1),
)
print(res$value)
theta <- res$par
test_error <- MSE(y_test, as.matrix(X_test) %*% theta)
train_errors <<- c(train_errors, res$value)
test_errors <<- c(test_errors, test_error)
if (res$convergence == 0) {
break
}
return(train_error)
}
res$counts
theta <- rep(0, ncol(X_train))
range <- c(train_errors[501:length(train_errors)], test_errors[501:length(test_errors)])
res <- optim(
par = theta,
fn = cost_function,
method = "BFGS",
control = list(trace = 1, maxit = 200),
)
plot(
train_errors,
......@@ -147,21 +122,18 @@ plot(
ylab = "Error",
xlab = "Iteration",
main = "Training and Test Errors over Iterations",
lty = 1,
ylim = c(min(range), max(range)),
xlim = c(501, length(train_errors)),
ylim = c(0.25, 0.75),
xlim = c(501, 10000),
)
points(test_errors,
type = "l",
col = "red",
lty = 2)
points(test_errors, type = "l", col = "red", )
legend(
"topright",
legend = c("Training Error", "Test Error"),
col = c("blue", "red"),
lty = c(1, 2)
legend = c("Training Error", "Test Error", "Optimal Test Error"),
lty = c(1, 1, 0),
col = c("blue", "red", "green"),
pch = c(NA, NA, 19),
)
early_stopping <- which.min(test_errors)
......@@ -169,6 +141,11 @@ early_stopping
test_errors[early_stopping]
points(early_stopping,
test_errors[early_stopping],
col = "green",
pch = 19)
optimal_theta <- res$par
optimal_train_mse <- res$value
optimal_train_mse
......
This diff is collapsed.
lab2/figures/assignment3-4.png

4.78 KiB | W: | H:

lab2/figures/assignment3-4.png

4.79 KiB | W: | H:

lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment