{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":7072203,"sourceType":"datasetVersion","datasetId":4072938},{"sourceId":151346476,"sourceType":"kernelVersion"},{"sourceId":151583600,"sourceType":"kernelVersion"},{"sourceId":153134190,"sourceType":"kernelVersion"}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pyarrow.parquet as pq\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom data_utilities import *\nfrom tqdm.notebook import tqdm\nimport metric\nfrom data_utilities import FeatureAugmentor","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:42:33.801626Z","iopub.execute_input":"2023-12-01T10:42:33.802304Z","iopub.status.idle":"2023-12-01T10:42:37.998421Z","shell.execute_reply.started":"2023-12-01T10:42:33.802276Z","shell.execute_reply":"2023-12-01T10:42:37.997484Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Setting Up the DataManager","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"transforms = SleepStatesTransformer().get_transform()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:42:38.000344Z","iopub.execute_input":"2023-12-01T10:42:38.000879Z","iopub.status.idle":"2023-12-01T10:42:38.005920Z","shell.execute_reply.started":"2023-12-01T10:42:38.000843Z","shell.execute_reply":"2023-12-01T10:42:38.005026Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"end, limit = get_split(ratio=0.8)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:42:38.007414Z","iopub.execute_input":"2023-12-01T10:42:38.007689Z","iopub.status.idle":"2023-12-01T10:43:37.603218Z","shell.execute_reply.started":"2023-12-01T10:42:38.007664Z","shell.execute_reply":"2023-12-01T10:43:37.602171Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_manager = SleepStatesTrainDataManager(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\", \"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\",transforms = transforms, end=end)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:43:37.605500Z","iopub.execute_input":"2023-12-01T10:43:37.605799Z","iopub.status.idle":"2023-12-01T10:45:57.693613Z","shell.execute_reply.started":"2023-12-01T10:43:37.605773Z","shell.execute_reply":"2023-12-01T10:45:57.692584Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_manager_val = SleepStatesValDataManager(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\", limit=limit, transforms=transforms)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:45:57.694987Z","iopub.execute_input":"2023-12-01T10:45:57.695376Z","iopub.status.idle":"2023-12-01T10:46:52.252311Z","shell.execute_reply.started":"2023-12-01T10:45:57.695333Z","shell.execute_reply":"2023-12-01T10:46:52.251481Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"### import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n  ","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:46:52.253530Z","iopub.execute_input":"2023-12-01T10:46:52.253834Z","iopub.status.idle":"2023-12-01T10:46:52.258185Z","shell.execute_reply.started":"2023-12-01T10:46:52.253799Z","shell.execute_reply":"2023-12-01T10:46:52.257197Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DEVICE=\"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:46:52.259401Z","iopub.execute_input":"2023-12-01T10:46:52.260005Z","iopub.status.idle":"2023-12-01T10:46:52.273499Z","shell.execute_reply.started":"2023-12-01T10:46:52.259971Z","shell.execute_reply":"2023-12-01T10:46:52.272614Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# LSTM MODEL","metadata":{}},{"cell_type":"code","source":"class LSTMModel(nn.Module):\n    def __init__(self, input_size,output_size,hidden_size, num_layers):\n        super(LSTMModel, self).__init__()\n        \n        self.hidden_size = hidden_size\n        self.num_layers = num_layers\n        \n        \n        self.lstm1 = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n        \n#         self.bn = nn.BatchNorm1d(hidden_size)\n        self.linear = nn.Linear(hidden_size, output_size)\n        \n    def forward(self, x):\n        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n        \n        out, _ = self.lstm1(x, (h0, c0))\n#         print(out.shape)\n#         feed out to linear\n        return self.linear(out)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:48:28.995739Z","iopub.execute_input":"2023-12-01T10:48:28.996442Z","iopub.status.idle":"2023-12-01T10:48:29.004070Z","shell.execute_reply.started":"2023-12-01T10:48:28.996409Z","shell.execute_reply":"2023-12-01T10:48:29.003059Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selections = [0,1,2]\ninput_size = len(selections)\n\nhidden_size= 16\nnum_layers= 3\n\noutput_size = 3\n\nmodel = LSTMModel(input_size, output_size, hidden_size, num_layers)\nmodel.to(DEVICE)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:48:29.112715Z","iopub.execute_input":"2023-12-01T10:48:29.113321Z","iopub.status.idle":"2023-12-01T10:48:29.123946Z","shell.execute_reply.started":"2023-12-01T10:48:29.113289Z","shell.execute_reply":"2023-12-01T10:48:29.123022Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Attention LSTM Model","metadata":{}},{"cell_type":"code","source":"class SelfAttention(nn.Module):\n    def __init__(self, input_dim, output_dim):\n#         query,key,value layers\n        super(SelfAttention, self).__init__()\n        self.input_dim = input_dim\n        self.output_dim = output_dim\n        \n        self.query_layer = nn.Linear(input_dim, output_dim)\n        self.key_layer = nn.Linear(input_dim, output_dim)\n        self.value_layer = nn.Linear(input_dim, output_dim)\n        self.softmax = nn.Softmax(dim=2)\n    def forward(self, x): \n        \n        Q = self.query_layer(x)\n        K = self.key_layer(x)\n        V = self.value_layer(x)\n        \n        scale = self.input_dim ** 0.5\n        attention_scores = torch.bmm(Q, K.transpose(1, 2)) / scale\n        attention = self.softmax(attention_scores)\n        out = torch.bmm(attention, V)\n        return out \n        \n        \nclass AttentionLSTM(nn.Module):\n    def __init__(self, input_size,output_size,hidden_size, num_layers,bidirectional=False):\n        super(AttentionLSTM, self).__init__()\n        \n        self.hidden_size = hidden_size\n        self.num_layers = num_layers\n        self.bidirectional= bidirectional \n        n=1\n        if self.bidirectional:\n            n = 2\n        \n        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True,bidirectional=self.bidirectional)\n#         self.bn = nn.BatchNorm1d(hidden_size)\n        self.attention = SelfAttention(hidden_size, output_size)\n# have an attentional block here \n#         self.linear = nn.Linear(hidden_size*n, hidden_size)\n#         self.nl = nn.LeakyReLU(0.1)\n#         self.linear2 = nn.Linear(hidden_size, output_size)\n        \n    def forward(self, x):\n        n=1\n        if self.bidirectional:\n            n = 2\n        h0 = torch.ones(self.num_layers*n, x.size(0), self.hidden_size).to(x.device)*0.5\n        c0 = torch.ones(self.num_layers*n, x.size(0), self.hidden_size).to(x.device)*0.5\n\n        \n        out, _ = self.lstm(x, (h0, c0))\n        \n        out = self.attention(out)\n        \n#         feed out to linear\n        return out","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = AttentionLSTM(input_size, output_size, hidden_size, num_layers)","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MultiTierLSTM(nn.Module):\n    def __init__(self, input_size,output_size,hidden_size, num_layers):\n        super(MultiTierLSTM, self).__init__()\n        \n        self.hidden_size = hidden_size\n        self.num_layers = num_layers\n        \n        self.lstms = nn.ModuleList([\n            nn.LSTM(input_size=1, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)\n            for _ in range(input_size)\n        ])\n        \n#         self.bn = nn.BatchNorm1d(hidden_size)\n        \n#         self.linear_main = nn.Linear(hidden_size*input_size, (hidden_size*input_size)//2)\n#         self.nl = nn.ReLU()\n        self.linear_final = nn.Linear(hidden_size*input_size, output_size)\n        \n        \n    def forward(self, x):\n        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)\n        \n        outputs = []\n        for i, lstm in enumerate(self.lstms):\n            # Extract each feature and pass it through its respective LSTM\n            feature = x[:, :, i].unsqueeze(2)  # Shape: (batch_size, sequence_length, 1)\n            out, _ = lstm(feature, (h0,c0))\n            outputs.append(out)\n            \n        # Stack the outputs of individual LSTMs along the last dimension\n#         outputs = torch.stack(outputs, dim=-1)  # Shape: (batch_size, sequence_length, input_dim, hidden_dim)\n        \n        concatenated_outputs = torch.cat(outputs, dim=2)  # Shape: (batch_size, input_dim * hidden_dim)\n        \n#         print(concatenated_outputs.shape)\n        # Pass concatenated output through a linear layer\n#         out = self.linear_main(concatenated_outputs)  # Shape: (batch_size, linear_out)\n#         out = self.nl(out)\n        out = self.linear_final(concatenated_outputs)\n        return out\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:57:33.112331Z","iopub.execute_input":"2023-12-01T10:57:33.112692Z","iopub.status.idle":"2023-12-01T10:57:33.122943Z","shell.execute_reply.started":"2023-12-01T10:57:33.112663Z","shell.execute_reply":"2023-12-01T10:57:33.121842Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selections = [0,1,2]\ninput_size = len(selections)# (x1,x2, multiplied x1x2, lagged_multiplied x1x2)\n\nhidden_size= 20\nnum_layers= 2\n\noutput_size = 3\n\nmodel = MultiTierLSTM(input_size, output_size, hidden_size, num_layers)\nmodel.to(DEVICE)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:11.871087Z","iopub.execute_input":"2023-12-01T10:58:11.871474Z","iopub.status.idle":"2023-12-01T10:58:11.883081Z","shell.execute_reply.started":"2023-12-01T10:58:11.871445Z","shell.execute_reply":"2023-12-01T10:58:11.882191Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training Setup   ","metadata":{}},{"cell_type":"code","source":"s = 0\nfor param in model.parameters():\n    s+=param.numel()\ns","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:12.387953Z","iopub.execute_input":"2023-12-01T10:58:12.388541Z","iopub.status.idle":"2023-12-01T10:58:12.394786Z","shell.execute_reply.started":"2023-12-01T10:58:12.388510Z","shell.execute_reply":"2023-12-01T10:58:12.393836Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"feature_augmentor = FeatureAugmentor(indices=[0,1], window_size=300, lags=[500], selections=selections)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:12.551954Z","iopub.execute_input":"2023-12-01T10:58:12.552301Z","iopub.status.idle":"2023-12-01T10:58:12.556845Z","shell.execute_reply.started":"2023-12-01T10:58:12.552271Z","shell.execute_reply":"2023-12-01T10:58:12.555937Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluator_fa = FeatureAugmentor(indices=[0,1], window_size=300, lags=[500], selections=[0,1,2,3])","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:13.099343Z","iopub.execute_input":"2023-12-01T10:58:13.099693Z","iopub.status.idle":"2023-12-01T10:58:13.104934Z","shell.execute_reply.started":"2023-12-01T10:58:13.099665Z","shell.execute_reply":"2023-12-01T10:58:13.103723Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# loss_fn = nn.CrossEntropyLoss(weight = torch.tensor([0.1, 1.0, 1.0]).to(DEVICE)  )\nloss_fn = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:13.423184Z","iopub.execute_input":"2023-12-01T10:58:13.423584Z","iopub.status.idle":"2023-12-01T10:58:13.428270Z","shell.execute_reply.started":"2023-12-01T10:58:13.423549Z","shell.execute_reply":"2023-12-01T10:58:13.427295Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Actual Training Loop","metadata":{}},{"cell_type":"code","source":"infer_model = Model(model,device=DEVICE, selections = [0,1,3])","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:15:22.748214Z","iopub.execute_input":"2023-12-01T11:15:22.749195Z","iopub.status.idle":"2023-12-01T11:15:22.753368Z","shell.execute_reply.started":"2023-12-01T11:15:22.749159Z","shell.execute_reply":"2023-12-01T11:15:22.752403Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = torch.optim.AdamW(model.parameters(),lr = 5e-6)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:09:24.674239Z","iopub.execute_input":"2023-12-01T11:09:24.675163Z","iopub.status.idle":"2023-12-01T11:09:24.680538Z","shell.execute_reply.started":"2023-12-01T11:09:24.675106Z","shell.execute_reply":"2023-12-01T11:09:24.679552Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluator = Evaluator()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:35.512929Z","iopub.execute_input":"2023-12-01T10:58:35.513673Z","iopub.status.idle":"2023-12-01T10:58:35.517691Z","shell.execute_reply.started":"2023-12-01T10:58:35.513638Z","shell.execute_reply":"2023-12-01T10:58:35.516736Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_manager.reset()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:58:35.788793Z","iopub.execute_input":"2023-12-01T10:58:35.789123Z","iopub.status.idle":"2023-12-01T10:58:35.794044Z","shell.execute_reply.started":"2023-12-01T10:58:35.789096Z","shell.execute_reply":"2023-12-01T10:58:35.793063Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_steps = 250\nbatch_size = 64\nSAMPLE_WIDTH = 3995\nlosses = []\n\nval_scores = [0]\nvalidation_frequency = int(n_steps*25)\nn_val_samples = 20\n\nDEVICE=\"cuda\" if torch.cuda.is_available() else \"cpu\"\n# pbar =\nACCUMULATE_GRADIENTS = 50\n\n\nmodel.train()\nfor i in  tqdm(range(n_steps)):\n#     n,l_seq,h_in\n    model.train()\n  \n    \n    sample = data_manager.sample(batch_size, state_based=True, feature_augmentor = feature_augmentor.get_augmented)\n    \n    if len(sample[0].shape)==2:\n        print('err')\n        continue\n    \n    \n    a,b = random.randint(1,3999-SAMPLE_WIDTH),random.randint(3000,3999)\n    \n    trimmed_sample = sample[0][:,:,a:a+SAMPLE_WIDTH]\n    trimmed_label =  sample[1][:,a:a+SAMPLE_WIDTH]\n    \n    output = model(trimmed_sample.permute(0,2,1).to(DEVICE))\n#     print(output.shape)\n    optimizer.zero_grad()\n    \n    loss = loss_fn(output.view(-1,3), trimmed_label.reshape(-1).to(DEVICE))\n\n#     sample i for step instead  \n    \n#     batch, sequence, channel\n# select: [:,i,:]\n#     s = random.randint(200,4000)\n#     loss = loss_fn(output[:,s,:], sample[1][:,s].to(DEVICE))\n    \n    \n    \n    loss.backward()\n    \n    \n#     nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.35)\n\n    optimizer.step()\n    \n#     scheduler.step()\n    \n    \n    \n    \n    losses.append(loss.item())\n    \n    if (i+1) % validation_frequency == 0:\n        model.eval()\n        val_scores.append(evaluator.evaluate(infer_model, data_manager_val, evaluator_fa,k=n_val_samples))\n    else:\n        val_scores.append(val_scores[-1])\n    \n#     if i%ACCUMULATE_GRADIENTS == 0:\n#         optimizer.step()\n#         optimizer.zero_grad()\n#     if i == 500:\n#         optimizer = torch.optim.Adam(model.parameters(),lr = 1e-4)\n#         print(loss.item())\n#     if i == 750:\n#         optimizer = torch.optim.Adam(model.parameters(),lr = 1e-5)\n#         print(loss.item())\n#     if i == 1000:\n#         optimizer = torch.optim.Adam(model.parameters(),lr = 1e-6)\n#         print(loss.item())\n#     if i == 1250:\n#         optimizer = torch.optim.Adam(model.parameters(),lr = 5e-7)\n#         print(loss.item())\n    \n#     if i%100 == 0:\n        \n#         pbar.set_description(f'step {i+1}, loss={loss.item()}')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:11:58.486401Z","iopub.execute_input":"2023-12-01T11:11:58.487130Z","iopub.status.idle":"2023-12-01T11:14:08.375264Z","shell.execute_reply.started":"2023-12-01T11:11:58.487099Z","shell.execute_reply":"2023-12-01T11:14:08.374316Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(losses)\nplt.plot(val_scores)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:14:09.597716Z","iopub.execute_input":"2023-12-01T11:14:09.598094Z","iopub.status.idle":"2023-12-01T11:14:09.838388Z","shell.execute_reply.started":"2023-12-01T11:14:09.598066Z","shell.execute_reply":"2023-12-01T11:14:09.837499Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluator.evaluate(infer_model, data_manager_val, evaluator_fa,k=n_val_samples)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:15:29.281049Z","iopub.execute_input":"2023-12-01T11:15:29.281396Z","iopub.status.idle":"2023-12-01T11:15:45.661114Z","shell.execute_reply.started":"2023-12-01T11:15:29.281368Z","shell.execute_reply":"2023-12-01T11:15:45.660192Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plotter.plot_outputs(sample[0], output)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T10:56:53.390954Z","iopub.execute_input":"2023-12-01T10:56:53.391331Z","iopub.status.idle":"2023-12-01T10:56:53.395608Z","shell.execute_reply.started":"2023-12-01T10:56:53.391303Z","shell.execute_reply":"2023-12-01T10:56:53.394613Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\nwith torch.no_grad():\n    sample = data_manager.sample(32, state_based=True, feature_augmentor=feature_augmentor.get_augmented)\n    i=10\n    print(sample[0].shape)\n    inp = sample[0]\n#     inp = sample[0][:,:,1000:]\n    sample = list(sample)\n#     sample[0] = sample[0][:,:,:]\n    output = model(sample[0].permute(0,2,1).to(DEVICE))\n    \n    plt.title(\"model preds for sample\")\n    \n    plt.plot(sample[0][i,0,:].detach(), color=\"gray\", alpha=0.7, label=\"angleZ\")\n    plt.plot(sample[0][i,1,:].detach(), color=\"pink\", alpha=0.7, label=\"enmo\")\n    \n#     plt.plot(sample[1][0,:])\n    \n    plt.plot(output.softmax(dim=2)[i,:,0].cpu().detach(), label=\"Not in use\", linewidth=2.6)\n    plt.plot(output.softmax(dim=2)[i,:,1].cpu().detach(), label=\"ASLEEP\", linewidth=2.5)\n    plt.plot(output.softmax(dim=2)[i,:,2].cpu().detach(), label=\"AWAKE\",linewidth=2.5)\n    \n#     plt.ylim([0,1])\n    plt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:15:27.705093Z","iopub.execute_input":"2023-12-01T11:15:27.705465Z","iopub.status.idle":"2023-12-01T11:15:28.804261Z","shell.execute_reply.started":"2023-12-01T11:15:27.705436Z","shell.execute_reply":"2023-12-01T11:15:28.803368Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"out","metadata":{"execution":{"iopub.status.busy":"2023-11-23T06:44:43.919787Z","iopub.execute_input":"2023-11-23T06:44:43.920628Z","iopub.status.idle":"2023-11-23T06:44:43.927709Z","shell.execute_reply.started":"2023-11-23T06:44:43.920596Z","shell.execute_reply":"2023-11-23T06:44:43.926784Z"}}},{"cell_type":"code","source":"model.load_state_dict(torch.load('/kaggle/working/lstm_v2.pt'))","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:15:10.913497Z","iopub.execute_input":"2023-12-01T11:15:10.913850Z","iopub.status.idle":"2023-12-01T11:15:10.924263Z","shell.execute_reply.started":"2023-12-01T11:15:10.913822Z","shell.execute_reply":"2023-12-01T11:15:10.923281Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Inference Model","metadata":{}},{"cell_type":"code","source":"class Model(ModelBase):\n    def __init__(self,model: torch.nn.Module, device='cuda'):\n        assert isinstance(model,torch.nn.Module)\n        self.model = model\n        self.device = device\n    \n    def predict(self, samples):\n#         full length sequences\n# batch, seq, [Z,Enmo,Step]\n        outs = model(samples[1][:,:,0:2].to('cuda').float())\n        event_indices = self.get_event_points(outs, par_length=1000)\n        return event_indices, outs\n\n    def get_event_points(self, outs, par_length=100, starting_buffer=200):\n#         outs can be transformed (i.e moving average)\n#         1:asleep, 2:awake\n# will always be in awake first\n        outs = outs.softmax(2)\n        print(outs.shape)\n#         return\n        state= \"AWAKE\"\n        streak_index = 0\n        curr_streak = 0\n        events = [-1,-1]\n        for i in range(starting_buffer, outs.shape[1],1):\n            if state == \"AWAKE\":\n#                 look for change into asleep\n                if outs[0,i,1] > outs[0,i,2]:\n                    if curr_streak == 0:\n                        streak_index = i\n                    curr_streak+=1\n            \n                    if curr_streak > par_length:\n                        \n                        \n                        curr_streak = 0\n                        state=\"ASLEEP\"\n                        events[0] = streak_index\n                else:\n                    curr_streak = 0\n            else:\n#                 ASLEEP\n                if outs[0,i,2] > outs[0,i,1]:\n                    if curr_streak == 0:\n                        streak_index = i\n                    curr_streak+=1\n            \n                    if curr_streak > par_length:\n                        \n                        \n                        curr_streak = 0\n                        state=\"DONE\"\n                        events[1] = streak_index\n                        return events\n                else:\n                    curr_streak = 0\n#         in case the last streak index is still less than par_length \n        events[1] = streak_index\n        return events       \n                \n\n    def __call__(self, X):\n        return self.predict(X)\n    \n# get outs\n# get moving average\n# select onset time\n# select awake time\n# select return answers ** structure tbd\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(model.state_dict(), 'lstm_v2.pt')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:08:27.512105Z","iopub.execute_input":"2023-12-01T11:08:27.512975Z","iopub.status.idle":"2023-12-01T11:08:27.522255Z","shell.execute_reply.started":"2023-12-01T11:08:27.512942Z","shell.execute_reply":"2023-12-01T11:08:27.521272Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load('/kaggle/working/lstm_1.pt'))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"infer_model = Model(model)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_manager_val = SleepStatesValDataManager(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\", limit=122_000_000, transforms = transforms)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = data_manager_val.sample()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"events, outs = infer_model(sample)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"outs","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Plotter.plot_outputs(sample[1][0], outs, lim=[-1,2])\nplt.axvline(events[0], linewidth=2.5)\nplt.axvline(events[1], linewidth=2.5, color='red')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = sample[1].unsqueeze(0)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample[1].shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(sample[:,:,1].detach())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out = model(sample[1][:,0:2].unsqueeze(0).to('cuda').float())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model(data_manager.sample(1)[0].permute(0,2,1).to('cuda'))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(out.softmax(2)[0,:,2].detach().cpu())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_outputs(sample, output, title=\"outputs, samples\"):\n    plt.title(\"model preds for sample\")\n    i=0\n    \n    plt.plot(sample[:,0].detach(), color=\"gray\", alpha=0.7, label=\"angleZ\")\n    plt.plot(sample[:,1].detach(), color=\"pink\", alpha=0.7, label=\"enmo\")\n    \n#     plt.plot(sample[1][0,:])\n    \n    plt.plot(output.softmax(2)[i,:,0].cpu().detach(), label=\"Not in use\", linewidth=2.6)\n    plt.plot(output.softmax(2)[i,:,1].cpu().detach(), label=\"ASLEEP\", linewidth=2.5)\n    plt.plot(output.softmax(2)[i,:,2].cpu().detach(), label=\"AWAKE\",linewidth=2.5)\n    \n    plt.ylim([0,1])\n    plt.legend()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out.argmax(2)[0].detach().cpu()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Plotter.plot_outputs(sample[1], out, others=[out.argmax(2)[0].detach().cpu()/2])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(sample[1][0,:,0].detach(), color=\"gray\", alpha=0.7, label=\"angleZ\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Plotter:\n    @staticmethod\n    def plot_outputs(sample, output, title=\"outputs, samples\", others=[]):\n        plt.title(\"model preds for sample\")\n        i=0\n\n        plt.plot(sample[:,0].detach(), color=\"gray\", alpha=0.7, label=\"angleZ\")\n        plt.plot(sample[:,1].detach(), color=\"pink\", alpha=0.7, label=\"enmo\")\n\n    #     plt.plot(sample[1][0,:])\n\n        plt.plot(output.softmax(2)[i,:,0].cpu().detach(), label=\"Not in use\", linewidth=2.6)\n        plt.plot(output.softmax(2)[i,:,1].cpu().detach(), label=\"ASLEEP\", linewidth=2.5)\n        plt.plot(output.softmax(2)[i,:,2].cpu().detach(), label=\"AWAKE\",linewidth=2.5)\n        \n#         print(one_ds)\n        for arr in others:\n            plt.plot(arr)\n        \n        plt.ylim([0,2])\n        plt.legend()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Plotter.plot_outputs(sample[1], out, others=[out.argmax(2)[0].detach().cpu()/2])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}