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

Lab 3: Code and figure fixes

parent 29fdb5f6
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ h_time <- 4 ...@@ -13,7 +13,7 @@ h_time <- 4
a <- 58.4274 a <- 58.4274
b <- 14.826 b <- 14.826
date <- "1966-12-10" date <- "1983-05-10"
times <- c( times <- c(
"04:00:00", "04:00:00",
"06:00:00", "06:00:00",
......
...@@ -89,8 +89,8 @@ err3 ...@@ -89,8 +89,8 @@ err3
# 3. Implementation of SVM predictions. # 3. Implementation of SVM predictions.
gaussian_kernel <- function(x_i, x_star, sigma) { gaussian_kernel <- function(x, sigma = 0.05) {
exp(-(dist(rbind(x_i, x_star)) ^ 2) / (2 * sigma ^ 2)) exp(-(x ^ 2) / (2 * sigma ^ 2))
} }
sv <- alphaindex(filter3)[[1]] sv <- alphaindex(filter3)[[1]]
...@@ -99,21 +99,15 @@ inte <- -b(filter3) ...@@ -99,21 +99,15 @@ inte <- -b(filter3)
k <- NULL k <- NULL
for (i in 1:10) { for (i in 1:10) {
# We produce predictions for just the first 10 points in the dataset. # We produce predictions for just the first 10 points in the dataset.
k2 <- inte
k2 <- 0
data_point <- spam[i, -58] data_point <- spam[i, -58]
for (j in 1:length(sv)) { for (j in 1:length(sv)) {
support_vector <- spam[sv[j], -58] support_vector <- spam[sv[j], -58]
kernel_value <- gaussian_kernel(support_vector, data_point, sigma = 0.05) kernel_value <- gaussian_kernel(sum((support_vector - data_point)^2), sigma = 0.05)
k2 <- k2 + co[j] * kernel_value k2 <- k2 + co[j] * kernel_value
} }
k2 <- k2 + inte
print(k2) print(k2)
k <- c(k, sign(k2)) k <- c(k, sign(k2))
} }
# Only first correct, close to decision boundary (0.006292512). # Only first correct, close to decision boundary (0.006292512).
......
library(neuralnet) library(neuralnet)
set.seed(1234567890) set.seed(1234567890)
# ----1.----
Var <- runif(500, 0, 10) Var <- runif(500, 0, 10)
mydata <- data.frame(Var, Sin = sin(Var))
mydata <- data.frame(Var, Sin=sin(Var))
tr <- mydata[1:25,] # Training tr <- mydata[1:25, ] # Training
te <- mydata[26:500,] # Test te <- mydata[26:500, ] # Test
# Random initialization of the weights in the interval [-1, 1] # Random initialization of the weights in the interval [-1, 1]
winit <- runif(10,-1,1) winit <- runif(10, -1, 1)
formula <- Sin ~ Var formula <- Sin ~ Var
nn <- neuralnet( formula , data = tr, hidden = c(10), startweights = winit ) nn <- neuralnet(
# Plot of the training data (black), test data (blue), and predictions (red) formula ,
plot(tr, cex=2) data = tr,
points(te, col = "blue", cex=1) hidden = c(10),
points(te[,1],predict(nn,te), col="red", cex=1) startweights = winit
)
### PART 2 ###
# Plot of the training data (black), test data (blue), and predictions (red)
plot(
tr,
cex = 2,
ylab = "sin(x)",
xlab = "x",
main = "NN with logistic activation function"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----2.----
h1 <- function(x) { h1 <- function(x) {
x x
} }
h2 <- function(x) { h2 <- function(x) {
ifelse(x>0,x,0) ifelse(x > 0, x, 0)
} }
h3 <- function(x) { h3 <- function(x) {
log(1 + exp(x)) log(1 + exp(x))
} }
nn_h1 <- neuralnet(
formula ,
nn_h1 <- neuralnet( formula , data = tr, hidden = c(10), startweights = t(winit), act.fct = h1 ) data = tr,
# Plot of the training data (black), test data (blue), and predictions (red) hidden = c(10),
plot(tr, cex=2, main = "h1") startweights = t(winit),
points(te, col = "blue", cex=1) act.fct = h1
points(te[,1],predict(nn_h1,te), col="red", cex=1) )
plot(tr,
cex = 2,
nn_h2 <- neuralnet( formula , data = tr, hidden = c(10), startweights = t(winit), act.fct = h2 ) xlab = "x",
# Plot of the training data (black), test data (blue), and predictions (red) "sin(x)",
plot(tr, cex=2, main="h2") main = "NN with linear activation function (h1)")
points(te, col = "blue", cex=1) points(te, col = "blue", cex = 1)
points(te[,1],predict(nn_h2,te), col="red", cex=1) points(te[, 1], predict(nn_h1, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
nn_h3 <- neuralnet( formula , data = tr, hidden = c(10), startweights = t(winit), act.fct = h3 ) col = c("black", "blue", "red"),
# Plot of the training data (black), test data (blue), and predictions (red) pch = 1,
plot(tr, cex=2, main = "h3") pt.cex = c(2, 1, 1),
points(te, col = "blue", cex=1) cex = 1
points(te[,1],predict(nn_h3,te), col="red", cex=1) )
nn_h2 <- neuralnet(
# part 3 formula ,
data = tr,
hidden = c(10),
startweights = t(winit),
act.fct = h2
)
plot(
tr,
cex = 2,
xlab = "x",
ylab = "sin(x)",
main = "NN with ReLU activation function (h2)"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn_h2, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
nn_h3 <- neuralnet(
formula ,
data = tr,
hidden = c(10),
startweights = t(winit),
act.fct = h3
)
plot(
tr,
cex = 2,
xlab = "x",
ylab = "sin(x)",
main = "NN with softplus activation function (h3)"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn_h3, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----3.----
Var1 <- runif(500, 0, 50) Var1 <- runif(500, 0, 50)
mydata1 <- data.frame(Var = Var1, Sin = sin(Var1))
mydata1 <- data.frame(Var = Var1, Sin=sin(Var1))
pred1 <- predict(nn, mydata1)
plot(mydata1, cex=2, main = "500 random points",ylim = c(-10,10))
points(mydata1, col = "blue", cex=1) plot(
pred <- predict(nn,te) mydata1,
prediciton <- predict(nn,mydata1) cex = 2,
points(mydata1[,1],prediciton, col="red", cex = 1) main = "NN with logistic activation",
ylim = c(-10, 2),
xlab = "x",
# Part 4 ylab = "sin(x)"
)
points(mydata1, col = "blue", cex = 1)
points(mydata1[, 1], pred1, col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----4.----
pred1[order(pred1)][1]
nn$weights nn$weights
# Large weights -> logistic activation function caps -> covergance for high x
# Part 5 # ----5.----
Var2 <- runif(500, 0, 10) Var2 <- runif(500, 0, 10)
mydata2 <- data.frame(Sin2=sin(Var2), Var2) mydata2 <- data.frame(Sin2 = sin(Var2), Var2)
formula1 <- Sin2 ~ Var2 formula1 <- Sin2 ~ Var2
nn2 <- neuralnet( formula1 , data = mydata2, hidden = c(10), startweights = winit) nn2 <- neuralnet(
formula1 ,
data = mydata2,
hidden = c(10),
startweights = winit
)
plot(mydata2, cex=2, main = "500 random points reverse", ylim = c(-2,10))
points(mydata2, col = "blue", cex=1)
points(mydata2[,1],predict(nn2,mydata2), col="red", cex = 1) plot(
mydata2,
cex = 2,
main = "NN with logistic activation function",
xlab = "sin(x)",
ylab = "x",
ylim = c(-2, 10)
)
points(mydata2, col = "blue", cex = 1)
points(mydata2[, 1],
predict(nn2, mydata2),
col = "red",
cex = 1)
grid()
legend(
"topleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# One-to-many mapping from sin(x) to x, e.g. both sin(0) and sin(2pi) equals 0
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