Skip to content

Instantly share code, notes, and snippets.

View alstat's full-sized avatar

Al Asaad alstat

View GitHub Profile
Pkg.add("Flux")
Pkg.add("Images")
Pkg.add("MLDatasets")
using Flux: onehotbatch, argmax, crossentropy, throttle
using Base.Iterators: repeated
using MLDatasets
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));
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));
@alstat
alstat / c1.jl
Last active March 27, 2016 00:33
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)
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')
@alstat
alstat / sgd4.R
Last active January 5, 2016 00:23
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')),
@alstat
alstat / sgd3.R
Last active January 5, 2016 00:27
# 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
lm(y ~ x)
# OUTPUT
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
2.7130 0.7973
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