Skip to content
Snippets Groups Projects
assignment4.R 3.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • library(neuralnet)
    
    set.seed(1234567890)
    
    
    # ----1.----
    
    
    Var <- runif(500, 0, 10)
    
    
    mydata <- data.frame(Var, Sin = sin(Var))
    
    tr <- mydata[1:25, ] # Training
    te <- mydata[26:500, ] # Test
    
    
    
    # Random initialization of the weights in the interval [-1, 1]
    
    winit <- runif(10, -1, 1)
    
    formula <- Sin ~ Var
    
    nn <- neuralnet(
      formula ,
      data = tr,
      hidden = c(10),
      startweights = winit
    )
    
    # 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) {
      x
    }
    
    h2 <- function(x) {
    
      ifelse(x > 0, x, 0)
    
    }
    
    h3 <- function(x)  {
      log(1 + exp(x))
    }
    
    
    nn_h1 <- neuralnet(
      formula ,
      data = tr,
      hidden = c(10),
      startweights = t(winit),
      act.fct = h1
    )
    
    plot(tr,
         cex = 2,
         xlab = "x",
         "sin(x)",
         main = "NN with linear activation function (h1)")
    points(te, col = "blue", 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"),
      col = c("black", "blue", "red"),
      pch = 1,
      pt.cex = c(2, 1, 1),
      cex = 1
    )
    
    nn_h2 <- neuralnet(
      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)
    
    
    mydata1 <- data.frame(Var = Var1, Sin = sin(Var1))
    
    pred1 <- predict(nn, mydata1)
    
    plot(
      mydata1,
      cex = 2,
      main = "NN with logistic activation",
      ylim = c(-10, 2),
      xlab = "x",
      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]
    
    Mehmet Celik Yildirim's avatar
    Mehmet Celik Yildirim committed
    nn$weights
    
    # ----5.----
    
    Mehmet Celik Yildirim's avatar
    Mehmet Celik Yildirim committed
    
    Var2 <- runif(500, 0, 10)
    
    
    mydata2 <- data.frame(Sin2 = sin(Var2), Var2)
    
    Mehmet Celik Yildirim's avatar
    Mehmet Celik Yildirim committed
    
    
    formula2 <- Var2 ~ Sin2
    
    Mehmet Celik Yildirim's avatar
    Mehmet Celik Yildirim committed
    
    
    nn2 <- neuralnet(
    
      data = mydata2,
      hidden = c(10),
    
      startweights = winit,
      threshold = 0.1
    
      col = "blue",
      cex = 1,
    
      main = "NN with logistic activation function",
      xlab = "sin(x)",
      ylab = "x",
    )
    points(mydata2[, 1],
           predict(nn2, mydata2),
           col = "red",
           cex = 1)
    grid()
    legend(
    
      "bottomleft",
      legend = c("Training data", "Predicted training data"),
      col = c("blue", "red"),
    
      pch = 1,
    
      pt.cex = c(1, 1),
    
      cex = 1