Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
practice deep learning example
  • Loading branch information
csunny committed Mar 19, 2023
commit 8e0fa6aea7a6c24c20274b38a7858f0d099bdbb8
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ venv.bak/
.mypy_cache/
.idea
.vscode
data
267 changes: 267 additions & 0 deletions examples/pytorch/attention.ipynb

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions examples/pytorch/attention_seq2seq.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math \n",
"import torch\n",
"from torch import nn\n",
"from d2l import torch as d2l"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num_hiddens, num_headers = 100, 5"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "nlp_env",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.9.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9be210f112d3bd93a6327b459217e15105c4405a02aec2765b09f959417e942a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
243 changes: 243 additions & 0 deletions examples/pytorch/line_reguration.ipynb

Large diffs are not rendered by default.

222 changes: 222 additions & 0 deletions examples/pytorch/pytorch_file.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.makedirs(os.path.join('..', 'data'), exist_ok=True)\n",
"data_file = os.path.join(\"..\", \"data\", \"house_tiny.csv\")\n",
"\n",
"with open(data_file, 'w') as f:\n",
" f.write(\"NumRooms, Alley, Price\\n\")\n",
" f.write(\"NA, Pave, 127500\\n\")\n",
" f.write(\"2,NA,106000\\n\")\n",
" f.write(\"4,NA,178100\\n\")\n",
" f.write(\"NA,NA,140000\\n\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" NumRooms Alley Price\n",
"0 NaN Pave 127500\n",
"1 2.0 NaN 106000\n",
"2 4.0 NaN 178100\n",
"3 NaN NaN 140000\n"
]
}
],
"source": [
"import pandas as pd\n",
"data = pd.read_csv(os.path.join(\"..\", \"data\", \"house_tiny.csv\"))\n",
"print(data)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"x = torch.arange(20).reshape(5, 4)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0, 4, 8, 12, 16],\n",
" [ 1, 5, 9, 13, 17],\n",
" [ 2, 6, 10, 14, 18],\n",
" [ 3, 7, 11, 15, 19]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x\n",
"x.T"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0, 2, 6],\n",
" [ 6, 0, 20],\n",
" [18, 28, 40]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B = torch.tensor([[1, 2, 3], [2,0,4], [3, 4, 5]])\n",
"B\n",
"\n",
"A = torch.arange(9).reshape(3, 3)\n",
"\n",
"A\n",
"A*B"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([2, 5, 4])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.ones([2, 5, 4])\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([5, 4])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.sum().shape\n",
"a.sum(axis=1).shape\n",
"a.sum(axis=2).shape\n",
"a.sum(axis=0).shape"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.9088, 0.8009, 0.1684, 0.0296, 0.5991],\n",
" [0.5647, 0.1612, 0.0448, 0.4050, 0.8085]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = torch.rand(2, 5)\n",
"A.shape\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.cuda.device_count()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading