Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher dirkschumacher/lm_tf.R
Last active Jan 28, 2018

Embed
What would you like to do?
# 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
You can’t perform that action at this time.