mirror of
https://github.com/wassname/DeepTime.git
synced 2026-07-17 11:23:18 +08:00
87 KiB
87 KiB
In [1]:
import warnings
warnings.simplefilter("ignore")
# autoreload import your package
%load_ext autoreload
%autoreload 2In [2]:
import os
from os.path import join
import math
import logging
from typing import Callable, Optional, Union, Dict, Tuple
from matplotlib import pyplot as plt
from pathlib import Path
import matplotlib.colors as mcolors
import gin
from fire import Fire
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch import optim
from torch import nn
from experiments.base import Experiment
from data.datasets import ForecastDataset
from models import get_model
from utils.checkpoint import Checkpoint
from utils.ops import default_device, to_tensor
from utils.losses import get_loss_fn
from utils.metrics import calc_metrics
from experiments.forecast import get_data
gin.enter_interactive_mode()In [ ]:
In [19]:
def plot(model_name="deeptime", save_path=Path("storage/experiments/Exchange/96M/repeat=0"), i=200, title=None, plot=True):
gin.clear_config()
gin.parse_config(open(save_path/"config.gin"))
train_set, train_loader = get_data(flag='train', batch_size=2)
model = get_model(model_name,
dim_size=train_set.data_x.shape[1],
datetime_feats=train_set.timestamps.shape[-1]).to(default_device())
model.load_state_dict(torch.load(save_path/'model.pth'))
model = model.eval()
b = train_set[i]
b = [bb[None, :] for bb in b]
b2 = list(map(to_tensor, b))
context_past_x, context_y, query_past_x, query_y, context_time, query_time = b2
with torch.no_grad():
forecast = model(*b2)
if title is None:
title = str(save_path).split('/')[-3:]
title = "-".join(title)
colors = list(mcolors.BASE_COLORS.keys())
l = x.shape[1]
forecast2 = forecast[0].detach().cpu().numpy()
x2 = x[0].cpu()
y2 = y[0].cpu()
l2 = y.shape[1]
i_past = list(range(l))
i_future = list(range(l, l+l2))
if plot:
plt.title(title)
for i in range(x.shape[-1]):
plt.plot(i_past, x2[:, i], c=colors[i])
for i in range(x.shape[-1]):
plt.plot(i_future, y2[:, i], c=colors[i])
for i in range(x.shape[-1]):
plt.plot(i_future, forecast2[:, i], c=colors[i], linestyle='--')
return x2, y2, forecast2, i_past, i_future
Out [19]:
<data.datasets.ForecastDataset at 0x7fe2a3a58b50>
In [ ]:
In [ ]:
In [135]:
def plot_multi(save_paths=[Path("storage/experiments/Exchange/96M/repeat=0")], i=200, title=None, plot=True):
assert len(save_paths)>0
for j in range(len(save_paths)):
save_path = save_paths[j]
gin.clear_config()
gin.parse_config(open(save_path/"config.gin"))
model_name = gin.query_parameter("instance.model_type")
train_set, train_loader = get_data(flag='test', batch_size=3)
model = get_model(model_name,
dim_size=train_set.data_x.shape[1],
datetime_feats=train_set.timestamps.shape[-1]).to(default_device())
model.load_state_dict(torch.load(save_path/'model.pth'))
model = model.eval()
b = train_set[i]
b = [bb[None, :] for bb in b]
b2 = list(map(to_tensor, b))
# b = next(iter(train_loader))
# print([s.shape for s in b])
context_past_x, context_y, query_past_x, query_y, context_time, query_time = b2
with torch.no_grad():
forecast = model(*b2)
colors = list(mcolors.BASE_COLORS.keys())
l = context_time.shape[1]
forecast2 = forecast[0].detach().cpu().numpy()
x2 = context_y[0].cpu()
y2 = query_y[0].cpu()
l2 = query_time.shape[1]
i_past = list(range(l))
i_future = list(range(l, l+l2))
if plot:
mtitle = str(save_path).split('/')[-2:]
tags = mtitle[-1]
tags = [x.split('=')[-1] for x in tags.split(',')]
mtitle[-1] = ' '.join(tags)
mtitle = "-".join(mtitle)
plt.plot(i_future, forecast2[:, 0], c=colors[j], linestyle='--', label=f"{mtitle}")
if plot:
plt.plot(i_past, x2[:, 0], c=colors[0], label=f"past")
plt.plot(i_future, y2[:, 0], c=colors[0], label="future true", alpha=0.3)
plt.legend()
plt.title(title)
return x2, y2, forecast2, i_past, i_future
In [ ]:
In [91]:
save_path = Path('storage/experiments/Stocks/96M2S/repeat=0')In [92]:
gin.clear_config()
config_path = save_path/"config.gin"
gin.parse_config(open(config_path))
model_name = gin.query_parameter("instance.model_type")
model_name[0;31m---------------------------------------------------------------------------[0m [0;31mFileNotFoundError[0m Traceback (most recent call last) Cell [0;32mIn [92], line 3[0m [1;32m 1[0m gin[38;5;241m.[39mclear_config() [1;32m 2[0m config_path [38;5;241m=[39m save_path[38;5;241m/[39m[38;5;124m"[39m[38;5;124mconfig.gin[39m[38;5;124m"[39m [0;32m----> 3[0m gin[38;5;241m.[39mparse_config([38;5;28;43mopen[39;49m[43m([49m[43mconfig_path[49m[43m)[49m) [1;32m 4[0m model_name [38;5;241m=[39m gin[38;5;241m.[39mquery_parameter([38;5;124m"[39m[38;5;124minstance.model_type[39m[38;5;124m"[39m) [1;32m 5[0m model_name File [0;32m~/miniforge3/envs/deeptime/lib/python3.8/site-packages/IPython/core/interactiveshell.py:282[0m, in [0;36m_modified_open[0;34m(file, *args, **kwargs)[0m [1;32m 275[0m [38;5;28;01mif[39;00m file [38;5;129;01min[39;00m {[38;5;241m0[39m, [38;5;241m1[39m, [38;5;241m2[39m}: [1;32m 276[0m [38;5;28;01mraise[39;00m [38;5;167;01mValueError[39;00m( [1;32m 277[0m [38;5;124mf[39m[38;5;124m"[39m[38;5;124mIPython won[39m[38;5;124m'[39m[38;5;124mt let you open fd=[39m[38;5;132;01m{[39;00mfile[38;5;132;01m}[39;00m[38;5;124m by default [39m[38;5;124m"[39m [1;32m 278[0m [38;5;124m"[39m[38;5;124mas it is likely to crash IPython. If you know what you are doing, [39m[38;5;124m"[39m [1;32m 279[0m [38;5;124m"[39m[38;5;124myou can use builtins[39m[38;5;124m'[39m[38;5;124m open.[39m[38;5;124m"[39m [1;32m 280[0m ) [0;32m--> 282[0m [38;5;28;01mreturn[39;00m [43mio_open[49m[43m([49m[43mfile[49m[43m,[49m[43m [49m[38;5;241;43m*[39;49m[43margs[49m[43m,[49m[43m [49m[38;5;241;43m*[39;49m[38;5;241;43m*[39;49m[43mkwargs[49m[43m)[49m [0;31mFileNotFoundError[0m: [Errno 2] No such file or directory: 'storage/experiments/Stocks/96M2S/repeat=0/config.gin'
In [93]:
from experiments.forecast import ForecastExperiment
exp = ForecastExperiment(config_path=config_path)
expERROR:root:Path not found: storage/experiments/Stocks/96M2S/repeat=0/config.gin
[0;31m---------------------------------------------------------------------------[0m [0;31mOSError[0m Traceback (most recent call last) Cell [0;32mIn [93], line 2[0m [1;32m 1[0m [38;5;28;01mfrom[39;00m [38;5;21;01mexperiments[39;00m[38;5;21;01m.[39;00m[38;5;21;01mforecast[39;00m [38;5;28;01mimport[39;00m ForecastExperiment [0;32m----> 2[0m exp [38;5;241m=[39m [43mForecastExperiment[49m[43m([49m[43mconfig_path[49m[38;5;241;43m=[39;49m[43mconfig_path[49m[43m)[49m [1;32m 3[0m exp File [0;32m/media/wassname/SGIronWolf/projects5/investing/DeepTime/experiments/base.py:24[0m, in [0;36mExperiment.__init__[0;34m(self, config_path)[0m [1;32m 22[0m [38;5;28mself[39m[38;5;241m.[39mconfig_path [38;5;241m=[39m config_path [1;32m 23[0m [38;5;28mself[39m[38;5;241m.[39mroot [38;5;241m=[39m Path(config_path)[38;5;241m.[39mparent [0;32m---> 24[0m [43mgin[49m[38;5;241;43m.[39;49m[43mparse_config_file[49m[43m([49m[38;5;28;43mself[39;49m[38;5;241;43m.[39;49m[43mconfig_path[49m[43m)[49m File [0;32m~/miniforge3/envs/deeptime/lib/python3.8/site-packages/gin/config.py:2457[0m, in [0;36mparse_config_file[0;34m(config_file, skip_unknown, print_includes_and_imports)[0m [1;32m 2455[0m [38;5;28;01mreturn[39;00m results [1;32m 2456[0m err_str [38;5;241m=[39m [38;5;124m'[39m[38;5;124mUnable to open file: [39m[38;5;132;01m{}[39;00m[38;5;124m. Searched config paths: [39m[38;5;132;01m{}[39;00m[38;5;124m.[39m[38;5;124m'[39m [0;32m-> 2457[0m [38;5;28;01mraise[39;00m [38;5;167;01mIOError[39;00m(err_str[38;5;241m.[39mformat(config_file, prefixes)) [0;31mOSError[0m: Unable to open file: storage/experiments/Stocks/96M2S/repeat=0/config.gin. Searched config paths: [''].
In [94]:
# exp.run()In [151]:
# list the models we have run...
m=sorted(Path("storage/experiments/Stocks").glob("**/_SUCCESS"))
print(m)[Path('storage/experiments/Stocks/96M2S/base_learner=None,inr=INR,encoder=inception,repeat=0/_SUCCESS'), Path('storage/experiments/Stocks/96M2S/base_learner=None,inr=INRPlus2,encoder=inception,repeat=0/_SUCCESS'), Path('storage/experiments/Stocks/96M2S/base_learner=Ridge,inr=INR,encoder=inception,repeat=0/_SUCCESS')]
In [152]:
# m = [
# # Path('storage/experiments/Stocks/96M/repeat=0/_SUCCESS'),
# Path(
# 'storage/experiments/Stocks/96M2S/base_learner=None,inr=INRPlus2,encoder=inception,repeat=0/_SUCCESS'
# ),
# Path(
# 'storage/experiments/Stocks/96M2S/base_learner=Ridge,inr=INR,encoder=inception,repeat=0/_SUCCESS'
# ),
# Path(
# 'storage/experiments/Stocks/96M2S/base_learner=Ridge,inr=INRPlus2,encoder=inception,repeat=0/_SUCCESS'
# ),
# Path(
# 'storage/experiments/Stocks/96M2S/base_learner=Transformer,inr=INR,encoder=inception,repeat=0/_SUCCESS'
# ),
# Path(
# 'storage/experiments/Stocks/96M2S/base_learner=Transformer,inr=INRPlus2,encoder=inception,repeat=0/_SUCCESS'
# ),
# # Path('storage/experiments/Stocks/96S/repeat=0/_SUCCESS'),
# # Path('storage/experiments/Stocks/96Splus/repeat=0/_SUCCESS'),
# # Path('storage/experiments/Stocks/96Splusshort/repeat=0/_SUCCESS'),
# # Path('storage/experiments/Stocks/96Sshort/repeat=0/_SUCCESS')
# ]In [153]:
save_paths = [mm.parent for mm in m]
plot_multi(
save_paths=save_paths,
i=1000,
)
1Out [153]:
receptive field [114 126 42]=[38 18 2]*[[ 1 1 1] [ 1 2 4] [ 1 4 16]] receptive field [114 126 42]=[38 18 2]*[[ 1 1 1] [ 1 2 4] [ 1 4 16]] receptive field [690 378 242]=[138 18 2]*[[ 1 1 1] [ 1 2 4] [ 1 4 16] [ 1 6 36] [ 1 8 64]] receptive field [114 126 42]=[38 18 2]*[[ 1 1 1] [ 1 2 4] [ 1 4 16]]
1
In [ ]:
In [ ]:
In [139]:
train_set, train_loader = get_data(flag='test', batch_size=3)
b = context_past_x, context_y, query_past_x, query_y, context_time, query_time = train_set[100]
print([bb.shape for bb in b])
# context_y, query_y[(92, 5), (46, 1), (92, 5), (46, 1), (46, 7), (46, 7)]
In [80]:
cx_start, cx_end, c_start, c_end, qx_start, qx_end, q_start, q_end = train_set.get_inds(100)
cx_start, cx_end, c_start, c_end, qx_start, qx_end, q_start, q_endOut [80]:
(100, 192, 192, 238, 146, 238, 238, 284)
In [81]:
plt.hlines(1, cx_start, cx_end)
plt.hlines(2, c_start, c_end)
plt.hlines(3, qx_start, qx_end)
plt.hlines(4, q_start, q_end)Out [81]:
<matplotlib.collections.LineCollection at 0x7fe2a0938f10>