python - 使用 TensorFlow 在我的 keras 代码中提前停止不起作用
问题描述
当我使用提前停止模型训练仅一个时期时,这不是应该做的。
这是没有提前停止的示例:
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return np.array(X), np.array(y)
sequence = np.arange(10, 1000, 10)
n_steps = 3
X, y = split_sequence(sequence, n_steps)
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_absolute_percentage_error')
# early_stopping = EarlyStopping(monitor='val_loss', patience= 5)
hist = model.fit(X, y, validation_split=0.2, batch_size = 16, epochs = 200)
从以下屏幕截图中可以看出,前 15 个以上的 epoch 的误差不断下降:
现在,如果我尝试提前停止它会在第一个时期停止:
hist = model.fit(X, y, validation_split=0.2, callbacks = [EarlyStopping(patience=5)], batch_size = 16)
我做错了什么,我该如何纠正?
解决方案
您忘记在此调用中指定 epoch 数,因此默认为 1:
hist = model.fit(X, y, validation_split=0.2, callbacks = [EarlyStopping(patience=5)], batch_size = 16)
将其更改为:
hist = model.fit(X, y, validation_split=0.2, callbacks=[EarlyStopping(patience=5)], batch_size=16, epochs=200)
干杯
推荐阅读
- node.js - Firebase 功能:发送响应后如何最好地等待解除阻塞的承诺?
- list - 如何按属性从列表中过滤对象 - Kotlin
- node.js - 编译包含依赖项的打字稿节点 js 应用程序
- flutter - 是否有任何方法或包装让我在我的设备主页中显示特定的应用程序屏幕
- apache2.4 - 服务器证书不包含与 django 的服务器名称匹配的 ID
- python - Plotly 4.9.0 的 Jupyterlab 扩展安装失败
- angularjs - 更新到 Angular 10 后,如果没有“new”,就无法调用类构造函数 UpgradeComponent
- python - 特征工程中的拟合和拟合变换有什么区别?
- amazon-web-services - 现有 S3 的无服务器触发器因后缀重叠而失败
- php - centos php服务器上的文件上传错误日志