Fix bugs in the new models
This commit is contained in:
		 Submodule .latent-data/qlib updated: 419629e4d2...ba56e4071e
									
								
							| @@ -47,13 +47,12 @@ task: | ||||
|             net_config: | ||||
|                 name: basic | ||||
|                 d_feat: 6 | ||||
|                 stem_dim: 48 | ||||
|                 embed_dims: [48, 48, 48, 48, 48] | ||||
|                 embed_dim: 48 | ||||
|                 num_heads: [4, 4, 4, 4, 4] | ||||
|                 mlp_hidden_multipliers: [4, 4, 4, 4, 4] | ||||
|                 qkv_bias: True | ||||
|                 pos_drop: 0.1 | ||||
|                 other_drop: 0.1 | ||||
|                 other_drop: 0 | ||||
|             opt_config: | ||||
|             loss: mse | ||||
|             GPU: 0 | ||||
|   | ||||
| @@ -45,7 +45,6 @@ DEFAULT_NET_CONFIG = None | ||||
| _default_max_depth = 5 | ||||
| DefaultSearchSpace = dict( | ||||
|     d_feat=6, | ||||
|     stem_dim=spaces.Categorical(*_get_list_mul(8, 16)), | ||||
|     embed_dim=spaces.Categorical(*_get_list_mul(8, 16)), | ||||
|     num_heads=_get_mul_specs((1, 2, 4, 8), _default_max_depth), | ||||
|     mlp_hidden_multipliers=_get_mul_specs((0.5, 1, 2, 4, 8), _default_max_depth), | ||||
| @@ -61,7 +60,6 @@ class SuperTransformer(super_core.SuperModule): | ||||
|     def __init__( | ||||
|         self, | ||||
|         d_feat: int = 6, | ||||
|         stem_dim: super_core.IntSpaceType = DefaultSearchSpace["stem_dim"], | ||||
|         embed_dim: List[super_core.IntSpaceType] = DefaultSearchSpace["embed_dim"], | ||||
|         num_heads: List[super_core.IntSpaceType] = DefaultSearchSpace["num_heads"], | ||||
|         mlp_hidden_multipliers: List[super_core.IntSpaceType] = DefaultSearchSpace[ | ||||
| @@ -74,15 +72,14 @@ class SuperTransformer(super_core.SuperModule): | ||||
|     ): | ||||
|         super(SuperTransformer, self).__init__() | ||||
|         self._embed_dim = embed_dim | ||||
|         self._stem_dim = stem_dim | ||||
|         self._num_heads = num_heads | ||||
|         self._mlp_hidden_multipliers = mlp_hidden_multipliers | ||||
|  | ||||
|         # the stem part | ||||
|         self.input_embed = super_core.SuperAlphaEBDv1(d_feat, stem_dim) | ||||
|         self.cls_token = nn.Parameter(torch.zeros(1, 1, self.stem_dim)) | ||||
|         self.input_embed = super_core.SuperAlphaEBDv1(d_feat, embed_dim) | ||||
|         self.cls_token = nn.Parameter(torch.zeros(1, 1, self.embed_dim)) | ||||
|         self.pos_embed = super_core.SuperPositionalEncoder( | ||||
|             d_model=stem_dim, max_seq_len=max_seq_len, dropout=pos_drop | ||||
|             d_model=embed_dim, max_seq_len=max_seq_len, dropout=pos_drop | ||||
|         ) | ||||
|         # build the transformer encode layers -->> check params | ||||
|         _assert_types(num_heads, (tuple, list)) | ||||
| @@ -111,15 +108,13 @@ class SuperTransformer(super_core.SuperModule): | ||||
|         self.apply(self._init_weights) | ||||
|  | ||||
|     @property | ||||
|     def stem_dim(self): | ||||
|         return spaces.get_max(self._stem_dim) | ||||
|     def embed_dim(self): | ||||
|         return spaces.get_max(self._embed_dim) | ||||
|  | ||||
|     @property | ||||
|     def abstract_search_space(self): | ||||
|         root_node = spaces.VirtualNode(id(self)) | ||||
|         if not spaces.is_determined(self._stem_dim): | ||||
|             root_node.append("_stem_dim", self._stem_dim.abstract(reuse_last=True)) | ||||
|         if not spaces.is_determined(self._stem_dim): | ||||
|         if not spaces.is_determined(self._embed_dim): | ||||
|             root_node.append("_embed_dim", self._embed_dim.abstract(reuse_last=True)) | ||||
|         xdict = dict( | ||||
|             input_embed=self.input_embed.abstract_search_space, | ||||
| @@ -155,13 +150,13 @@ class SuperTransformer(super_core.SuperModule): | ||||
|     def forward_candidate(self, input: torch.Tensor) -> torch.Tensor: | ||||
|         batch, flatten_size = input.shape | ||||
|         feats = self.input_embed(input)  # batch * 60 * 64 | ||||
|         if not spaces.is_determined(self._stem_dim): | ||||
|             stem_dim = self.abstract_child["_stem_dim"].value | ||||
|         if not spaces.is_determined(self._embed_dim): | ||||
|             embed_dim = self.abstract_child["_embed_dim"].value | ||||
|         else: | ||||
|             stem_dim = spaces.get_determined_value(self._stem_dim) | ||||
|             embed_dim = spaces.get_determined_value(self._embed_dim) | ||||
|         cls_tokens = self.cls_token.expand(batch, -1, -1) | ||||
|         cls_tokens = F.interpolate( | ||||
|             cls_tokens, size=(stem_dim), mode="linear", align_corners=True | ||||
|             cls_tokens, size=(embed_dim), mode="linear", align_corners=True | ||||
|         ) | ||||
|         feats_w_ct = torch.cat((cls_tokens, feats), dim=1) | ||||
|         feats_w_tp = self.pos_embed(feats_w_ct) | ||||
| @@ -191,7 +186,6 @@ def get_transformer(config): | ||||
|     if name == "basic": | ||||
|         model = SuperTransformer( | ||||
|             d_feat=config.get("d_feat"), | ||||
|             stem_dim=config.get("stem_dim"), | ||||
|             embed_dim=config.get("embed_dim"), | ||||
|             num_heads=config.get("num_heads"), | ||||
|             mlp_hidden_multipliers=config.get("mlp_hidden_multipliers"), | ||||
|   | ||||
| @@ -2,17 +2,42 @@ | ||||
|  "cells": [ | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 1, | ||||
|    "execution_count": null, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "name": "stdout", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "library path: /Users/xuanyidong/Desktop/XAutoDL/lib\n" | ||||
|      ] | ||||
|     }, | ||||
|     { | ||||
|      "name": "stderr", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "[95290:MainThread](2021-03-03 12:14:32,922) INFO - qlib.Initialization - [config.py:276] - default_conf: client.\n", | ||||
|       "[95290:MainThread](2021-03-03 12:14:32,925) WARNING - qlib.Initialization - [config.py:291] - redis connection failed(host=127.0.0.1 port=6379), cache will not be used!\n", | ||||
|       "[95290:MainThread](2021-03-03 12:14:33,203) INFO - qlib.Initialization - [__init__.py:46] - qlib successfully initialized based on client settings.\n", | ||||
|       "[95290:MainThread](2021-03-03 12:14:33,205) INFO - qlib.Initialization - [__init__.py:47] - data_path=/Users/xuanyidong/.qlib/qlib_data/cn_data\n" | ||||
|       "[61704:MainThread](2021-03-22 13:56:38,104) INFO - qlib.Initialization - [config.py:276] - default_conf: client.\n", | ||||
|       "[61704:MainThread](2021-03-22 13:56:38,106) WARNING - qlib.Initialization - [config.py:291] - redis connection failed(host=127.0.0.1 port=6379), cache will not be used!\n", | ||||
|       "[61704:MainThread](2021-03-22 13:56:38,680) INFO - qlib.Initialization - [__init__.py:46] - qlib successfully initialized based on client settings.\n", | ||||
|       "[61704:MainThread](2021-03-22 13:56:38,681) INFO - qlib.Initialization - [__init__.py:47] - data_path=/Users/xuanyidong/.qlib/qlib_data/cn_data\n" | ||||
|      ] | ||||
|     }, | ||||
|     { | ||||
|      "name": "stdout", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "{'class': 'DatasetH',\n", | ||||
|       " 'kwargs': {'handler': {'class': 'Alpha158',\n", | ||||
|       "                        'kwargs': {'end_time': '2020-08-01',\n", | ||||
|       "                                   'fit_end_time': '2014-12-31',\n", | ||||
|       "                                   'fit_start_time': '2008-01-01',\n", | ||||
|       "                                   'instruments': 'csi100',\n", | ||||
|       "                                   'start_time': '2008-01-01'},\n", | ||||
|       "                        'module_path': 'qlib.contrib.data.handler'},\n", | ||||
|       "            'segments': {'test': ('2017-01-01', '2020-08-01'),\n", | ||||
|       "                         'train': ('2008-01-01', '2014-12-31'),\n", | ||||
|       "                         'valid': ('2015-01-01', '2016-12-31')}},\n", | ||||
|       " 'module_path': 'qlib.data.dataset'}\n" | ||||
|      ] | ||||
|     } | ||||
|    ], | ||||
| @@ -24,6 +49,16 @@ | ||||
|     "import numpy as np\n", | ||||
|     "import pandas as pd\n", | ||||
|     "\n", | ||||
|     "from pathlib import Path\n", | ||||
|     "\n", | ||||
|     "__file__ = os.path.dirname(os.path.realpath(\"__file__\"))\n", | ||||
|     "\n", | ||||
|     "lib_dir = (Path(__file__).parent / \"..\" / \"lib\").resolve()\n", | ||||
|     "print(\"library path: {:}\".format(lib_dir))\n", | ||||
|     "assert lib_dir.exists(), \"{:} does not exist\".format(lib_dir)\n", | ||||
|     "if str(lib_dir) not in sys.path:\n", | ||||
|     "    sys.path.insert(0, str(lib_dir))\n", | ||||
|     "\n", | ||||
|     "from qlib import config as qconfig\n", | ||||
|     "from qlib.utils import init_instance_by_config\n", | ||||
|     "\n", | ||||
| @@ -41,7 +76,7 @@ | ||||
|     "                        \"end_time\": \"2020-08-01\",\n", | ||||
|     "                        \"fit_start_time\": \"2008-01-01\",\n", | ||||
|     "                        \"fit_end_time\": \"2014-12-31\",\n", | ||||
|     "                        \"instruments\": \"csi300\",\n", | ||||
|     "                        \"instruments\": \"csi100\",\n", | ||||
|     "                    },\n", | ||||
|     "                },\n", | ||||
|     "                \"segments\": {\n", | ||||
| @@ -50,7 +85,15 @@ | ||||
|     "                    \"test\": (\"2017-01-01\", \"2020-08-01\"),\n", | ||||
|     "                },\n", | ||||
|     "            },\n", | ||||
|     "        }" | ||||
|     "        }\n", | ||||
|     "pprint.pprint(dataset_config)\n", | ||||
|     "dataset = init_instance_by_config(dataset_config)\n", | ||||
|     "\n", | ||||
|     "df_train, df_valid, df_test = dataset.prepare(\n", | ||||
|     "            [\"train\", \"valid\", \"test\"],\n", | ||||
|     "            col_set=[\"feature\", \"label\"],\n", | ||||
|     "            data_key=DataHandlerLP.DK_L,\n", | ||||
|     "        )" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
| @@ -89,39 +132,10 @@ | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "pprint.pprint(dataset_config)\n", | ||||
|     "dataset = init_instance_by_config(dataset_config)" | ||||
|     "from trade_models.transformations import get_transformer\n", | ||||
|     "\n", | ||||
|     "model = get_transformer(None)" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 7, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "name": "stdout", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "{'class': 'MLflowExpManager', 'module_path': 'qlib.workflow.expm', 'kwargs': {'uri': 'file:/Users/xuanyidong/Desktop/AutoDL-Projects/notebooks/Q/mlruns', 'default_exp_name': 'Experiment'}}\n", | ||||
|       "Wrapper(provider=<qlib.workflow.QlibRecorder object at 0x7ff46b8a4850>)\n", | ||||
|       "<qlib.workflow.expm.MLflowExpManager object at 0x7ff46b8a4c10>\n" | ||||
|      ] | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "from qlib.workflow import R\n", | ||||
|     "from qlib.config import C\n", | ||||
|     "print(C.exp_manager)\n", | ||||
|     "print(R)\n", | ||||
|     "print(R.exp_manager)" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": null, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [] | ||||
|   } | ||||
|  ], | ||||
|  "metadata": { | ||||
| @@ -140,7 +154,7 @@ | ||||
|    "name": "python", | ||||
|    "nbconvert_exporter": "python", | ||||
|    "pygments_lexer": "ipython3", | ||||
|    "version": "3.8.3" | ||||
|    "version": "3.8.8" | ||||
|   } | ||||
|  }, | ||||
|  "nbformat": 4, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user