多样本的低层级实现
在本练习中,您将通过在多样本情形下构建第一个稠密隐藏层,进一步加深对低层级实现方法的直觉。我们假设模型已训练完毕,第一层的权重 weights1 和偏置 bias1 已可用。接下来,我们将把张量 borrower_features 与变量 weights1 做矩阵相乘。回忆一下,borrower_features 张量包含受教育程度、婚姻状况和年龄。最后,对 products1 + bias1 的各元素应用 sigmoid 函数,得到 dense1。
\(products1 = \begin{bmatrix} 3 & 3 & 23 \\ 2 & 1 & 24 \\ 1 & 1 & 49 \\ 1 & 1 & 49 \\ 2 & 1 & 29 \end{bmatrix} \begin{bmatrix} -0.6 & 0.6 \\ 0.8 & -0.3 \\ -0.09 & -0.08 \end{bmatrix}\)
注意,matmul() 和 keras() 已从 tensorflow 导入。
本练习是课程的一部分
Python 中的 TensorFlow 入门
练习说明
- 通过将特征张量与权重做矩阵相乘来计算
products1。 - 使用 sigmoid 激活函数变换
products1 + bias1。 - 打印
borrower_features、weights1、bias1和dense1的形状。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Compute the product of borrower_features and weights1
products1 = ____
# Apply a sigmoid activation function to products1 + bias1
dense1 = ____
# Print the shapes of borrower_features, weights1, bias1, and dense1
print('\n shape of borrower_features: ', borrower_features.shape)
print('\n shape of weights1: ', ____.shape)
print('\n shape of bias1: ', ____.shape)
print('\n shape of dense1: ', ____.shape)