私募

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

期货量化交易软件:时间序列挖掘的数据标签使用标签数据的示例(

[复制链接]
发表于 2024-4-20 08:09:36 | 显示全部楼层 |阅读模式
定义执行逻辑1. 定义全局变量:
1 K" x8 D% x( B& S0 vep=200__train=Falsemt_data_len=200000max_encoder_length = 2*96max_prediction_length = 30batch_size = 128
/ Z- U  D" w4 _+ R 期货量化交易软件:时间序列挖掘的数据标签使用标签数据的示例(-1.jpg 添加图片注释,不超过 140 字(可选)__train用于控制赫兹量化交易软件当前是在训练还是测试模型。值得注意的是,ep用于控制最大训练时期。由于我们已经设置了EarlyStoping,因此可以将该值设置得更大一点,因为当模型不再收敛时,它将自动停止。mt_data_len是从客户端获得的最近时间序列数据的数量。max_encoder_length 和 max_prediction_length 分别是最大编码长度和最大预测长度。2.训练当训练完成时,赫兹量化交易软件还需要将当前的最佳训练结果保存到本地文件中,因此我们定义了一个json文件来保存这些信息:info_file='results.json'为了使我们的训练过程更加清晰,赫兹量化交易软件需要避免在训练过程中输出一些不必要的警告信息,因此我们将添加以下代码:
/ }- d- |- H) Bwarnings.filterwarnings("ignore")& U) x) ?2 w: v% R# @
接下来是我们的训练逻辑:
/ C. H8 F: E+ n/ A2 N8 a/ vdt=get_data(mt_data_len=mt_data_len)if __train:    # print(dt)    # dt=get_data(mt_data_len=mt_data_len)    t_loader,v_loader,training=spilt_data(dt,                                    t_shuffle=False,t_drop_last=True,                                    v_shuffle=False,v_drop_last=True)    lr=get_learning_rate()    trainer__=train()    m_c_back=trainer__.checkpoint_callback    m_l_back=trainer__.early_stopping_callback    best_m_p=m_c_back.best_model_path    best_m_l=m_l_back.best_score.item()    # print(best_m_p)    if os.path.exists(info_file):        with open(info_file,'r+') as f1:            last=json.load(fp=f1)            last_best_model=last['last_best_model']            last_best_score=last['last_best_score']            if last_best_score > best_m_l:                last['last_best_model']=best_m_p                last['last_best_score']=best_m_l                json.dump(last,fp=f1)    else:                       with open(info_file,'w') as f2:            json.dump(dict(last_best_model=best_m_p,last_best_score=best_m_l),fp=f2)
3 @' [5 ^& G  B4 E/ `' W: Z训练完成后,您可以在根目录的results.json文件中找到我们最佳模型的存储位置和最佳分数。在训练过程中,您将看到一个进度条,显示每个 epoch 的进度。 训练:3 e# s4 D% m7 U3 M8 W! L5 r2 b' j
& h- s: ?$ B8 k4 U$ m/ X4 f
期货量化交易软件:时间序列挖掘的数据标签使用标签数据的示例(-2.jpg 添加图片注释,不超过 140 字(可选)训练完成
2 s  ^7 t: a" D8 ?* ~* E( _' b. U, P/ w7 N& U) g3 v
期货量化交易软件:时间序列挖掘的数据标签使用标签数据的示例(-3.jpg 添加图片注释,不超过 140 字(可选)3. 验证模型在训练之后,赫兹量化交易软件希望验证模型并将其可视化。我们可以添加以下代码:
$ {- x5 `. L$ Jbest_model = NHiTS.load_from_checkpoint(best_m_p)predictions = best_model.predict(v_loader, trainer_kwargs=dict(accelerator="cpu",logger=False), return_y=True)raw_predictions = best_model.predict(v_loader, mode="raw", return_x=True, trainer_kwargs=dict(accelerator="cpu",logger=False))for idx in range(10):  # plot 10 examples    best_model.plot_prediction(raw_predictions.x, raw_predictions.output, idx=idx, add_loss_to_title=True)    # sample 500 pathssamples = best_model.loss.sample(raw_predictions.output["prediction"][[0]], n_samples=500)[0]# plot predictionfig = best_model.plot_prediction(raw_predictions.x, raw_predictions.output, idx=0, add_loss_to_title=True)ax = fig.get_axes()[0]# plot first two sampled pathsax.plot(samples[:, 0], color="g", label="Sample 1")ax.plot(samples[:, 1], color="r", label="Sample 2")fig.legend()plt.show()
1 M* h0 e5 u! v( ^您也可以在训练期间使用TensorBoard实时查看训练情况的可视化,我们在这里不做演示。结果:( n- W% X% D3 G% D' ?
% Z: M; h  [9 Z2 b
期货量化交易软件:时间序列挖掘的数据标签使用标签数据的示例(-4.jpg 添加图片注释,不超过 140 字(可选)
http://www.simu001.cn/x288579x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2025-6-15 14:19 , Processed in 0.390293 second(s), 34 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表