Linear Regression in TensorFlow is easy to implement.
In the Linear Regression Model:
The goal is to find a relationship between a scalar dependent variable y and independent variables X.
The model is based on real world data and can be used to make predictions. Of course you can use random data, but it makes more sense to use real world data.
Consider this example:
X: gdp per capita
The model is:
Y_predicted = X * w + b
You can create a linear regression prediction model in a few steps.
If you want you can see the graph with TensorBoard.
You can find the complete code and dataset in this repo.
- read the data
This can be a simple text file with tab separated values.
create placeholders for inputs
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')
create weights and bias
w = tf.get_variable('weights', initializer=tf.constant(0.0))
b = tf.get_variable('bias', initializer=tf.constant(0.0))
make model to predict
Y_predicted = w * X + b
define loss function
# can use square error as loss function
loss = tf.square(Y - Y_predicted, name='loss')
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0003).minimize(loss)
train model (initialize variables, run optimizer)