mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-04 19:41:00 +08:00
Fix code with block.
This commit is contained in:
@@ -50,9 +50,7 @@ class CatBoostModel(Model):
|
||||
|
||||
# CatBoost needs 1D array as its label
|
||||
if y_train.values.ndim == 2 and y_train.values.shape[1] == 1:
|
||||
y_train_1d, y_valid_1d = np.squeeze(y_train.values), np.squeeze(
|
||||
y_valid.values
|
||||
)
|
||||
y_train_1d, y_valid_1d = np.squeeze(y_train.values), np.squeeze(y_valid.values)
|
||||
else:
|
||||
raise ValueError("CatBoost doesn't support multi-label training")
|
||||
|
||||
|
||||
@@ -124,9 +124,7 @@ class ALSTM(Model):
|
||||
elif optimizer.lower() == "gd":
|
||||
self.train_optimizer = optim.SGD(self.ALSTM_model.parameters(), lr=self.lr)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"optimizer {} is not supported!".format(optimizer)
|
||||
)
|
||||
raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
|
||||
|
||||
self._fitted = False
|
||||
if self.use_gpu:
|
||||
@@ -171,12 +169,8 @@ class ALSTM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
label = torch.from_numpy(
|
||||
y_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
feature = feature.cuda()
|
||||
@@ -208,9 +202,7 @@ class ALSTM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
@@ -320,9 +312,7 @@ class ALSTM(Model):
|
||||
|
||||
|
||||
class ALSTMModel(nn.Module):
|
||||
def __init__(
|
||||
self, d_feat=6, hidden_size=64, num_layers=2, dropout=0.0, rnn_type="GRU"
|
||||
):
|
||||
def __init__(self, d_feat=6, hidden_size=64, num_layers=2, dropout=0.0, rnn_type="GRU"):
|
||||
super().__init__()
|
||||
self.hid_size = hidden_size
|
||||
self.input_size = d_feat
|
||||
@@ -337,9 +327,7 @@ class ALSTMModel(nn.Module):
|
||||
except:
|
||||
raise ValueError("unknown rnn_type `%s`" % self.rnn_type)
|
||||
self.net = nn.Sequential()
|
||||
self.net.add_module(
|
||||
"fc_in", nn.Linear(in_features=self.input_size, out_features=self.hid_size)
|
||||
)
|
||||
self.net.add_module("fc_in", nn.Linear(in_features=self.input_size, out_features=self.hid_size))
|
||||
self.net.add_module("act", nn.Tanh())
|
||||
self.rnn = klass(
|
||||
input_size=self.hid_size,
|
||||
@@ -365,12 +353,8 @@ class ALSTMModel(nn.Module):
|
||||
def forward(self, inputs):
|
||||
# inputs: [batch_size, input_size*input_day]
|
||||
inputs = inputs.view(len(inputs), self.input_size, -1)
|
||||
inputs = inputs.permute(
|
||||
0, 2, 1
|
||||
) # [batch, input_size, seq_len] -> [batch, seq_len, input_size]
|
||||
rnn_out, _ = self.rnn(
|
||||
self.net(inputs)
|
||||
) # [batch, seq_len, num_directions * hidden_size]
|
||||
inputs = inputs.permute(0, 2, 1) # [batch, input_size, seq_len] -> [batch, seq_len, input_size]
|
||||
rnn_out, _ = self.rnn(self.net(inputs)) # [batch, seq_len, num_directions * hidden_size]
|
||||
attention_score = self.att_net(rnn_out) # [batch, seq_len, 1]
|
||||
out_att = torch.mul(rnn_out, attention_score)
|
||||
out_att = torch.sum(out_att, dim=1)
|
||||
|
||||
@@ -126,9 +126,7 @@ class GATs(Model):
|
||||
elif optimizer.lower() == "gd":
|
||||
self.train_optimizer = optim.SGD(self.GAT_model.parameters(), lr=self.lr)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"optimizer {} is not supported!".format(optimizer)
|
||||
)
|
||||
raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
|
||||
|
||||
self._fitted = False
|
||||
if self.use_gpu:
|
||||
@@ -258,22 +256,14 @@ class GATs(Model):
|
||||
self.logger.info("Loading pretrained model...")
|
||||
if self.base_model == "LSTM":
|
||||
pretrained_model = LSTMModel()
|
||||
pretrained_model.load_state_dict(
|
||||
torch.load("benchmarks/LSTM/model_lstm_csi300.pkl")
|
||||
)
|
||||
pretrained_model.load_state_dict(torch.load("benchmarks/LSTM/model_lstm_csi300.pkl"))
|
||||
|
||||
elif self.base_model == "GRU":
|
||||
pretrained_model = GRUModel()
|
||||
pretrained_model.load_state_dict(
|
||||
torch.load("benchmarks/GRU/model_gru_csi300.pkl")
|
||||
)
|
||||
pretrained_model.load_state_dict(torch.load("benchmarks/GRU/model_gru_csi300.pkl"))
|
||||
|
||||
model_dict = self.GAT_model.state_dict()
|
||||
pretrained_dict = {
|
||||
k: v
|
||||
for k, v in pretrained_model.state_dict().items()
|
||||
if k in model_dict
|
||||
}
|
||||
pretrained_dict = {k: v for k, v in pretrained_model.state_dict().items() if k in model_dict}
|
||||
model_dict.update(pretrained_dict)
|
||||
self.GAT_model.load_state_dict(model_dict)
|
||||
self.logger.info("Loading pretrained model Done...")
|
||||
@@ -343,9 +333,7 @@ class GATs(Model):
|
||||
|
||||
|
||||
class GATModel(nn.Module):
|
||||
def __init__(
|
||||
self, d_feat=6, hidden_size=64, num_layers=2, dropout=0.0, base_model="GRU"
|
||||
):
|
||||
def __init__(self, d_feat=6, hidden_size=64, num_layers=2, dropout=0.0, base_model="GRU"):
|
||||
super().__init__()
|
||||
|
||||
if base_model == "GRU":
|
||||
|
||||
@@ -124,9 +124,7 @@ class GRU(Model):
|
||||
elif optimizer.lower() == "gd":
|
||||
self.train_optimizer = optim.SGD(self.gru_model.parameters(), lr=self.lr)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"optimizer {} is not supported!".format(optimizer)
|
||||
)
|
||||
raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
|
||||
|
||||
self._fitted = False
|
||||
if self.use_gpu:
|
||||
@@ -171,12 +169,8 @@ class GRU(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
label = torch.from_numpy(
|
||||
y_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
feature = feature.cuda()
|
||||
@@ -208,9 +202,7 @@ class GRU(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
|
||||
@@ -124,9 +124,7 @@ class LSTM(Model):
|
||||
elif optimizer.lower() == "gd":
|
||||
self.train_optimizer = optim.SGD(self.lstm_model.parameters(), lr=self.lr)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"optimizer {} is not supported!".format(optimizer)
|
||||
)
|
||||
raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
|
||||
|
||||
self._fitted = False
|
||||
if self.use_gpu:
|
||||
@@ -171,12 +169,8 @@ class LSTM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
label = torch.from_numpy(
|
||||
y_train_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
feature = feature.cuda()
|
||||
@@ -208,9 +202,7 @@ class LSTM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = torch.from_numpy(
|
||||
x_values[indices[i : i + self.batch_size]]
|
||||
).float()
|
||||
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float()
|
||||
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float()
|
||||
|
||||
if self.use_gpu:
|
||||
|
||||
@@ -56,52 +56,30 @@ class SFM_Model(nn.Module):
|
||||
self.hidden_dim = hidden_size
|
||||
self.device = device
|
||||
|
||||
self.W_i = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty((self.input_dim, self.hidden_dim)))
|
||||
)
|
||||
self.U_i = nn.Parameter(
|
||||
init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim))
|
||||
)
|
||||
self.W_i = nn.Parameter(init.xavier_uniform_(torch.empty((self.input_dim, self.hidden_dim))))
|
||||
self.U_i = nn.Parameter(init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim)))
|
||||
self.b_i = nn.Parameter(torch.zeros(self.hidden_dim))
|
||||
|
||||
self.W_ste = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim))
|
||||
)
|
||||
self.U_ste = nn.Parameter(
|
||||
init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim))
|
||||
)
|
||||
self.W_ste = nn.Parameter(init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim)))
|
||||
self.U_ste = nn.Parameter(init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim)))
|
||||
self.b_ste = nn.Parameter(torch.ones(self.hidden_dim))
|
||||
|
||||
self.W_fre = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty(self.input_dim, self.freq_dim))
|
||||
)
|
||||
self.U_fre = nn.Parameter(
|
||||
init.orthogonal_(torch.empty(self.hidden_dim, self.freq_dim))
|
||||
)
|
||||
self.W_fre = nn.Parameter(init.xavier_uniform_(torch.empty(self.input_dim, self.freq_dim)))
|
||||
self.U_fre = nn.Parameter(init.orthogonal_(torch.empty(self.hidden_dim, self.freq_dim)))
|
||||
self.b_fre = nn.Parameter(torch.ones(self.freq_dim))
|
||||
|
||||
self.W_c = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim))
|
||||
)
|
||||
self.U_c = nn.Parameter(
|
||||
init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim))
|
||||
)
|
||||
self.W_c = nn.Parameter(init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim)))
|
||||
self.U_c = nn.Parameter(init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim)))
|
||||
self.b_c = nn.Parameter(torch.zeros(self.hidden_dim))
|
||||
|
||||
self.W_o = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim))
|
||||
)
|
||||
self.U_o = nn.Parameter(
|
||||
init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim))
|
||||
)
|
||||
self.W_o = nn.Parameter(init.xavier_uniform_(torch.empty(self.input_dim, self.hidden_dim)))
|
||||
self.U_o = nn.Parameter(init.orthogonal_(torch.empty(self.hidden_dim, self.hidden_dim)))
|
||||
self.b_o = nn.Parameter(torch.zeros(self.hidden_dim))
|
||||
|
||||
self.U_a = nn.Parameter(init.orthogonal_(torch.empty(self.freq_dim, 1)))
|
||||
self.b_a = nn.Parameter(torch.zeros(self.hidden_dim))
|
||||
|
||||
self.W_p = nn.Parameter(
|
||||
init.xavier_uniform_(torch.empty(self.hidden_dim, self.output_dim))
|
||||
)
|
||||
self.W_p = nn.Parameter(init.xavier_uniform_(torch.empty(self.hidden_dim, self.output_dim)))
|
||||
self.b_p = nn.Parameter(torch.zeros(self.output_dim))
|
||||
|
||||
self.activation = nn.Tanh()
|
||||
@@ -137,12 +115,8 @@ class SFM_Model(nn.Module):
|
||||
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))
|
||||
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)
|
||||
)
|
||||
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))
|
||||
|
||||
ste = torch.reshape(ste, (-1, self.hidden_dim, 1))
|
||||
fre = torch.reshape(fre, (-1, 1, self.freq_dim))
|
||||
@@ -331,9 +305,7 @@ class SFM(Model):
|
||||
elif optimizer.lower() == "gd":
|
||||
self.train_optimizer = optim.SGD(self.sfm_model.parameters(), lr=self.lr)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"optimizer {} is not supported!".format(optimizer)
|
||||
)
|
||||
raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
|
||||
|
||||
self._fitted = False
|
||||
self.sfm_model.to(self.device)
|
||||
@@ -356,16 +328,8 @@ class SFM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = (
|
||||
torch.from_numpy(x_values[indices[i : i + self.batch_size]])
|
||||
.float()
|
||||
.to(self.device)
|
||||
)
|
||||
label = (
|
||||
torch.from_numpy(y_values[indices[i : i + self.batch_size]])
|
||||
.float()
|
||||
.to(self.device)
|
||||
)
|
||||
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
|
||||
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
|
||||
|
||||
pred = self.sfm_model(feature)
|
||||
loss = self.loss_fn(pred, label)
|
||||
@@ -391,16 +355,8 @@ class SFM(Model):
|
||||
if len(indices) - i < self.batch_size:
|
||||
break
|
||||
|
||||
feature = (
|
||||
torch.from_numpy(x_train_values[indices[i : i + self.batch_size]])
|
||||
.float()
|
||||
.to(self.device)
|
||||
)
|
||||
label = (
|
||||
torch.from_numpy(y_train_values[indices[i : i + self.batch_size]])
|
||||
.float()
|
||||
.to(self.device)
|
||||
)
|
||||
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
|
||||
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
|
||||
|
||||
pred = self.sfm_model(feature)
|
||||
loss = self.loss_fn(pred, label)
|
||||
|
||||
@@ -47,9 +47,7 @@ class XGBModel(Model):
|
||||
|
||||
# Lightgbm need 1D array as its label
|
||||
if y_train.values.ndim == 2 and y_train.values.shape[1] == 1:
|
||||
y_train_1d, y_valid_1d = np.squeeze(y_train.values), np.squeeze(
|
||||
y_valid.values
|
||||
)
|
||||
y_train_1d, y_valid_1d = np.squeeze(y_train.values), np.squeeze(y_valid.values)
|
||||
else:
|
||||
raise ValueError("XGBoost doesn't support multi-label training")
|
||||
|
||||
@@ -72,6 +70,4 @@ class XGBModel(Model):
|
||||
if self.model is None:
|
||||
raise ValueError("model is not fitted yet!")
|
||||
x_test = dataset.prepare("test", col_set="feature")
|
||||
return pd.Series(
|
||||
self.model.predict(xgb.DMatrix(x_test.values)), index=x_test.index
|
||||
)
|
||||
return pd.Series(self.model.predict(xgb.DMatrix(x_test.values)), index=x_test.index)
|
||||
|
||||
Reference in New Issue
Block a user