开始使用免费开始使用

编译模型

接下来,您将编译之前定义的模型。编译模型时,需要指定要使用的优化器和损失函数。视频中,Dan 提到 Adam 优化器是一个非常好的选择。您可以在此处阅读关于它和其他 Keras 优化器的更多信息:here。如果您想更深入了解,还可以阅读介绍 Adam 优化器的原始论文

在本练习中,您将使用 Adam 优化器和均方误差损失函数。动手试试吧!

本练习是课程的一部分

Python 深度学习入门

查看课程

练习说明

  • 使用 model.compile() 编译模型。将 optimizer 设为 'adam'loss 设为 'mean_squared_error'

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import necessary modules
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

# Specify the model
n_cols = predictors.shape[1]
model = Sequential()
model.add(Dense(50, activation='relu', input_shape = (n_cols,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(1))

# Compile the model
____

# Verify that model contains information from compiling
print("Loss function: " + model.loss)
编辑并运行代码