mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 08:16:54 +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"
|
region: "cn"
|
||||||
|
|
||||||
- `provider_uri`
|
- `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`
|
- `region`
|
||||||
- If region == ``qlib.config.REG_CN``, 'qlib' will be initialized in US-stock mode.
|
- If `region` == "us", ``Qlib`` will be initialized in US-stock mode.
|
||||||
- If region == ``qlib.config.REG_US``, 'qlib' will be initialized in china-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>`_.
|
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_US``: US stock market.
|
||||||
- ``qlib.config.REG_CN``: China 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`
|
- `redis_host`
|
||||||
Type: str, optional parameter(default: "127.0.0.1"), host of `redis`
|
Type: str, optional parameter(default: "127.0.0.1"), host of `redis`
|
||||||
The lock and cache mechanism relies on 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
|
# when testing, please modify the following parameters according to the specific environment
|
||||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||||
region: "cn"
|
region: "cn"
|
||||||
|
redis_port: 1222
|
||||||
|
|||||||
@@ -55,3 +55,4 @@ qlib_data:
|
|||||||
# when testing, please modify the following parameters according to the specific environment
|
# when testing, please modify the following parameters according to the specific environment
|
||||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||||
region: "cn"
|
region: "cn"
|
||||||
|
redis_port: 1222
|
||||||
|
|||||||
@@ -133,11 +133,14 @@ class DNNModelPytorch(Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self._fitted = False
|
self._fitted = False
|
||||||
self.dnn_model.cuda()
|
self.use_gpu = torch.cuda.is_available()
|
||||||
|
|
||||||
# set the visible GPU
|
if self.use_gpu:
|
||||||
if self.visible_GPU:
|
self.dnn_model.cuda()
|
||||||
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
|
|
||||||
|
# set the visible GPU
|
||||||
|
if self.visible_GPU:
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
|
||||||
|
|
||||||
def fit(
|
def fit(
|
||||||
self,
|
self,
|
||||||
@@ -175,13 +178,14 @@ class DNNModelPytorch(Model):
|
|||||||
train_num = y_train_values.shape[0]
|
train_num = y_train_values.shape[0]
|
||||||
|
|
||||||
# prepare validation data
|
# prepare validation data
|
||||||
x_val_cuda = torch.from_numpy(x_valid.values).float()
|
x_val_auto = torch.from_numpy(x_valid.values).float()
|
||||||
y_val_cuda = torch.from_numpy(y_valid.values).float()
|
y_val_auto = torch.from_numpy(y_valid.values).float()
|
||||||
w_val_cuda = torch.from_numpy(w_valid.values).float()
|
w_val_auto = torch.from_numpy(w_valid.values).float()
|
||||||
|
|
||||||
x_val_cuda = x_val_cuda.cuda()
|
if self.use_gpu:
|
||||||
y_val_cuda = y_val_cuda.cuda()
|
x_val_auto = x_val_auto.cuda()
|
||||||
w_val_cuda = w_val_cuda.cuda()
|
y_val_auto = y_val_auto.cuda()
|
||||||
|
w_val_auto = w_val_auto.cuda()
|
||||||
|
|
||||||
for step in range(self.max_steps):
|
for step in range(self.max_steps):
|
||||||
if stop_steps >= self.early_stop_rounds:
|
if stop_steps >= self.early_stop_rounds:
|
||||||
@@ -193,17 +197,18 @@ class DNNModelPytorch(Model):
|
|||||||
self.train_optimizer.zero_grad()
|
self.train_optimizer.zero_grad()
|
||||||
|
|
||||||
choice = np.random.choice(train_num, self.batch_size)
|
choice = np.random.choice(train_num, self.batch_size)
|
||||||
x_batch = x_train_values[choice]
|
x_batch_auto = x_train_values[choice]
|
||||||
y_batch = y_train_values[choice]
|
y_batch_auto = y_train_values[choice]
|
||||||
w_batch = w_train_values[choice]
|
w_batch_auto = w_train_values[choice]
|
||||||
|
|
||||||
x_batch_cuda = x_batch.float().cuda()
|
if self.use_gpu:
|
||||||
y_batch_cuda = y_batch.float().cuda()
|
x_batch_auto = x_batch_auto.float().cuda()
|
||||||
w_batch_cuda = w_batch.float().cuda()
|
y_batch_auto = y_batch_auto.float().cuda()
|
||||||
|
w_batch_auto = w_batch_auto.float().cuda()
|
||||||
|
|
||||||
# forward
|
# forward
|
||||||
preds = self.dnn_model(x_batch_cuda)
|
preds = self.dnn_model(x_batch_auto)
|
||||||
cur_loss = self.get_loss(preds, w_batch_cuda, y_batch_cuda, self.loss_type)
|
cur_loss = self.get_loss(preds, w_batch_auto, y_batch_auto, self.loss_type)
|
||||||
cur_loss.backward()
|
cur_loss.backward()
|
||||||
self.train_optimizer.step()
|
self.train_optimizer.step()
|
||||||
loss.update(cur_loss.item())
|
loss.update(cur_loss.item())
|
||||||
@@ -220,8 +225,8 @@ class DNNModelPytorch(Model):
|
|||||||
loss_val = AverageMeter()
|
loss_val = AverageMeter()
|
||||||
|
|
||||||
# forward
|
# forward
|
||||||
preds = self.dnn_model(x_val_cuda)
|
preds = self.dnn_model(x_val_auto)
|
||||||
cur_loss_val = self.get_loss(preds, w_val_cuda, y_val_cuda, self.loss_type)
|
cur_loss_val = self.get_loss(preds, w_val_auto, y_val_auto, self.loss_type)
|
||||||
loss_val.update(cur_loss_val.item())
|
loss_val.update(cur_loss_val.item())
|
||||||
if verbose:
|
if verbose:
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
@@ -245,7 +250,8 @@ class DNNModelPytorch(Model):
|
|||||||
|
|
||||||
# restore the optimal parameters after training ??
|
# restore the optimal parameters after training ??
|
||||||
self.dnn_model.load_state_dict(torch.load(save_path))
|
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):
|
def get_loss(self, pred, w, target, loss_type):
|
||||||
if loss_type == "mse":
|
if loss_type == "mse":
|
||||||
@@ -261,11 +267,16 @@ class DNNModelPytorch(Model):
|
|||||||
def predict(self, x_test):
|
def predict(self, x_test):
|
||||||
if not self._fitted:
|
if not self._fitted:
|
||||||
raise ValueError("model is not fitted yet!")
|
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()
|
self.dnn_model.eval()
|
||||||
|
|
||||||
with torch.no_grad():
|
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
|
return preds
|
||||||
|
|
||||||
def score(self, x_test, y_test, w_test=None):
|
def score(self, x_test, y_test, w_test=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user