Skip to content

Instantly share code, notes, and snippets.

View GarrettMooney's full-sized avatar

Garrett Mooney GarrettMooney

View GitHub Profile
@GarrettMooney
GarrettMooney / gpu_toggle.sh
Created May 3, 2020 01:29
Toggle between using GPU for Deep Learning and Video Games.
#!/bin/bash
flag=$1
if [[ $flag == 'DL' ]]; then
# -------------
# Deep Learning
# -------------
echo "Configuring GPU for Deep Learning..."
sudo apt-get install -y libnvidia-common-440
sudo apt-get install -y cuda
sudo apt-get install -y cuda-drivers
@GarrettMooney
GarrettMooney / parallel.h
Created October 21, 2019 03:41
parallel for loop in C++
#pragma once
#include <algorithm>
#include <functional>
#include <future>
#include <thread>
#include <vector>
// adapted from https://stackoverflow.com/a/49188371/2055486
@GarrettMooney
GarrettMooney / kl_entropy.R
Created September 28, 2018 04:29
Entropy, KL Divergence, & Rcpp
library(ggplot2)
theme_set(theme_classic())
# Visualizing entropy ---------------------------------------------------------
entropy <- function(p) -sum(p * log(p))
n <- 1e6
p <- runif(n)
q <- 1 - p
z <- matrix(c(p, q), ncol = 2)
ent <- apply(z, 1, entropy)
@GarrettMooney
GarrettMooney / entropy_example.R
Created September 28, 2018 04:22
Entropy & Rcpp
li(ggplot2)
theme_set(theme_classic())
entropy <- function(p) -sum(p * log(p))
n <- 1e6
p <- runif(n)
q <- 1 - p
z <- matrix(c(p, q), ncol = 2)
ent <- apply(z, 1, entropy)
qplot(q, ent, geom = "line") +
@GarrettMooney
GarrettMooney / kl_divergence_example.R
Created September 28, 2018 04:21
KL Divergence & Rcpp
li(ggplot2)
theme_set(theme_classic())
kld <- function(p, q) sum(p * log(p / q))
n <- 1e6
p <- c(0.6, 0.4)
q1 <- runif(n)
q2 <- 1 - q1
z <- matrix(c(q1, q2), ncol = 2)
kl <- apply(z, 1, kld, p = p)
@GarrettMooney
GarrettMooney / .Rprofile
Last active September 20, 2018 03:04
.Rprofile
.First <- function() {
# set TZ if unset
if (is.na(Sys.getenv("TZ", unset = NA)))
Sys.setenv(TZ = "America/New_York")
# bail if revo R
if (exists("Revo.version"))
return()
@GarrettMooney
GarrettMooney / closed_form_ridge_regression.R
Created September 5, 2018 03:46
Closed form ridge regression.
library(tidyverse)
y <- as.matrix(mtcars$mpg)
X <- model.matrix(mpg ~ ., mtcars)
# linear regression
solve(crossprod(X)) %*% t(X) %*% y
solve(crossprod(X) + 0 * diag(rep(1, ncol(X)))) %*% t(X) %*% y
# ridge regression
@GarrettMooney
GarrettMooney / create_gist.R
Created August 30, 2018 01:08
Create gist via R.
library(gistr)
# create
gist_create(files='../create_gist.R',
description='Create gist via R.')
# update
gists(what = "minepublic")[[1]] %>%
update_files('../create_gist.R') %>%
update()
@GarrettMooney
GarrettMooney / prediction_by_matrix_multiplication.R
Created August 30, 2018 01:06
Predict for multiple models using linear algebra.
library(purrr)
# two regressions
lm_fit <- lm(mpg ~ wt + cyl, data = mtcars)
bayes_fit <- rstanarm::stan_glm(mpg ~ wt + cyl, data = mtcars)
# design matrix [32 x 3]
X <- model.matrix(lm_fit)
# coefficient matrix [3 x 2]
@GarrettMooney
GarrettMooney / get_code.R
Last active March 8, 2018 01:41
Get code from blog
library(rvest)
library(purrr)
library(stringr)
url <- "http://ellisp.github.io/blog/2016/11/06/forecastxgb"
robotstxt::get_robotstxt(url)
url %>%
read_html() %>% html_nodes("span") %>% html_text() %>%
reduce(., str_c) %>%