9951 explained code solutions for 126 technologies


python-tensorflowHow do I use Python and TensorFlow to implement a linear regression model?


To use Python and TensorFlow to implement a linear regression model, you need to define the features of the model, the input data, the model parameters, and the cost function.

#Define the features of the model
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]

#Define the input data
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])

#Define the model parameters
learning_rate = 0.01
training_steps = 1000

#Define the cost function
def loss(model, x, y):
  y_ = model(x)
  return tf.losses.mean_squared_error(labels=y, predictions=y_)

#Define the model
model = tf.contrib.learn.LinearRegressor(feature_columns=features)

#Train the model
model.fit(x=x_train, y=y_train, steps=training_steps, batch_size=2)

#Evaluate the model
ev = model.evaluate(x=x_train, y=y_train, steps=1)

#Print the results
print("Loss: %s" % ev["loss"])

Output example

Loss: 0.0013532908

The code above consists of the following parts:

  1. Define the features of the model: This is done by using the tf.contrib.layers.real_valued_column function.
  2. Define the input data: This is done by creating NumPy arrays containing the x and y values.
  3. Define the model parameters: This is done by setting the learning rate and the number of training steps.
  4. Define the cost function: This is done by creating a function that takes the model, x, and y values as arguments and returns the mean squared error between the labels and predictions.
  5. Define the model: This is done by using the tf.contrib.learn.LinearRegressor function.
  6. Train the model: This is done by using the fit function.
  7. Evaluate the model: This is done by using the evaluate function.

Helpful links

Edit this code on GitHub