The model investigative jupyter notebook. It's older
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
parent
f527929ba1
commit
4596d15c0a
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"execution_count": 1,
|
||||
"id": "b4cc996f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -29,9 +29,17 @@
|
||||
"import os\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "11cc2375",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Import data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"execution_count": 2,
|
||||
"id": "b0fe3fe6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -44,7 +52,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"execution_count": 3,
|
||||
"id": "083c39ae",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -140,58 +148,58 @@
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548087</th>\n",
|
||||
" <th>548217</th>\n",
|
||||
" <td>21</td>\n",
|
||||
" <td>113</td>\n",
|
||||
" <td>1.747781e+09</td>\n",
|
||||
" <td>35.9836</td>\n",
|
||||
" <td>28.1056</td>\n",
|
||||
" <td>7.1</td>\n",
|
||||
" <td>2.1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548218</th>\n",
|
||||
" <td>18</td>\n",
|
||||
" <td>941</td>\n",
|
||||
" <td>1.747781e+09</td>\n",
|
||||
" <td>36.2797</td>\n",
|
||||
" <td>36.0253</td>\n",
|
||||
" <td>7.0</td>\n",
|
||||
" <td>1.9</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548219</th>\n",
|
||||
" <td>11</td>\n",
|
||||
" <td>141</td>\n",
|
||||
" <td>1.747782e+09</td>\n",
|
||||
" <td>37.2222</td>\n",
|
||||
" <td>36.9486</td>\n",
|
||||
" <td>7.3</td>\n",
|
||||
" <td>1.2</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548220</th>\n",
|
||||
" <td>32</td>\n",
|
||||
" <td>143</td>\n",
|
||||
" <td>1.747783e+09</td>\n",
|
||||
" <td>-31.1900</td>\n",
|
||||
" <td>-68.2500</td>\n",
|
||||
" <td>81.0</td>\n",
|
||||
" <td>3.3</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548221</th>\n",
|
||||
" <td>40</td>\n",
|
||||
" <td>326</td>\n",
|
||||
" <td>1.747745e+09</td>\n",
|
||||
" <td>-4.3300</td>\n",
|
||||
" <td>132.9700</td>\n",
|
||||
" <td>10.0</td>\n",
|
||||
" <td>790</td>\n",
|
||||
" <td>1.747783e+09</td>\n",
|
||||
" <td>17.6300</td>\n",
|
||||
" <td>-101.3340</td>\n",
|
||||
" <td>41.2</td>\n",
|
||||
" <td>4.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548088</th>\n",
|
||||
" <td>40</td>\n",
|
||||
" <td>401</td>\n",
|
||||
" <td>1.747745e+09</td>\n",
|
||||
" <td>-30.1300</td>\n",
|
||||
" <td>-69.4600</td>\n",
|
||||
" <td>10.0</td>\n",
|
||||
" <td>4.1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548089</th>\n",
|
||||
" <td>13</td>\n",
|
||||
" <td>75</td>\n",
|
||||
" <td>1.747745e+09</td>\n",
|
||||
" <td>38.9889</td>\n",
|
||||
" <td>27.9292</td>\n",
|
||||
" <td>8.9</td>\n",
|
||||
" <td>1.4</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548090</th>\n",
|
||||
" <td>31</td>\n",
|
||||
" <td>35</td>\n",
|
||||
" <td>1.747746e+09</td>\n",
|
||||
" <td>-8.0000</td>\n",
|
||||
" <td>107.0500</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>3.1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>548091</th>\n",
|
||||
" <td>47</td>\n",
|
||||
" <td>448</td>\n",
|
||||
" <td>1.747746e+09</td>\n",
|
||||
" <td>-23.3231</td>\n",
|
||||
" <td>-179.9220</td>\n",
|
||||
" <td>540.0</td>\n",
|
||||
" <td>4.7</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>548092 rows × 7 columns</p>\n",
|
||||
"<p>548222 rows × 7 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
@ -202,16 +210,16 @@
|
||||
"3 31 39 1.577837e+09 19.1900 -67.8400 28.0 3.1\n",
|
||||
"4 35 69 1.577837e+09 -25.6400 -70.5200 53.0 3.5\n",
|
||||
"... ... ... ... ... ... ... ...\n",
|
||||
"548087 40 326 1.747745e+09 -4.3300 132.9700 10.0 4.0\n",
|
||||
"548088 40 401 1.747745e+09 -30.1300 -69.4600 10.0 4.1\n",
|
||||
"548089 13 75 1.747745e+09 38.9889 27.9292 8.9 1.4\n",
|
||||
"548090 31 35 1.747746e+09 -8.0000 107.0500 16.0 3.1\n",
|
||||
"548091 47 448 1.747746e+09 -23.3231 -179.9220 540.0 4.7\n",
|
||||
"548217 21 113 1.747781e+09 35.9836 28.1056 7.1 2.1\n",
|
||||
"548218 18 941 1.747781e+09 36.2797 36.0253 7.0 1.9\n",
|
||||
"548219 11 141 1.747782e+09 37.2222 36.9486 7.3 1.2\n",
|
||||
"548220 32 143 1.747783e+09 -31.1900 -68.2500 81.0 3.3\n",
|
||||
"548221 40 790 1.747783e+09 17.6300 -101.3340 41.2 4.0\n",
|
||||
"\n",
|
||||
"[548092 rows x 7 columns]"
|
||||
"[548222 rows x 7 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 35,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -222,7 +230,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"execution_count": 4,
|
||||
"id": "967ec29d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -244,7 +252,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"execution_count": 5,
|
||||
"id": "77c900ad",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -267,19 +275,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"id": "f0c623e1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/var/folders/l9/cmw34wr13t3cp2_91pq103100000gn/T/ipykernel_1457/1713124793.py:1: RuntimeWarning: divide by zero encountered in divide\n",
|
||||
" inv_mag_frequency = 1/np.bincount(df['bin'].to_numpy())\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# inv_mag_frequency = 1/np.bincount(df['bin'].to_numpy())/\n",
|
||||
"# print(inv_mag_frequency)\n",
|
||||
@ -288,9 +287,17 @@
|
||||
"# print(sum(mag_frequency))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5eea5b40",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Create model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "00b89e53",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -316,11 +323,93 @@
|
||||
"\n",
|
||||
"input_size = 5\n",
|
||||
"hidden_size = 20\n",
|
||||
"class_count = 100\n",
|
||||
"regressor_output_size = 3\n",
|
||||
"class_count = 120\n",
|
||||
"regressor_output_size = 4\n",
|
||||
"# model = MultiRNN(input_size, hidden_size, class_count, regressor_output_size)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0e346047",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Create dataset/dataloader"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "2744a649",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class TimeseriesDataset(torch.utils.data.Dataset):\n",
|
||||
" def __init__(self, x, class_y, regress_y, seq_len=100):\n",
|
||||
" self.seq_len = seq_len\n",
|
||||
" self.x = x\n",
|
||||
" self.class_y = class_y\n",
|
||||
" self.regress_y = regress_y\n",
|
||||
" \n",
|
||||
" def __len__(self):\n",
|
||||
" return self.x.__len__() - (self.seq_len-1)\n",
|
||||
" \n",
|
||||
" def __getitem__(self, index):\n",
|
||||
" return (self.x[index:index+self.seq_len], self.class_y[index+self.seq_len], self.regress_y[index+self.seq_len])\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "5714b17d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"x = torch.tensor(df[['time_to_next_event','time','lat','lon','depth','mag']].values).to(torch.float)\n",
|
||||
"y1 = torch.tensor(df[['bin']].values)\n",
|
||||
"y2 = torch.tensor(df[['time_to_next_event', 'time', 'lat', 'lon']].values).to(torch.float)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "72f7b0f1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"train_dataset = TimeseriesDataset(x, y1, y2, seq_len=1000)\n",
|
||||
"train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=10, shuffle=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ee154b1b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0 torch.Size([10, 1000, 6]) torch.Size([10, 1]) torch.Size([10, 4])\n",
|
||||
"54723\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# for i, d in enumerate(train_loader):\n",
|
||||
"# print(i, d[0].shape, d[1].shape, d[2].shape)\n",
|
||||
"# break\n",
|
||||
"# print(len(train_loader))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2277a6ee",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Create the Lightning setup for model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@ -332,33 +421,12 @@
|
||||
" def __init__(self, input_size, hidden_size, class_count, output_size):\n",
|
||||
" super().__init__()\n",
|
||||
" self.model = MultiRNN(input_size, hidden_size, class_count, regressor_output_size)\n",
|
||||
" self.device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
||||
" self.model.to(self.device)\n",
|
||||
"\n",
|
||||
" def forward(self, x):\n",
|
||||
" return self.model.forward(x)\n",
|
||||
" \n",
|
||||
" def training_step(self, batch, batch_idx):\n",
|
||||
" x, y1, y2 = batch\n",
|
||||
" y_hat = self(x)\n",
|
||||
" loss1 = nn.functional.cross_entropy(y_hat, y1)\n",
|
||||
" loss2 = nn.functional.mse_loss(y_hat,y2)\n",
|
||||
" # loss = F.cross_entropy(y_hat, y)\n",
|
||||
" loss = loss1 + loss2\n",
|
||||
" self.log('train_loss_class', loss1)\n",
|
||||
" self.log('train_loss_regress', loss1)\n",
|
||||
" return loss\n",
|
||||
"\n",
|
||||
" def training_step(self, batch, batch_idx):\n",
|
||||
" x, y = batch\n",
|
||||
" y_hat = self(x)\n",
|
||||
" \n",
|
||||
" loss = F.cross_entropy(y_hat, y)\n",
|
||||
" acc = (y_hat.argmax(1) == y).float().mean()\n",
|
||||
" \n",
|
||||
" self.log(\"train_loss\", loss)\n",
|
||||
" self.log(\"train_acc\", acc)\n",
|
||||
" return loss\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def configure_optimizers(self):\n",
|
||||
" optimizer = torch.optim.Adam(self.model.parameters(), lr=1e-3)\n",
|
||||
" scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(\n",
|
||||
@ -369,11 +437,66 @@
|
||||
" \"optimizer\": optimizer,\n",
|
||||
" \"lr_scheduler\": {\n",
|
||||
" \"scheduler\": scheduler,\n",
|
||||
" \"monitor\": \"val_loss\",\n",
|
||||
" \"monitor\": \"val_loss_class\",\n",
|
||||
" },\n",
|
||||
" }"
|
||||
" }\n",
|
||||
" \n",
|
||||
" def training_step(self, batch, batch_idx):\n",
|
||||
" x, y1, y2 = batch\n",
|
||||
" \n",
|
||||
" x.to(self.device)\n",
|
||||
" y1.to(self.device)\n",
|
||||
" y2.to(self.device)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" class_y_hat, regress_y_hat = self(x)\n",
|
||||
" loss1 = nn.functional.cross_entropy(class_y_hat, y1)\n",
|
||||
" loss2 = nn.functional.mse_loss(regress_y_hat, y2)\n",
|
||||
" # loss = F.cross_entropy(y_hat, y)\n",
|
||||
" loss = loss1 + loss2\n",
|
||||
" self.log('train_loss_class', loss1)\n",
|
||||
" self.log('train_loss_regress', loss1) #, on_epoch=True)\n",
|
||||
" return loss\n",
|
||||
" \n",
|
||||
" def validation_step(self, val_batch, batch_idx):\n",
|
||||
" x, y1, y2 = val_batch\n",
|
||||
" \n",
|
||||
" x.to(self.device)\n",
|
||||
" y1.to(self.device)\n",
|
||||
" y2.to(self.device)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" class_y_hat, regress_y_hat = self(x)\n",
|
||||
" loss1 = nn.functional.cross_entropy(class_y_hat, y1)\n",
|
||||
" loss2 = nn.functional.mse_loss(regress_y_hat, y2)\n",
|
||||
" # loss = F.cross_entropy(y_hat, y)\n",
|
||||
" loss = loss1 + loss2\n",
|
||||
" self.log('val_loss_class', loss1)\n",
|
||||
" self.log('val_loss_regress', loss1) #, on_epoch=True)\n",
|
||||
" return loss\n",
|
||||
"\n",
|
||||
" # def training_step(self, batch, batch_idx):\n",
|
||||
" # x, y = batch\n",
|
||||
" # class_y_hat, regress_y_hat = self(x)\n",
|
||||
" \n",
|
||||
" # loss = F.cross_entropy(y_hat, y)\n",
|
||||
" # acc = (y_hat.argmax(1) == y).float().mean()\n",
|
||||
" \n",
|
||||
" # self.log(\"train_loss\", loss)\n",
|
||||
" # self.log(\"train_acc\", acc)\n",
|
||||
" # return loss\n",
|
||||
" \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "97657a45",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@ -418,7 +541,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "sideprojects",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@ -432,7 +555,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.8"
|
||||
"version": "3.11.11"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user