Get startedGet started for free

Calculating slopes

You're now going to practice calculating slopes. When plotting the mean-squared error loss function against predictions, the slope is 2 * x * (xb-y), or 2 * input_data * error. Note that x and b may have multiple numbers (x is a vector for each data point, and b is a vector). In this case, the output will also be a vector, which is exactly what you want.

You're ready to write the code to calculate this slope while using a single data point. You'll use pre-defined weights called weights as well as data for a single point called input_data. The actual value of the target you want to predict is stored in target.

This exercise is part of the course

Introduction to Deep Learning in Python

View Course

Exercise instructions

  • Calculate the predictions, preds, by multiplying weights by the input_data and computing their sum.
  • Calculate the error, which is preds minus target. Notice that this error corresponds to xb-y in the gradient expression.
  • Calculate the slope of the loss function with respect to the prediction. To do this, you need to take the product of input_data and error and multiply that by 2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate the predictions: preds
preds = ____

# Calculate the error: error
error = ____ - ____

# Calculate the slope: slope
slope = ____ * ____ * ____

# Print the slope
print(slope)
Edit and Run Code