开始使用免费开始使用

分批训练线性模型

在本练习中,您将继续上一题的进度,分批训练一个线性回归模型。我们会按批次遍历数据集,并在每一步后更新模型变量 interceptslope。这种方式能让我们在内存无法一次容纳的大型数据集上完成训练。

请注意,损失函数 loss_function(intercept, slope, targets, features) 已为您定义。此外,keras 已导入,numpy 可用名为 np。请按损失函数参数出现的顺序,将可训练变量填写到 var_list 中。

本练习是课程的一部分

Python 中的 TensorFlow 入门

查看课程

练习说明

  • 使用 .Adam() 优化器。
  • chunksize 为 100,按批次从 'kc_house_data.csv' 读取数据。
  • batch 中提取 price 列,转换为 32 位浮点类型的 numpy 数组,并赋给 price_batch
  • 完成损失函数、填写可训练变量列表,并执行最小化操作。

交互式实操练习

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

# Initialize Adam optimizer
opt = keras.optimizers.____

# Load data in batches
for batch in pd.read_csv('____', ____=____):
	size_batch = np.array(batch['sqft_lot'], np.float32)

	# Extract the price values for the current batch
	price_batch = np.array(batch['____'], np.____)

	# Complete the loss, fill in the variable list, and minimize
	opt.minimize(lambda: loss_function(____, slope, price_batch, size_batch), var_list=[intercept, ____])

# Print trained parameters
print(intercept.numpy(), slope.numpy())
编辑并运行代码