mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-02 18:40:58 +08:00
make dnn model compatible with CPU & update docs for init param
This commit is contained in:
@@ -574,10 +574,24 @@ The `qlib_data` field describes the parameters of qlib initialization.
|
||||
region: "cn"
|
||||
|
||||
- `provider_uri`
|
||||
The local directory where the data loaded by 'get_data.py' is stored.
|
||||
Type: str. The URI of the Qlib data. For example, it could be the location where the data loaded by ``get_data.py`` are stored.
|
||||
- `region`
|
||||
- If region == ``qlib.config.REG_CN``, 'qlib' will be initialized in US-stock mode.
|
||||
- If region == ``qlib.config.REG_US``, 'qlib' will be initialized in china-stock mode.
|
||||
- If `region` == "us", ``Qlib`` will be initialized in US-stock mode.
|
||||
- If `region` == "cn", ``Qlib`` will be initialized in china-stock mode.
|
||||
- `redis_host`
|
||||
Type: str, optional parameter(default: "127.0.0.1"), host of `redis`
|
||||
The lock and cache mechanism relies on redis.
|
||||
- `redis_port`
|
||||
Type: int, optional parameter(default: 6379), port of `redis`
|
||||
|
||||
.. note::
|
||||
|
||||
The value of `region` should be aligned with the data stored in `provider_uri`. Currently, ``scripts/get_data.py`` only provides China stock market data. If users want to use the US stock market data, they should prepare their own US-stock data in `provider_uri` and switch to US-stock mode.
|
||||
|
||||
.. note::
|
||||
|
||||
If Qlib fails to connect redis via `redis_host` and `redis_port`, cache mechanism will not be used! Please refer to `Cache <data.html#cache>`_ for details.
|
||||
|
||||
|
||||
Please refer to `Initialization <../start/initialization.html>`_.
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ Besides `provider_uri` and `region`, `qlib.init` has other parameters. The follo
|
||||
- ``qlib.config.REG_US``: US stock market.
|
||||
- ``qlib.config.REG_CN``: China stock market.
|
||||
|
||||
Different modse will result in different trading limitations and costs.
|
||||
Different modes will result in different trading limitations and costs.
|
||||
- `redis_host`
|
||||
Type: str, optional parameter(default: "127.0.0.1"), host of `redis`
|
||||
The lock and cache mechanism relies on redis.
|
||||
|
||||
@@ -51,3 +51,4 @@ qlib_data:
|
||||
# when testing, please modify the following parameters according to the specific environment
|
||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||
region: "cn"
|
||||
redis_port: 1222
|
||||
|
||||
@@ -55,3 +55,4 @@ qlib_data:
|
||||
# when testing, please modify the following parameters according to the specific environment
|
||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||
region: "cn"
|
||||
redis_port: 1222
|
||||
|
||||
@@ -133,11 +133,14 @@ class DNNModelPytorch(Model):
|
||||
)
|
||||
|
||||
self._fitted = False
|
||||
self.dnn_model.cuda()
|
||||
self.use_gpu = torch.cuda.is_available()
|
||||
|
||||
# set the visible GPU
|
||||
if self.visible_GPU:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
|
||||
if self.use_gpu:
|
||||
self.dnn_model.cuda()
|
||||
|
||||
# set the visible GPU
|
||||
if self.visible_GPU:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
|
||||
|
||||
def fit(
|
||||
self,
|
||||
@@ -175,13 +178,14 @@ class DNNModelPytorch(Model):
|
||||
train_num = y_train_values.shape[0]
|
||||
|
||||
# prepare validation data
|
||||
x_val_cuda = torch.from_numpy(x_valid.values).float()
|
||||
y_val_cuda = torch.from_numpy(y_valid.values).float()
|
||||
w_val_cuda = torch.from_numpy(w_valid.values).float()
|
||||
x_val_auto = torch.from_numpy(x_valid.values).float()
|
||||
y_val_auto = torch.from_numpy(y_valid.values).float()
|
||||
w_val_auto = torch.from_numpy(w_valid.values).float()
|
||||
|
||||
x_val_cuda = x_val_cuda.cuda()
|
||||
y_val_cuda = y_val_cuda.cuda()
|
||||
w_val_cuda = w_val_cuda.cuda()
|
||||
if self.use_gpu:
|
||||
x_val_auto = x_val_auto.cuda()
|
||||
y_val_auto = y_val_auto.cuda()
|
||||
w_val_auto = w_val_auto.cuda()
|
||||
|
||||
for step in range(self.max_steps):
|
||||
if stop_steps >= self.early_stop_rounds:
|
||||
@@ -193,17 +197,18 @@ class DNNModelPytorch(Model):
|
||||
self.train_optimizer.zero_grad()
|
||||
|
||||
choice = np.random.choice(train_num, self.batch_size)
|
||||
x_batch = x_train_values[choice]
|
||||
y_batch = y_train_values[choice]
|
||||
w_batch = w_train_values[choice]
|
||||
x_batch_auto = x_train_values[choice]
|
||||
y_batch_auto = y_train_values[choice]
|
||||
w_batch_auto = w_train_values[choice]
|
||||
|
||||
x_batch_cuda = x_batch.float().cuda()
|
||||
y_batch_cuda = y_batch.float().cuda()
|
||||
w_batch_cuda = w_batch.float().cuda()
|
||||
if self.use_gpu:
|
||||
x_batch_auto = x_batch_auto.float().cuda()
|
||||
y_batch_auto = y_batch_auto.float().cuda()
|
||||
w_batch_auto = w_batch_auto.float().cuda()
|
||||
|
||||
# forward
|
||||
preds = self.dnn_model(x_batch_cuda)
|
||||
cur_loss = self.get_loss(preds, w_batch_cuda, y_batch_cuda, self.loss_type)
|
||||
preds = self.dnn_model(x_batch_auto)
|
||||
cur_loss = self.get_loss(preds, w_batch_auto, y_batch_auto, self.loss_type)
|
||||
cur_loss.backward()
|
||||
self.train_optimizer.step()
|
||||
loss.update(cur_loss.item())
|
||||
@@ -220,8 +225,8 @@ class DNNModelPytorch(Model):
|
||||
loss_val = AverageMeter()
|
||||
|
||||
# forward
|
||||
preds = self.dnn_model(x_val_cuda)
|
||||
cur_loss_val = self.get_loss(preds, w_val_cuda, y_val_cuda, self.loss_type)
|
||||
preds = self.dnn_model(x_val_auto)
|
||||
cur_loss_val = self.get_loss(preds, w_val_auto, y_val_auto, self.loss_type)
|
||||
loss_val.update(cur_loss_val.item())
|
||||
if verbose:
|
||||
self.logger.info(
|
||||
@@ -245,7 +250,8 @@ class DNNModelPytorch(Model):
|
||||
|
||||
# restore the optimal parameters after training ??
|
||||
self.dnn_model.load_state_dict(torch.load(save_path))
|
||||
torch.cuda.empty_cache()
|
||||
if self.use_gpu:
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
def get_loss(self, pred, w, target, loss_type):
|
||||
if loss_type == "mse":
|
||||
@@ -261,11 +267,16 @@ class DNNModelPytorch(Model):
|
||||
def predict(self, x_test):
|
||||
if not self._fitted:
|
||||
raise ValueError("model is not fitted yet!")
|
||||
x_test = torch.from_numpy(x_test.values).float().cuda()
|
||||
x_test = torch.from_numpy(x_test.values).float()
|
||||
if self.use_gpu:
|
||||
x_test = x_test.cuda()
|
||||
self.dnn_model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
preds = self.dnn_model(x_test).detach().cpu().numpy()
|
||||
if self.use_gpu:
|
||||
preds = self.dnn_model(x_test).detach().cpu().numpy()
|
||||
else:
|
||||
preds = self.dnn_model(x_test).detach().numpy()
|
||||
return preds
|
||||
|
||||
def score(self, x_test, y_test, w_test=None):
|
||||
|
||||
Reference in New Issue
Block a user