mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-18 18:04:31 +08:00
Fix config and format
This commit is contained in:
@@ -27,7 +27,7 @@ port_analysis_config: &port_analysis_config
|
|||||||
task:
|
task:
|
||||||
model:
|
model:
|
||||||
class: HATS
|
class: HATS
|
||||||
module_path: qlib.contrib.model.pytorch_gats
|
module_path: qlib.contrib.model.pytorch_hats
|
||||||
kwargs:
|
kwargs:
|
||||||
d_feat: 6
|
d_feat: 6
|
||||||
hidden_size: 64
|
hidden_size: 64
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from ...model.base import Model
|
|||||||
from ...data.dataset import DatasetH
|
from ...data.dataset import DatasetH
|
||||||
from ...data.dataset.handler import DataHandlerLP
|
from ...data.dataset.handler import DataHandlerLP
|
||||||
|
|
||||||
|
|
||||||
class SFM_Model(nn.Module):
|
class SFM_Model(nn.Module):
|
||||||
def __init__(self, d_feat=6, output_dim=1, freq_dim=10, hidden_size=64, dropout_W=0.0, dropout_U=0.0, device="cpu"):
|
def __init__(self, d_feat=6, output_dim=1, freq_dim=10, hidden_size=64, dropout_W=0.0, dropout_U=0.0, device="cpu"):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -71,7 +72,7 @@ class SFM_Model(nn.Module):
|
|||||||
|
|
||||||
for ts in range(time_step):
|
for ts in range(time_step):
|
||||||
x = input[:, ts, :]
|
x = input[:, ts, :]
|
||||||
if(len(self.states)==0): #hasn't initialized yet
|
if len(self.states) == 0: # hasn't initialized yet
|
||||||
self.init_states(x)
|
self.init_states(x)
|
||||||
self.get_constants(x)
|
self.get_constants(x)
|
||||||
p_tm1 = self.states[0]
|
p_tm1 = self.states[0]
|
||||||
@@ -89,8 +90,9 @@ class SFM_Model(nn.Module):
|
|||||||
x_c = torch.matmul(x * B_W[0], self.W_c) + self.b_c
|
x_c = torch.matmul(x * B_W[0], self.W_c) + self.b_c
|
||||||
x_o = torch.matmul(x * B_W[0], self.W_o) + self.b_o
|
x_o = torch.matmul(x * B_W[0], self.W_o) + self.b_o
|
||||||
|
|
||||||
i = self.inner_activation(x_i + torch.matmul(h_tm1 * B_U[0], self.U_i)) # not sure whether I am doing in the right unsquuze
|
i = self.inner_activation(
|
||||||
|
x_i + torch.matmul(h_tm1 * B_U[0], self.U_i)
|
||||||
|
) # not sure whether I am doing in the right unsquuze
|
||||||
|
|
||||||
ste = self.inner_activation(x_ste + torch.matmul(h_tm1 * B_U[0], self.U_ste))
|
ste = self.inner_activation(x_ste + torch.matmul(h_tm1 * B_U[0], self.U_ste))
|
||||||
fre = self.inner_activation(x_fre + torch.matmul(h_tm1 * B_U[0], self.U_fre))
|
fre = self.inner_activation(x_fre + torch.matmul(h_tm1 * B_U[0], self.U_fre))
|
||||||
@@ -152,13 +154,14 @@ class SFM_Model(nn.Module):
|
|||||||
|
|
||||||
def get_constants(self, x):
|
def get_constants(self, x):
|
||||||
constants = []
|
constants = []
|
||||||
constants.append([torch.tensor(1.).to(self.device) for _ in range(6)])
|
constants.append([torch.tensor(1.0).to(self.device) for _ in range(6)])
|
||||||
constants.append([torch.tensor(1.).to(self.device) for _ in range(7)])
|
constants.append([torch.tensor(1.0).to(self.device) for _ in range(7)])
|
||||||
array = np.array([float(ii) / self.freq_dim for ii in range(self.freq_dim)])
|
array = np.array([float(ii) / self.freq_dim for ii in range(self.freq_dim)])
|
||||||
constants.append(torch.tensor(array).to(self.device))
|
constants.append(torch.tensor(array).to(self.device))
|
||||||
|
|
||||||
self.states[5:] = constants
|
self.states[5:] = constants
|
||||||
|
|
||||||
|
|
||||||
class SFM(Model):
|
class SFM(Model):
|
||||||
"""SFM Model
|
"""SFM Model
|
||||||
|
|
||||||
@@ -221,7 +224,7 @@ class SFM(Model):
|
|||||||
self.lr_decay_steps = lr_decay_steps
|
self.lr_decay_steps = lr_decay_steps
|
||||||
self.optimizer = optimizer.lower()
|
self.optimizer = optimizer.lower()
|
||||||
self.loss_type = loss
|
self.loss_type = loss
|
||||||
self.device = 'cuda:%d'%(GPU) if torch.cuda.is_available() else 'cpu'
|
self.device = "cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu"
|
||||||
self.use_gpu = torch.cuda.is_available()
|
self.use_gpu = torch.cuda.is_available()
|
||||||
self.seed = seed
|
self.seed = seed
|
||||||
|
|
||||||
@@ -277,7 +280,7 @@ class SFM(Model):
|
|||||||
freq_dim=self.freq_dim,
|
freq_dim=self.freq_dim,
|
||||||
dropout_W=self.dropout_W,
|
dropout_W=self.dropout_W,
|
||||||
dropout_U=self.dropout_U,
|
dropout_U=self.dropout_U,
|
||||||
device = self.device
|
device=self.device,
|
||||||
)
|
)
|
||||||
if optimizer.lower() == "adam":
|
if optimizer.lower() == "adam":
|
||||||
self.train_optimizer = optim.Adam(self.sfm_model.parameters(), lr=self.lr)
|
self.train_optimizer = optim.Adam(self.sfm_model.parameters(), lr=self.lr)
|
||||||
@@ -303,14 +306,7 @@ class SFM(Model):
|
|||||||
self._fitted = False
|
self._fitted = False
|
||||||
self.sfm_model.to(self.device)
|
self.sfm_model.to(self.device)
|
||||||
|
|
||||||
def fit(
|
def fit(self, dataset: DatasetH, evals_result=dict(), verbose=True, save_path=None, **kwargs):
|
||||||
self,
|
|
||||||
dataset: DatasetH,
|
|
||||||
evals_result=dict(),
|
|
||||||
verbose=True,
|
|
||||||
save_path=None,
|
|
||||||
**kwargs
|
|
||||||
):
|
|
||||||
|
|
||||||
df_train, df_valid = dataset.prepare(
|
df_train, df_valid = dataset.prepare(
|
||||||
["train", "valid"], col_set=["feature", "label"], data_key=DataHandlerLP.DK_L
|
["train", "valid"], col_set=["feature", "label"], data_key=DataHandlerLP.DK_L
|
||||||
@@ -399,7 +395,7 @@ class SFM(Model):
|
|||||||
# update learning rate
|
# update learning rate
|
||||||
self.scheduler.step(cur_loss_val)
|
self.scheduler.step(cur_loss_val)
|
||||||
|
|
||||||
if self.device != 'cpu':
|
if self.device != "cpu":
|
||||||
torch.cuda.empty_cache()
|
torch.cuda.empty_cache()
|
||||||
|
|
||||||
def get_loss(self, pred, target, loss_type):
|
def get_loss(self, pred, target, loss_type):
|
||||||
@@ -432,11 +428,11 @@ class SFM(Model):
|
|||||||
|
|
||||||
x_batch = torch.from_numpy(x_values[begin:end]).float()
|
x_batch = torch.from_numpy(x_values[begin:end]).float()
|
||||||
|
|
||||||
if self.device != 'cpu':
|
if self.device != "cpu":
|
||||||
x_batch = x_batch.to(self.device)
|
x_batch = x_batch.to(self.device)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
if self.device != 'cpu':
|
if self.device != "cpu":
|
||||||
pred = self.sfm_model(x_batch).detach().cpu().numpy()
|
pred = self.sfm_model(x_batch).detach().cpu().numpy()
|
||||||
else:
|
else:
|
||||||
pred = self.sfm_model(x_batch).detach().cpu().numpy()
|
pred = self.sfm_model(x_batch).detach().cpu().numpy()
|
||||||
@@ -461,8 +457,10 @@ class SFM(Model):
|
|||||||
self.sfm_model.load_state_dict(torch.load(_model_path))
|
self.sfm_model.load_state_dict(torch.load(_model_path))
|
||||||
self._fitted = True
|
self._fitted = True
|
||||||
|
|
||||||
|
|
||||||
class AverageMeter(object):
|
class AverageMeter(object):
|
||||||
"""Computes and stores the average and current value"""
|
"""Computes and stores the average and current value"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user