Spaces:
Sleeping
Sleeping
File size: 6,131 Bytes
1f1857d 943db67 1f1857d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import torch
from torch import nn
from torch import optim
from torch import functional as F
from einops import rearrange
import os
import pickle
#from modules.utils import *
from .utils import *
class Encoder(nn.Module):
def __init__(self, config):
super().__init__()
self.rnn = nn.RNN(input_size=config['z_dim'],
hidden_size=config['hidden_dim'],
num_layers=config['num_layer'])
self.fc = nn.Linear(in_features=config['hidden_dim'],
out_features=config['hidden_dim'])
def forward(self, x):
x_enc, _ = self.rnn(x)
x_enc = self.fc(x_enc)
return x_enc
class Decoder(nn.Module):
def __init__(self, config):
super().__init__()
self.rnn = nn.RNN(input_size=config['hidden_dim'],
hidden_size=config['hidden_dim'],
num_layers=config['num_layer'])
self.fc = nn.Linear(in_features=config['hidden_dim'],
out_features=config['z_dim'])
def forward(self, x_enc):
x_dec, _ = self.rnn(x_enc)
x_dec = self.fc(x_dec)
return x_dec
class Interpolator(nn.Module):
def __init__(self, config):
super().__init__()
self.sequence_inter = nn.Linear(in_features=(config['ts_size'] - config['total_mask_size']),
out_features=config['ts_size'])
self.feature_inter = nn.Linear(in_features=config['hidden_dim'],
out_features=config['hidden_dim'])
def forward(self, x):
# x(bs, vis_size, hidden_dim)
x = rearrange(x, 'b l f -> b f l') # x(bs, hidden_dim, vis_size)
x = self.sequence_inter(x) # x(bs, hidden_dim, ts_size)
x = rearrange(x, 'b f l -> b l f') # x(bs, ts_size, hidden_dim)
x = self.feature_inter(x) # x(bs, ts_size, hidden_dim)
return x
class StockEmbedder(nn.Module):
def __init__(self, cfg: dict = None) -> None:
"""
Args:
cfg (dict): {
'ts_size': 24,
'mask_size': 1,
'num_masks': 3,
'hidden_dim': 12,
'embed_dim': 6,
'num_layer': 3,
'z_dim': 6,
'num_embed': 32,
'stock_features': [],
'min_val': 0,
'max_val': 1e6
}
"""
super().__init__()
self.config = cfg
self.config['total_mask_size'] = self.config['num_masks'] * self.config['mask_size']
self.encoder = Encoder(config=self.config)
self.interpolator = Interpolator(config=self.config)
self.decoder = Decoder(config=self.config)
print('StockEmbedder initialized')
def mask_it(self,
x: torch.Tensor,
masks: torch.Tensor):
# x.shape = (bs, ts_size, z_dim)
b, l, f = x.shape
x_visible = x[~masks.bool(), :].reshape(b, -1, f) # (bs, vis_size, z_dim)
return x_visible
def forward_ae(self, x: torch.Tensor):
"""mae_pseudo_mask is equivalent to the Autoencoder
There is no interpolator in this mode
Args:
x (torch.Tensor): shape: (bs, ts_size, z_dim)
"""
out_encoder = self.encoder(x)
out_decoder = self.decoder(out_encoder)
return out_encoder, out_decoder
def forward_mae(self,
x: torch.Tensor,
masks: torch.Tensor):
"""No mask tokens, using Interpolation in the latent space
Args:
x (torch.Tensor): shape: (bs, ts_size, z_dim)
masks (torch.Tensor):
"""
x_vis = self.mask_it(x, masks=masks) # (bs, vis_size, z_dim)
out_encoder = self.encoder(x_vis) # (bs, vis_size, hidden_dim)
out_interpolator = self.interpolator(out_encoder) # (bs, ts_size, hidden_dim)
out_decoder = self.decoder(out_interpolator) # (bs, ts_size, z_dim)
return out_encoder, out_interpolator, out_decoder
def forward(self,
x: torch.Tensor,
masks: torch.Tensor = None,
mode: str = 'ae | mae'):
x = torch.tensor(x, dtype=torch.float32)
if masks is not None:
masks = torch.tensor(masks, dtype=torch.float32)
if mode == 'ae':
out_encoder, out_decoder = self.forward_ae(x)
return out_encoder, out_decoder
elif mode == 'mae':
out_encoder, out_interpolator, out_decoder = self.forward_mae(x, masks=masks)
return out_encoder, out_interpolator, out_decoder
def get_embedding(self,
stock_data: torch.Tensor,
embedding_used: str = 'encoder | decoder'):
"""get stock_embedding
Args:
stock_data (torch.Tensor): shape = (batch_size, stock_days, stock_features); NORMALIZED
"""
with torch.no_grad():
out_encoder, out_decoder = self.forward(stock_data, masks=None, mode='ae')
if embedding_used == 'encoder':
stock_embedding = out_encoder
elif embedding_used == 'decoder':
stock_embedding = out_decoder
return stock_embedding
def save(self, model_dir: str):
os.makedirs(model_dir, exist_ok=True)
# Save model:
torch.save(obj=self.state_dict(), f=os.path.join(model_dir, 'model.pth'))
# Save config:
with open(file=os.path.join(model_dir, 'config.pkl'), mode='wb') as f:
pickle.dump(obj=self.config, file=f) |