第 1 部分:参与赢取超赞奖品
在本练习中,您将学习 Dense 层。我们用一个有趣的场景来完成它:想象一档游戏节目,奖品由一个神经网络来决定。参赛者需要输入:
- 兄弟姐妹的数量,
- 今天喝了几杯咖啡,
- 是否喜欢西红柿,
然后模型会预测这位参赛者将赢得什么奖品。
为此,您将使用 Keras。您需要创建一个模型,其中输入层接受 3 个特征(兄弟姐妹数量为整数、咖啡杯数为整数、是否喜欢西红柿为 0 或 1)。然后输入经过一个 Dense 层,输出 3 个概率(即赢得汽车、礼品券或什么都没有的概率)。
Input、Dense 层以及 Keras 中的 Model 对象都已导入。同时提供了名为 init 的权重初始化器,用于初始化 Dense 层。
本练习是课程的一部分
使用 Keras 的机器翻译
练习说明
- 定义一个仅接受 3 名参赛者(批大小)的输入层,每位参赛者有 3 个输入:兄弟姐妹数量、咖啡杯数和是否喜欢西红柿(输入大小)。
- 定义一个具有 3 个输出、
softmax激活函数并以init作为初始化器的Dense层。 - 使用定义好的模型计算
x的预测结果。 - 为每位参赛者取得最可能的奖品(以整数表示)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define an input layer with batch size 3 and input size 3
inp = Input(____=(____,____))
# Get the output of the 3 node Dense layer
pred = ____(____, ____=____, kernel_initializer=init, bias_initializer=init)(inp)
model = Model(inputs=inp, outputs=pred)
names = ["Mark", "John", "Kelly"]
prizes = ["Gift voucher", "Car", "Nothing"]
x = np.array([[5, 0, 1], [0, 3, 1], [2, 2, 1]])
# Compute the model prediction for x
y = ____.____(____)
# Get the most probable class for each sample
classes = np.____(____, ____)
print("\n".join(["{} has probabilities {} and wins {}".format(n,p,prizes[c]) \
for n,p,c in zip(names, y, classes)]))