Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01.getting-started-with-pytorch changed #26

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,795 changes: 889 additions & 906 deletions 01.getting-started-with-pytorch.ipynb

Large diffs are not rendered by default.

6,145 changes: 3,112 additions & 3,033 deletions 02.face-detection-with-detectron2.ipynb

Large diffs are not rendered by default.

3,455 changes: 1,885 additions & 1,570 deletions 04.first-neural-network.ipynb

Large diffs are not rendered by default.

4,547 changes: 2,314 additions & 2,233 deletions 05.time-series-forecasting-covid-19.ipynb

Large diffs are not rendered by default.

343 changes: 343 additions & 0 deletions CherkNevis.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "1733d9de-60ea-46b9-b07e-0036aac64dfa",
"metadata": {},
"outputs": [],
"source": [
"test_data_size = 14\n",
"daily_cases = [1,11,3,4,5,6,7,8,9,10,23,4,5,6,7,899,34,8,44,5,657,57,0,5,65,7,6,7]\n",
"\n",
"train_data = daily_cases[:-test_data_size] # take all elements of daily_cases except the last 14\n",
"test_data = daily_cases[-test_data_size:]\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c511900c-20f9-450a-87e4-119cf53935df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 7, 8, 9, 0]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"listt = [1,2,3,4,5,6,7,8,9,0]\n",
"listt[-5:]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a8345fec-19ca-4284-a52a-34289e603427",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 11, 3, 4, 5, 6, 7, 8, 9, 10, 23, 4, 5, 6]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# train_data = daily_cases[:-test_data_size]\n",
"\n",
"train_data"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c3d7145d-b1d0-45ff-bc08-f5cb444ac99f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original daily_cases:\n",
"2024-06-15 100\n",
"2024-06-16 150\n",
"2024-06-17 200\n",
"2024-06-18 300\n",
"2024-06-19 400\n",
"Freq: D, dtype: int64\n",
"2024-06-15 NaN\n",
"2024-06-16 50.0\n",
"2024-06-17 50.0\n",
"2024-06-18 100.0\n",
"2024-06-19 100.0\n",
"Freq: D, dtype: float64\n"
]
},
{
"data": {
"text/plain": [
"2024-08-02 10\n",
"2024-08-03 20\n",
"2024-08-04 30\n",
"2024-08-05 40\n",
"2024-08-06 50\n",
"Freq: D, dtype: int64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample cumulative cases Series\n",
"import pandas as pd\n",
"data = [100, 150, 200, 300, 400]\n",
"index = pd.date_range(start='2024-06-15', periods=5)\n",
"daily_cases = pd.Series(data, index=index)\n",
"\n",
"# Original daily_cases (cumulative data)\n",
"print(\"Original daily_cases:\")\n",
"print(daily_cases)\n",
"ss = daily_cases.diff()\n",
"print(ss)\n",
"my_pandas_series = pd.Series([10,20,30,40,50], index = pd.date_range(start = '2024-08-02', periods = 5))\n",
"my_pandas_series\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e0602486-94af-493a-9f5c-f9c0661bcaca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Transformed daily_cases:\n",
"2024-06-15 NaN\n",
"2024-06-16 -50.0\n",
"2024-06-17 0.0\n",
"2024-06-18 50.0\n",
"2024-06-19 0.0\n",
"Freq: D, dtype: float64\n"
]
}
],
"source": [
"# Apply the transformation\n",
"daily_cases = daily_cases.diff()\n",
"\n",
"# Transformed daily_cases (daily data)\n",
"print(\"\\nTransformed daily_cases:\")\n",
"print(daily_cases)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "abe21bdd-dc98-4ec9-861a-d7358ed56e65",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"nan"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Example series\n",
"daily_cases = pd.Series([10, 20, 15])\n",
"\n",
"# Step 1: Calculate differences\n",
"diff = daily_cases.diff() # Result: [NaN, 10.0, -5.0]\n",
"\n",
"# Step 2: Replace NaN with the first element\n",
"filled_diff = diff.fillna(daily_cases[0]) # Result: [10.0, 10.0, -5.0]\n",
"\n",
"# Step 3: Convert to integers\n",
"final_result = filled_diff.astype(np.int64) # Result: [10, 10, -5]\n",
"\n",
"# Assign back to daily_cases\n",
"daily_cases = final_result\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "43880cb4-0fbd-45f2-af33-14d4b4316736",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scaled Train Data: [0. 0.25 0.5 0.75 1. ]\n",
"Scaled Test Data: [1.25 1.5 1.75 2. 2.25]\n"
]
}
],
"source": [
"import numpy as np\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"\n",
"# Example data\n",
"train_data = np.array([10, 20, 30, 40, 50])\n",
"test_data = np.array([60, 70, 80, 90, 100])\n",
"\n",
"# Initialize the scaler\n",
"scaler = MinMaxScaler()\n",
"\n",
"\n",
"# Fit the scaler on the training data\n",
"scaler = scaler.fit(np.expand_dims(train_data, axis=1))\n",
"\n",
"# Transform the training data\n",
"train_data_scaled = scaler.transform(np.expand_dims(train_data, axis=1))\n",
"\n",
"# Transform the test data\n",
"test_data_scaled = scaler.transform(np.expand_dims(test_data, axis=1))\n",
"\n",
"# Print results\n",
"print(\"Scaled Train Data:\", train_data_scaled.flatten())\n",
"print(\"Scaled Test Data:\", test_data_scaled.flatten())\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6b239718-9dd7-4e58-9657-67e00cbd596f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scaled Train Data: [[0. ]\n",
" [0.25]\n",
" [0.5 ]\n",
" [0.75]\n",
" [1. ]]\n",
"Scaled Test Data: [[1.25]\n",
" [1.5 ]\n",
" [1.75]\n",
" [2. ]\n",
" [2.25]]\n"
]
}
],
"source": [
"print(\"Scaled Train Data:\", train_data_scaled)\n",
"print(\"Scaled Test Data:\", test_data_scaled)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "23476fd8-6145-4f7d-81f7-adbc4855e452",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'LSTM_model.svg'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"from torchviz import make_dot\n",
"\n",
"# Define the LSTM model\n",
"class LSTMNet(nn.Module):\n",
" def __init__(self, input_size, hidden_size, output_size, n_layers=1):\n",
" super(LSTMNet, self).__init__()\n",
" self.hidden_size = hidden_size\n",
" self.n_layers = n_layers\n",
" self.lstm = nn.LSTM(input_size, hidden_size, n_layers, batch_first=True)\n",
" self.fc = nn.Linear(hidden_size, output_size)\n",
" \n",
" def forward(self, x):\n",
" h0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(x.device)\n",
" c0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(x.device)\n",
" out, _ = self.lstm(x, (h0, c0))\n",
" out = self.fc(out[:, -1, :])\n",
" return out\n",
"\n",
"# Hyperparameters\n",
"input_size = 1\n",
"hidden_size = 50\n",
"output_size = 1\n",
"num_layers = 1\n",
"\n",
"# Initialize the model\n",
"model = LSTMNet(input_size, hidden_size, output_size, num_layers)\n",
"\n",
"# Create a dummy input\n",
"dummy_input = torch.randn(1, 10, input_size) # Batch size = 1, Sequence length = 10, Input size = 1\n",
"\n",
"# Generate the computation graph\n",
"output = model(dummy_input)\n",
"dot = make_dot(output, params=dict(model.named_parameters()))\n",
"dot.format = 'svg'\n",
"dot.render(\"LSTM_model\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18b89e50-14a6-4bd1-9841-e876ae2bc983",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
50 changes: 50 additions & 0 deletions LSTM_model
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
digraph {
graph [size="12,12"]
node [align=left fontname=monospace fontsize=10 height=0.2 ranksep=0.1 shape=box style=filled]
2283802772080 [label="
(1, 1)" fillcolor=darkolivegreen1]
2283802592880 [label=AddmmBackward0]
2283802592688 -> 2283802592880
2283802772000 [label="fc.bias
(1)" fillcolor=lightblue]
2283802772000 -> 2283802592688
2283802592688 [label=AccumulateGrad]
2283802595280 -> 2283802592880
2283802595280 [label=SliceBackward0]
2283802593312 -> 2283802595280
2283802593312 [label=SelectBackward0]
2283802592112 -> 2283802593312
2283802592112 [label=SliceBackward0]
2283802593504 -> 2283802592112
2283802593504 [label=TransposeBackward0]
2283802592928 -> 2283802593504
2283802592928 [label=MkldnnRnnLayerBackward0]
2283802593408 -> 2283802592928
2283656326896 [label="lstm.weight_ih_l0
(200, 1)" fillcolor=lightblue]
2283656326896 -> 2283802593408
2283802593408 [label=AccumulateGrad]
2283802593744 -> 2283802592928
2283656326816 [label="lstm.weight_hh_l0
(200, 50)" fillcolor=lightblue]
2283656326816 -> 2283802593744
2283802593744 [label=AccumulateGrad]
2283802593648 -> 2283802592928
2283656326976 [label="lstm.bias_ih_l0
(200)" fillcolor=lightblue]
2283656326976 -> 2283802593648
2283802593648 [label=AccumulateGrad]
2283802594032 -> 2283802592928
2283656327056 [label="lstm.bias_hh_l0
(200)" fillcolor=lightblue]
2283656327056 -> 2283802594032
2283802594032 [label=AccumulateGrad]
2283802594992 -> 2283802592880
2283802594992 [label=TBackward0]
2283802593024 -> 2283802594992
2283802771920 [label="fc.weight
(1, 50)" fillcolor=lightblue]
2283802771920 -> 2283802593024
2283802593024 [label=AccumulateGrad]
2283802592880 -> 2283802772080
}
Binary file added LSTM_model.pdf
Binary file not shown.
Binary file added LSTM_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading