山东大学暑期项目实训——云主机服务比价与预测系统

山东大学暑期项目实训——云主机服务比价与预测系统(十二)

使用tensorflow做预测

一、简单的预测函数值

1.使用训练数据做预测

创建模型:

model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation='relu'),
    layers.Dense(1)
])

optimizer = tf.keras.optimizers.RMSprop(0.001)

model.compile(loss='mse',
              optimizer=optimizer,
              metrics=['mae', 'mse'])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zg1DNY5u-1627206421750)(/Users/yuanbao/Library/Application Support/typora-user-images/image-20210428155135493.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OCZBKzZe-1627206421751)(/Users/yuanbao/Library/Application Support/typora-user-images/image-20210428155128032.png)]

该图表显示在约200个 epochs 之后误差非但没有改进,反而出现恶化。 让我们更新 model.fit 调用,当验证值没有提高上是自动停止训练。 我们将使用一个 EarlyStopping callback 来测试每个 epoch 的训练条件。如果经过一定数量的 epochs 后没有改进,则自动停止训练。

# patience 值用来检查改进 epochs 的数量
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

定义步数是10个没有改进就自动停止;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5HIRceXr-1627206421753)(/Users/yuanbao/Library/Application Support/typora-user-images/image-20210428200102089.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VnfQSMgm-1627206421755)(/Users/yuanbao/Library/Application Support/typora-user-images/image-20210428200109570.png)]

对比两个图,发现效果很明显!

可以通过改变patience参数的值来使得训练对于未改进的忍受程度。

2.通过模型做预测

使用自己的模型对数据做预测:

loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=2)

print("Testing set Mean Abs Error: {:5.2f} MPG".format(mae))

test_predictions = model.predict(normed_test_data).flatten()

plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
plt.axis('equal')
plt.axis('square')
plt.xlim([0,plt.xlim()[1]])
plt.ylim([0,plt.ylim()[1]])
_ = plt.plot([-100, 100], [-100, 100])
plt.show()

结果很接近正确结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jxJHX56G-1627206421756)(/Users/yuanbao/Library/Application Support/typora-user-images/image-20210428201736562.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值