全连接层的线性代数
在 tensorflow 中有两种方式定义全连接(dense)层。第一种使用低层的线性代数运算;第二种使用高层的 keras 操作。本练习将采用第一种方法来构建下图所示的网络。
输入层包含 3 个特征——教育程度、婚姻状况和年龄——已经以 borrower_features 提供。隐藏层包含 2 个节点,输出层包含 1 个节点。
对于每一层,您将把前一层作为输入,初始化一组权重,计算输入与权重的乘积,然后应用激活函数。请注意,Variable()、ones()、matmul() 和 keras() 已从 tensorflow 导入。
本练习是课程的一部分
Python 中的 TensorFlow 入门
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize bias1
bias1 = Variable(1.0)
# Initialize weights1 as 3x2 variable of ones
weights1 = ____(ones((____, ____)))
# Perform matrix multiplication of borrower_features and weights1
product1 = ____
# Apply sigmoid activation function to product1 + bias1
dense1 = keras.activations.____(____ + ____)
# Print shape of dense1
print("\n dense1's output shape: {}".format(dense1.shape))