Skip to content

Instantly share code, notes, and snippets.

View bquast's full-sized avatar
:octocat:

Bastiaan Quast bquast

:octocat:
View GitHub Profile
@bquast
bquast / dropout.py
Last active October 2, 2015 12:28
extended version of the Hilton's dropout code from http://iamtrask.github.io/2015/07/28/dropout/
import numpy as np
alpha = 0.1
hidden_dim = 4
dropout_percent = 0.05
do_dropout = True
# compute sigmoid nonlinearity
def sigmoid(x):
output = 1/(1+np.exp(-x))
# create explore function
# to be applied to data.frames created by importing using the haven package
# https://cran.r-project.org/package=haven
# also available in the explore package:
# https://github.com/bquast/explore
explore <- function(data) {
description <- sapply(data, attr, "label")
variables <- names(description)
# https://designdatadecisions.wordpress.com/2016/05/05/manipulated-regression/
# Jyothi Subramanian, Ph. D.
library(manipulate)
## First define a custom function that fits a linear regression line
## to (x,y) points and overlays the regression line in a scatterplot.
## The plot is then 'manipulated' to change as y values change.
linregIllustrate <- function(x, y, e, h.max, h.med){
els <- function(formula, data, ...){
first_stage <- lm(formula,data=data, ...)
lm(update(formula, fitted(first_stage)~.), data, ...)
}
library(ggplot2)
library(scales)
library(gridExtra)
darkRed <- rgb(199,35,28, maxColorValue = 255)
darkGray <- rgb(128,128,128, maxColorValue = 255)
darkOLDblue <- rgb(0,15,118, maxColorValue = 255)
lightgray <- rgb(200,200,200, maxColorValue = 255)
#### OLS by hand
solve(qr.R(qr(freeny.x)))%*%t(qr.Q(qr(freeny.x)))%*%freeny.y
QR1<- qr(freeny.x)
solve(qr.R(QR1))%*%t(qr.Q(QR1))%*%freeny.y
solve(qr.R(QR1))%*%crossprod(qr.Q(QR1),freeny.y)
backsolve(qr.R(QR1),crossprod(qr.Q(QR1),freeny.y))
coef(lm(freeny.y~freeny.x -1))
@bquast
bquast / LSTM.R
Last active October 13, 2016 17:08
set.seed(1)
# define some functions
## convert integer to binary
i2b <- function(integer, length=8)
as.numeric(intToBits(integer))[1:length]
## apply
int2bin <- function(integer, length=8)
# define some functions
## convert integer to binary
i2b <- function(integer, length=8)
as.numeric(intToBits(integer))[1:length]
## apply
int2bin <- function(integer, length=8)
t(sapply(integer, i2b, length=length))
# sigmoid function
sigmoid <- function(x)
1 / (1 + exp(-x) )
# sigmoid derivative
sigmoid_output_to_derivative <- function(x)
x*(1-x)
# hidden layer size
hidden_dim = 4
## sigmoid function
sigmoid <- function(x, k=1, x0=0)
1 / (1+exp( -k*(x-x0) ))
## tanh^2 function
tanhsq <- function(x)
((exp(2*x)-1)^2)/((exp(2*x)+1)^2)
# define weights
Wa = matrix( c(0.45, 0.25), nrow=1 ); Ua = matrix(0.15); ba = matrix(0.20)