plot_loss: Gradient Descent Algorithm - Plotting the Loss Function

Description Usage Arguments Author(s) Examples

View source: R/gradientdescent.R

Description

plot_loss Plots the loss function of an object containing the results of a gradient descent object implementation

Usage

1
plot_loss(obj)

Arguments

obj

Object containing the results of a gradient descent implementation

Author(s)

Jocelyn T. Chi

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Generate some data for a simple bivariate example
set.seed(12345)
x <- sample(seq(from = -1, to = 1, by = 0.1), size = 50, replace = TRUE)
y <- 2*x + rnorm(50)

# Components required for gradient descent
X <- as.matrix(x)
y <- as.vector(y)
f <- function(X,y,b) {
   (1/2)*norm(y-X%*%b,"F")^{2}
}
grad_f <- function(X,y,b) {
   t(X)%*%(X%*%b - y)
}

# Run a simple gradient descent example
simple_ex <- gdescent(f,grad_f,X,y,alpha=0.01)

# Plot the loss function
plot_loss(simple_ex)

Example output

Loading required package: ggplot2
Loading required package: grid
Loading required package: Matrix
Minimum function value:
 36.84783

Intercept:
 0.2800326

Coefficient(s):
 2.122581

gettingtothebottom documentation built on May 29, 2017, 8:28 p.m.