分批训练线性模型
在本练习中,您将继续上一题的进度,分批训练一个线性回归模型。我们会按批次遍历数据集,并在每一步后更新模型变量 intercept 和 slope。这种方式能让我们在内存无法一次容纳的大型数据集上完成训练。
请注意,损失函数 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())