Aan de slagGa gratis aan de slag

Set up a linear regression

A univariate linear regression identifies the relationship between a single feature and the target tensor. In this exercise, we will use a property's lot size and price. Just as we discussed in the video, we will take the natural logarithms of both tensors, which are available as price_log and size_log.

In this exercise, you will define the model and the loss function. You will then evaluate the loss function for two different values of intercept and slope. Remember that the predicted values are given by intercept + features*slope. Additionally, note that keras.losses.mse() is available for you. Furthermore, slope and intercept have been defined as variables.

Deze oefening maakt deel uit van de cursus

Introduction to TensorFlow in Python

Cursus bekijken

Oefeninstructies

  • Define a function that returns the predicted values for a linear regression using intercept, features, and slope, and without using add() or multiply().
  • Complete the loss_function() by adding the model's variables, intercept and slope, as arguments.
  • Compute the mean squared error using targets and predictions.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Define a linear regression model
def linear_regression(intercept, slope, features = size_log):
	return ____

# Set loss_function() to take the variables as arguments
def loss_function(____, ____, features = size_log, targets = price_log):
	# Set the predicted values
	predictions = linear_regression(intercept, slope, features)
    
    # Return the mean squared error loss
	return keras.losses.____

# Compute the loss for different slope and intercept values
print(loss_function(0.1, 0.1).numpy())
print(loss_function(0.1, 0.5).numpy())
Code bewerken en uitvoeren