1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-15 00:36:55 +08:00
This commit is contained in:
Alex Wang
2020-11-25 19:46:48 +08:00
parent 2c403943b2
commit 64b7748033
2 changed files with 91 additions and 65 deletions

View File

@@ -1,5 +1,15 @@
# Copyright (c) Microsoft Corporation. # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License. # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys import sys
from pathlib import Path from pathlib import Path
@@ -61,22 +71,22 @@ if __name__ == "__main__":
"module_path": "qlib.contrib.model.pytorch_sfm", "module_path": "qlib.contrib.model.pytorch_sfm",
"kwargs": { "kwargs": {
"d_feat": 6, "d_feat": 6,
"hidden_size": 64, "hidden_size": 32,
"output_dim": 1, "output_dim" : 16,
"freq_dim": 15, "freq_dim" : 25,
"dropout_W": 0.5, "dropout_W": 0.5,
"dropout_U": 0.5, "dropout_U": 0.5,
"n_epochs": 10, "n_epochs": 200,
"lr": 1e-3, "lr": 1e-3,
"batch_size": 800, "batch_size": 200,
"early_stop": 20, "early_stop": 20,
"eval_steps": 5, "eval_steps": 5,
"loss": "mse", "loss": "mse",
"lr_decay": 0.96, "lr_decay" : 0.96,
"lr_decay_steps": 100, "lr_decay_steps" : 100,
"optimizer": "gd", "optimizer" : "adam",
"GPU": 1, "GPU": 1,
"seed": 0, "seed": 710,
}, },
}, },
"dataset": { "dataset": {

View File

@@ -21,12 +21,11 @@ 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__()
self.input_dim = d_feat self.input_dim = d_feat
self.output_dim = output_dim self.output_dim = output_dim
self.freq_dim = freq_dim self.freq_dim = freq_dim
self.hidden_dim = hidden_size self.hidden_dim = hidden_size
@@ -66,13 +65,13 @@ class SFM_Model(nn.Module):
self.states = [] self.states = []
def forward(self, input): def forward(self, input):
input = input.reshape(len(input), self.input_dim, -1) # [N, F, T] input = input.reshape(len(input), self.input_dim, -1) # [N, F, T]
input = input.permute(0, 2, 1) # [N, T, F] input = input.permute(0, 2, 1) # [N, T, F]
time_step = input.shape[1] time_step = input.shape[1]
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]
@@ -90,9 +89,8 @@ 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( 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
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))
@@ -154,14 +152,13 @@ class SFM_Model(nn.Module):
def get_constants(self, x): def get_constants(self, x):
constants = [] constants = []
constants.append([torch.tensor(1.0).to(self.device) for _ in range(6)]) constants.append([torch.tensor(1.).to(self.device) for _ in range(6)])
constants.append([torch.tensor(1.0).to(self.device) for _ in range(7)]) constants.append([torch.tensor(1.).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
@@ -188,7 +185,7 @@ class SFM(Model):
d_feat=6, d_feat=6,
hidden_size=64, hidden_size=64,
output_dim=1, output_dim=1,
freq_dim=10, freq_dim = 10,
dropout_W=0.0, dropout_W=0.0,
dropout_U=0.0, dropout_U=0.0,
n_epochs=200, n_epochs=200,
@@ -224,7 +221,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
@@ -232,6 +229,7 @@ class SFM(Model):
"SFM parameters setting:" "SFM parameters setting:"
"\nd_feat : {}" "\nd_feat : {}"
"\nhidden_size : {}" "\nhidden_size : {}"
"\noutput_size : {}"
"\nfrequency_dimension : {}" "\nfrequency_dimension : {}"
"\ndropout_W: {}" "\ndropout_W: {}"
"\ndropout_U: {}" "\ndropout_U: {}"
@@ -249,6 +247,7 @@ class SFM(Model):
"\nseed : {}".format( "\nseed : {}".format(
d_feat, d_feat,
hidden_size, hidden_size,
output_dim,
freq_dim, freq_dim,
dropout_W, dropout_W,
dropout_U, dropout_U,
@@ -273,13 +272,13 @@ class SFM(Model):
self.sfm_model = SFM_Model( self.sfm_model = SFM_Model(
d_feat=self.d_feat, d_feat=self.d_feat,
output_dim=self.output_dim, output_dim = self.output_dim,
hidden_size=self.hidden_size, hidden_size = self.hidden_size,
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)
elif optimizer.lower() == "gd": elif optimizer.lower() == "gd":
@@ -304,7 +303,14 @@ class SFM(Model):
self._fitted = False self._fitted = False
self.sfm_model.to(self.device) self.sfm_model.to(self.device)
def fit(self, dataset: DatasetH, evals_result=dict(), verbose=True, save_path=None, **kwargs): def fit(
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
@@ -360,7 +366,6 @@ class SFM(Model):
# validation # validation
train_loss += loss.val train_loss += loss.val
# print(loss.val)
if step and step % self.eval_steps == 0: if step and step % self.eval_steps == 0:
stop_steps += 1 stop_steps += 1
train_loss /= self.eval_steps train_loss /= self.eval_steps
@@ -394,12 +399,12 @@ 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):
if loss_type == "mse": if loss_type == "mse":
sqr_loss = (pred - target) ** 2 sqr_loss = (pred - target)**2
loss = sqr_loss.mean() loss = sqr_loss.mean()
return loss return loss
elif loss_type == "binary": elif loss_type == "binary":
@@ -414,17 +419,30 @@ class SFM(Model):
x_test = dataset.prepare("test", col_set="feature") x_test = dataset.prepare("test", col_set="feature")
index = x_test.index index = x_test.index
x_test = torch.from_numpy(x_test.values).float()
x_test = x_test.to(self.device)
self.sfm_model.eval() self.sfm_model.eval()
x_values = x_test.values
sample_num = x_values.shape[0]
preds = []
with torch.no_grad(): for begin in range(sample_num)[::self.batch_size]:
if self.device != "cpu": if sample_num-begin<self.batch_size:
preds = self.sfm_model(x_test).detach().cpu().numpy() end = sample_num
else: else:
preds = self.sfm_model(x_test).detach().numpy() end = begin + self.batch_size
return pd.Series(preds, index=index)
x_batch = torch.from_numpy(x_values[begin:end]).float()
if self.device != 'cpu':
x_batch = x_batch.to(self.device)
with torch.no_grad():
if self.device != 'cpu':
pred = self.sfm_model(x_batch).detach().cpu().numpy()
else:
pred = self.sfm_model(x_batch).detach().cpu().numpy()
preds.append(pred)
return pd.Series(np.concatenate(preds), index=index)
def save(self, filename, **kwargs): def save(self, filename, **kwargs):
with save_multiple_parts_file(filename) as model_dir: with save_multiple_parts_file(filename) as model_dir:
@@ -443,10 +461,8 @@ 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()