开始使用免费开始使用

准备进行批量训练

在按批训练线性模型之前,必须先定义变量、损失函数和优化操作。此练习中,您将准备训练一个模型,用 size_batch(以平方英尺计的地块面积批次)来预测 price_batch(房价批次)。与上一课不同,我们将使用 pandas 加载数据批次,将其转换为 numpy 数组,然后分步最小化损失函数。

已为您导入 Variable()keras()float32。请注意,切勿为模型或损失函数设置默认参数值,因为我们将在训练过程中按批生成数据。

本练习是课程的一部分

Python 中的 TensorFlow 入门

查看课程

练习说明

  • intercept 定义为初始值 10.0,数据类型为 32 位浮点数。
  • 定义模型,使用 interceptslopefeatures 返回预测值。
  • 定义一个名为 loss_function() 的函数,按顺序接收 interceptslopetargetsfeatures 作为参数。不要设置默认参数值。
  • 使用 targetspredictions 定义均方误差损失函数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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.____(____, ____)
编辑并运行代码