Get startedGet started for free

Preparing to batch train

Before we can train a linear model in batches, we must first define variables, a loss function, and an optimization operation. In this exercise, we will prepare to train a model that will predict price_batch, a batch of house prices, using size_batch, a batch of lot sizes in square feet. In contrast to the previous lesson, we will do this by loading batches of data using pandas, converting it to numpy arrays, and then using it to minimize the loss function in steps.

Variable(), keras(), and float32 have been imported for you. Note that you should not set default argument values for either the model or loss function, since we will generate the data in batches during the training process.

This exercise is part of the course

Introduction to TensorFlow in Python

View Course

Exercise instructions

  • Define intercept as having an initial value of 10.0 and a data type of 32-bit float.
  • Define the model to return the predicted values using intercept, slope, and features.
  • Define a function called loss_function() that takes intercept, slope, targets, and features as arguments and in that order. Do not set default argument values.
  • Define the mean squared error loss function using targets and predictions.

Hands-on interactive exercise

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

# Define the intercept and slope
intercept = ___
slope = Variable(0.5, float32)

# Define the model
def linear_regression(intercept, slope, features):
	# Define the predicted values
	return ____

# Define the loss function
def ____:
	# Define the predicted values
	predictions = linear_regression(____, ____, features)
    
 	# Define the MSE loss
	return keras.losses.____(____, ____)
Edit and Run Code