{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":9718828,"sourceType":"datasetVersion","datasetId":5932329}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport polars as pl\nimport math\n\nimport os\nimport re\nfrom tqdm import tqdm\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\n\nfrom sklearn.model_selection import train_test_split\n\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-25T09:21:50.236822Z","iopub.execute_input":"2024-10-25T09:21:50.237189Z","iopub.status.idle":"2024-10-25T09:21:55.471162Z","shell.execute_reply.started":"2024-10-25T09:21:50.237154Z","shell.execute_reply":"2024-10-25T09:21:55.470195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Encoder(nn.Module):\n    def __init__(self):\n        super(Encoder, self).__init__()\n    \n        self.attn = torch.nn.MultiheadAttention(embed_dim = 64*2,\n                                                num_heads=4,\n                                                dropout=0.1,\n                                                batch_first=True)\n        \n        self.linear1 = nn.Linear(in_features = 64*2,out_features = 128*2)\n        self.linear2 = nn.Linear(in_features = 128*2,out_features = 64*2)\n        \n        self.norm1 = nn.LayerNorm(normalized_shape = 64*2)\n        self.norm2 = nn.LayerNorm(normalized_shape = 64*2)\n        \n        self.dropout1 = torch.nn.Dropout(0.2)\n        self.dropout2 = torch.nn.Dropout(0.2)\n        self.dropout3 = torch.nn.Dropout(0.2)\n        \n    def forward(self,x,attn_mask=None,key_padding_mask=None):\n\n        x_tmp = self.norm1(x)\n        \n        x_tmp, _ = self.attn(query=x_tmp,\n                             key=x_tmp,\n                             value=x_tmp,\n                             attn_mask=attn_mask,\n                             key_padding_mask=key_padding_mask)\n\n            \n        x_tmp = self.dropout1(x_tmp)\n        x = x + x_tmp    \n        x_tmp = self.norm2(x) \n        x_tmp = self.linear1(x_tmp)\n        x_tmp = F.relu(x_tmp)\n            \n        x_tmp = self.dropout2(x_tmp)\n        x_tmp = self.linear2(x_tmp)\n        x_tmp = self.dropout3(x_tmp)\n        x = x + x_tmp\n            \n        return x\n    \n    \nclass PredTimeSeries(nn.Module):\n    def __init__(self,feat_dim,tgt_dim,latent_dim = 64*2):\n        super(PredTimeSeries, self).__init__()\n        \n        self.prenet = nn.Linear(in_features = feat_dim,out_features = 64*2)\n        \n        self.positional_encoding = nn.Parameter(torch.randn(1, 100, 64*2))\n        self.encoder1 = Encoder()\n        self.encoder2 = Encoder()\n\n        \n        self.fc1 = nn.Linear(64*2,32*2)\n        self.fc2 = nn.Linear(32*2,16*2)\n        self.fc3 = nn.Linear(16*2,tgt_dim)\n        \n        self.norm1 = nn.LayerNorm(32*2)\n        self.norm2 = nn.LayerNorm(16*2)\n\n        \n        self.drop1 = nn.Dropout(0.2)\n        self.drop2 = nn.Dropout(0.2)\n\n        \n        \n    def forward(self,x):\n        _,seq_len,feat_d = x.shape\n        \n        x = self.prenet(x)\n\n        x = x + self.positional_encoding[:, :seq_len, :]\n        \n        x = self.encoder1(x)\n        x = self.encoder2(x)\n\n        x = self.fc1(x)\n        x = self.norm1(x)\n        x = F.relu(x)\n        x = self.drop1(x)\n        \n        x = self.fc2(x)\n        x = self.norm2(x)\n        x = F.relu(x)\n        x = self.drop2(x)\n        \n        \n        x = self.fc3(x)\n\n            \n        return x\n\n    \n\nclass CustomWeightedR2Loss(nn.Module):\n    def __init__(self):\n        super(CustomWeightedR2Loss, self).__init__()\n\n    def forward(self, y_pred, y_true, weight):\n    \n        y_true_flat = y_true.flatten()\n        y_pred_flat = y_pred.flatten()\n        weight_flat = weight.flatten()\n\n \n        numerator = torch.sum(weight_flat * (y_true_flat - y_pred_flat) ** 2)\n        denominator = torch.sum(weight_flat * (y_true_flat) ** 2)\n\n        weighted_r2 = 1 - numerator / denominator\n        \n        return 1 - weighted_r2\n","metadata":{"execution":{"iopub.status.busy":"2024-10-25T09:22:29.253851Z","iopub.execute_input":"2024-10-25T09:22:29.254497Z","iopub.status.idle":"2024-10-25T09:22:29.275762Z","shell.execute_reply.started":"2024-10-25T09:22:29.254456Z","shell.execute_reply":"2024-10-25T09:22:29.274810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = PredTimeSeries( 79, 1)\nmodel.load_state_dict(torch.load('/kaggle/input/js-weight/model_final.pth',weights_only=True))\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-10-25T09:22:29.614617Z","iopub.execute_input":"2024-10-25T09:22:29.615621Z","iopub.status.idle":"2024-10-25T09:22:29.659313Z","shell.execute_reply.started":"2024-10-25T09:22:29.615569Z","shell.execute_reply":"2024-10-25T09:22:29.658375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(test,lags):\n    cols= [f'feature_0{i}' if i<10 else f'feature_{i}' for i in range(79)]\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    df = test[cols].to_pandas().fillna(3)\n\n    df_tensor = torch.FloatTensor(df.values).to(device)\n\n    model.eval()\n    test_preds=model(df_tensor.unsqueeze(1)).cpu().detach().numpy().flatten()\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2024-10-25T09:22:30.567070Z","iopub.execute_input":"2024-10-25T09:22:30.567783Z","iopub.status.idle":"2024-10-25T09:22:30.574524Z","shell.execute_reply.started":"2024-10-25T09:22:30.567742Z","shell.execute_reply":"2024-10-25T09:22:30.573543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"execution":{"iopub.status.busy":"2024-10-25T09:22:31.460156Z","iopub.execute_input":"2024-10-25T09:22:31.460881Z","iopub.status.idle":"2024-10-25T09:22:32.062005Z","shell.execute_reply.started":"2024-10-25T09:22:31.460842Z","shell.execute_reply":"2024-10-25T09:22:32.060992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}