Last active
January 28, 2018 19:03
-
-
Save dirkschumacher/dd48ffde9d9bdc2729fba0056537965b to your computer and use it in GitHub Desktop.
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
# 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