This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pkg.add("Flux") | |
Pkg.add("Images") | |
Pkg.add("MLDatasets") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Flux: onehotbatch, argmax, crossentropy, throttle | |
using Base.Iterators: repeated | |
using MLDatasets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Flux, Flux.Data.MNIST | |
using Flux: onehotbatch, argmax, crossentropy, throttle | |
using Base.Iterators: repeated | |
dataset = repeated((X, Y), 200); | |
evalcb = () -> @show(loss(X, Y)); | |
opt = ADAM(params(m)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DataFrames, Requests | |
# Reading data locally | |
path = joinpath(homedir(), "Desktop"); | |
my_dat = readtable(joinpath(path, "data.csv")); | |
# Reading data from the web | |
url = "https://raw.githubusercontent.com/alstat/Analysis-with-Programming/master/2014/Python/Numerical-Descriptions-of-the-Data/data.csv"; | |
my_dat = readtable(get_streaming(url)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gadfly | |
using Distributions | |
function plot_theme() | |
Theme(panel_fill = colorant"#3B444B", default_color = colorant"#DE3163", | |
grid_color = colorant"#848482") | |
end | |
x = rand(Dirichlet([10; 1; 1]), 3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xyplot(log(pz) ~ 1:length(pz), xlab = 'No. of Iterations', | |
ylab = expression(paste('log', '(', E['in'], ')', sep = '')), | |
type = c('l', 'g'), lwd = 2, col = 'black') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
z_val1 <- numeric(); beta0 <- beta1 <- seq(-5, 5, length.out = 15) | |
g <- expand.grid(b0 = beta0, b1 = beta1); beta <- t(as.matrix(g)) | |
for (i in 1:ncol(beta)) { | |
z_val1[i] <- loss(y, X, cbind(beta[, i])) | |
} | |
z_val1 <- matrix(z_val1, 15, 15) | |
library(plot3D) | |
par(mai = c(.2, .2, .2, .2)) | |
persp3D(beta0, beta1, z_val1, col = ramp.col(n = 100, col = c('#FFBF00', '#FF7E00', '#FF033E', '#9966CC', '#007FFF')), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the objective function | |
loss <- function (y, X, beta) { | |
error <- X %*% beta - y | |
denom <- 2 * nrow(y) | |
sum(error**2) / denom | |
} | |
# first derivative of the objective function | |
loss_prime <- function (y, X, beta) { | |
error <- X %*% beta - y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lm(y ~ x) | |
# OUTPUT | |
Call: | |
lm(formula = y ~ x) | |
Coefficients: | |
(Intercept) x | |
2.7130 0.7973 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(12345) | |
x <- rnorm(100, 15, 2) | |
beta <- rbind(3.4, .75) # true parameter values of function f | |
err <- rnorm(100) # add noise | |
X <- cbind(1, x) # data matrix | |
y <- X %*% beta + err # our hypothesis function h | |
# plot it |