比较激活函数
比较激活函数需要写一点代码,但这完全难不倒您!
您将把不同的激活函数用于第 2 章中为农场灌溉机器构建的多标签模型。函数 get_model('relu') 会返回该模型的一个副本,并将隐藏层的激活函数设为 'relu'。
您将遍历多个激活函数,为每个函数生成一个新模型并进行训练。把每次训练返回的 history 回调存入字典,您就能在下一个练习中可视化哪种激活函数表现最佳!
在训练模型时,X_train、y_train、X_test、y_test 已为您准备好可直接使用。
本练习是课程的一部分
Keras 深度学习入门
练习说明
- 将激活函数数组填写为
relu、leaky_relu、sigmoid和tanh。 - 在每次迭代中使用
get_model(),把当前激活函数作为参数传入以获取一个新模型。 - 训练模型时提供训练集和
validation_data,使用 20 个epochs,并将 verbose 设为 0。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Activation functions to try
activations = [____, ____, ____, ____]
# Loop over the activation functions
activation_results = {}
for act in activations:
# Get a new model with the current activation
model = ____
# Fit the model and store the history results
h_callback = ____
activation_results[act] = h_callback