Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher
Last active January 28, 2018 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkschumacher/dd48ffde9d9bdc2729fba0056537965b to your computer and use it in GitHub Desktop.
Save dirkschumacher/dd48ffde9d9bdc2729fba0056537965b to your computer and use it in GitHub Desktop.
# Linear regression with tensorflow and R
library(tensorflow)
# Y = X * beta + epsilon
# =>
# beta = (X'X)^-1X'y
# first we build the computational graph
X <- tf$placeholder(tf$float64, name = "X")
y <- tf$placeholder(tf$float64, name = "y")
x_mult <- tf$matmul(X, X, transpose_a = TRUE)
x_mult_inv <- tf$matrix_inverse(x_mult)
beta <- tf$matmul(
tf$matmul(x_mult_inv, X, transpose_b = TRUE),
y
)
# then we build the input data for our model,
# in this case we predict hp by cyl and mpg
formula <- hp ~ cyl + mpg
mtcars_model <- model.matrix(formula, data = mtcars)
# to evaluate the beta operation, we can call tf$eval
# with placeholder information for X and y
sess <- tf$Session()
tf_coef <- beta$eval(session = sess, feed_dict = list(
"X:0" = mtcars_model,
"y:0" = matrix(mtcars$hp, ncol = 1)
))
# for comparison, the lm function of R
lm_coef <- coef(lm(formula, data = mtcars))
all.equal(as.numeric(tf_coef), as.numeric(lm_coef))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment