Skip to content

Commit

Permalink
Code for first printing
Browse files Browse the repository at this point in the history
  • Loading branch information
elistevens committed Jul 1, 2020
1 parent 0635650 commit fb46b90
Show file tree
Hide file tree
Showing 39 changed files with 3,302 additions and 1,918 deletions.
3 changes: 2 additions & 1 deletion p1ch4/1_image_dog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"import os\n",
"\n",
"data_dir = '../data/p1ch4/image-cats/'\n",
"filenames = [name for name in os.listdir(data_dir) if os.path.splitext(name)[-1] == '.png']\n",
"filenames = [name for name in os.listdir(data_dir)\n",
" if os.path.splitext(name)[-1] == '.png']\n",
"for i, filename in enumerate(filenames):\n",
" img_arr = imageio.imread(os.path.join(data_dir, filename))\n",
" img_t = torch.from_numpy(img_arr)\n",
Expand Down
7 changes: 3 additions & 4 deletions p1ch4/2_volumetric_ct.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"text": [
"Reading DICOM (examining files): 1/99 files (1.0%99/99 files (100.0%)\n",
" Found 1 correct series.\n",
"Reading DICOM (loading data): 87/99 (87.999/99 (100.0%)\n"
"Reading DICOM (loading data): 31/99 (31.392/99 (92.999/99 (100.0%)\n"
]
},
{
Expand Down Expand Up @@ -52,7 +52,7 @@
{
"data": {
"text/plain": [
"torch.Size([1, 512, 512, 99])"
"torch.Size([1, 99, 512, 512])"
]
},
"execution_count": 3,
Expand All @@ -62,7 +62,6 @@
],
"source": [
"vol = torch.from_numpy(vol_arr).float()\n",
"vol = torch.transpose(vol, 0, 2)\n",
"vol = torch.unsqueeze(vol, 0)\n",
"\n",
"vol.shape"
Expand Down Expand Up @@ -120,7 +119,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
15 changes: 8 additions & 7 deletions p1ch4/3_tabular_wine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"source": [
"import numpy as np\n",
"import torch\n",
"torch.set_printoptions(edgeitems=2, precision=2)"
"torch.set_printoptions(edgeitems=2, precision=2, linewidth=75)"
]
},
{
Expand Down Expand Up @@ -36,7 +36,8 @@
"source": [
"import csv\n",
"wine_path = \"../data/p1ch4/tabular-wine/winequality-white.csv\"\n",
"wineq_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=\";\", skiprows=1)\n",
"wineq_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=\";\",\n",
" skiprows=1)\n",
"wineq_numpy"
]
},
Expand Down Expand Up @@ -222,8 +223,8 @@
{
"data": {
"text/plain": [
"tensor([6.85e+00, 2.78e-01, 3.34e-01, 6.39e+00, 4.58e-02, 3.53e+01, 1.38e+02,\n",
" 9.94e-01, 3.19e+00, 4.90e-01, 1.05e+01])"
"tensor([6.85e+00, 2.78e-01, 3.34e-01, 6.39e+00, 4.58e-02, 3.53e+01,\n",
" 1.38e+02, 9.94e-01, 3.19e+00, 4.90e-01, 1.05e+01])"
]
},
"execution_count": 10,
Expand All @@ -244,8 +245,8 @@
{
"data": {
"text/plain": [
"tensor([7.12e-01, 1.02e-02, 1.46e-02, 2.57e+01, 4.77e-04, 2.89e+02, 1.81e+03,\n",
" 8.95e-06, 2.28e-02, 1.30e-02, 1.51e+00])"
"tensor([7.12e-01, 1.02e-02, 1.46e-02, 2.57e+01, 4.77e-04, 2.89e+02,\n",
" 1.81e+03, 8.95e-06, 2.28e-02, 1.30e-02, 1.51e+00])"
]
},
"execution_count": 11,
Expand Down Expand Up @@ -448,7 +449,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
36 changes: 21 additions & 15 deletions p1ch4/4_time_series_bikes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"source": [
"import numpy as np\n",
"import torch\n",
"torch.set_printoptions(edgeitems=2, threshold=50)"
"torch.set_printoptions(edgeitems=2, threshold=50, linewidth=75)"
]
},
{
Expand All @@ -32,11 +32,12 @@
}
],
"source": [
"bikes_numpy = np.loadtxt(\"../data/p1ch4/bike-sharing-dataset/hour-fixed.csv\", \n",
" dtype=np.float32, \n",
" delimiter=\",\", \n",
" skiprows=1, \n",
" converters={1: lambda x: float(x[8:10])}) # <1>\n",
"bikes_numpy = np.loadtxt(\n",
" \"../data/p1ch4/bike-sharing-dataset/hour-fixed.csv\", \n",
" dtype=np.float32, \n",
" delimiter=\",\", \n",
" skiprows=1, \n",
" converters={1: lambda x: float(x[8:10])}) # <1>\n",
"bikes = torch.from_numpy(bikes_numpy)\n",
"bikes"
]
Expand Down Expand Up @@ -113,7 +114,8 @@
{
"data": {
"text/plain": [
"tensor([1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2])"
"tensor([1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2,\n",
" 2, 2])"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -162,9 +164,9 @@
{
"data": {
"text/plain": [
"tensor([[ 1.0000, 1.0000, 1.0000, 0.0000, 1.0000, 0.0000, 0.0000, 6.0000,\n",
" 0.0000, 1.0000, 0.2400, 0.2879, 0.8100, 0.0000, 3.0000, 13.0000,\n",
" 16.0000, 1.0000, 0.0000, 0.0000, 0.0000]])"
"tensor([[ 1.0000, 1.0000, 1.0000, 0.0000, 1.0000, 0.0000, 0.0000,\n",
" 6.0000, 0.0000, 1.0000, 0.2400, 0.2879, 0.8100, 0.0000,\n",
" 3.0000, 13.0000, 16.0000, 1.0000, 0.0000, 0.0000, 0.0000]])"
]
},
"execution_count": 8,
Expand Down Expand Up @@ -193,7 +195,8 @@
}
],
"source": [
"daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4, daily_bikes.shape[2])\n",
"daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4,\n",
" daily_bikes.shape[2])\n",
"daily_weather_onehot.shape"
]
},
Expand All @@ -214,7 +217,8 @@
}
],
"source": [
"daily_weather_onehot.scatter_(1, daily_bikes[:,9,:].long().unsqueeze(1) - 1, 1.0)\n",
"daily_weather_onehot.scatter_(\n",
" 1, daily_bikes[:,9,:].long().unsqueeze(1) - 1, 1.0)\n",
"daily_weather_onehot.shape"
]
},
Expand Down Expand Up @@ -245,7 +249,8 @@
"temp = daily_bikes[:, 10, :]\n",
"temp_min = torch.min(temp)\n",
"temp_max = torch.max(temp)\n",
"daily_bikes[:, 10, :] = (daily_bikes[:, 10, :] - temp_min) / (temp_max - temp_min)"
"daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - temp_min)\n",
" / (temp_max - temp_min))"
]
},
{
Expand All @@ -255,7 +260,8 @@
"outputs": [],
"source": [
"temp = daily_bikes[:, 10, :]\n",
"daily_bikes[:, 10, :] = (daily_bikes[:, 10, :] - torch.mean(temp)) / torch.std(temp)"
"daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - torch.mean(temp))\n",
" / torch.std(temp))"
]
}
],
Expand All @@ -275,7 +281,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
17 changes: 10 additions & 7 deletions p1ch5/1_parameter_estimation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"%matplotlib inline\n",
"import numpy as np\n",
"import torch\n",
"torch.set_printoptions(edgeitems=2)"
"torch.set_printoptions(edgeitems=2, linewidth=75)"
]
},
{
Expand Down Expand Up @@ -73,8 +73,8 @@
{
"data": {
"text/plain": [
"tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000, 21.8000,\n",
" 48.4000, 60.4000, 68.4000])"
"tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000,\n",
" 21.8000, 48.4000, 60.4000, 68.4000])"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -128,7 +128,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"shapes: x: torch.Size([]), y: torch.Size([3, 1]), z: torch.Size([1, 3]), a: torch.Size([2, 1, 1])\n",
"shapes: x: torch.Size([]), y: torch.Size([3, 1])\n",
" z: torch.Size([1, 3]), a: torch.Size([2, 1, 1])\n",
"x * y: torch.Size([3, 1])\n",
"y * z: torch.Size([3, 3])\n",
"y * z * a: torch.Size([2, 3, 3])\n"
Expand All @@ -140,7 +141,8 @@
"y = torch.ones(3,1)\n",
"z = torch.ones(1,3)\n",
"a = torch.ones(2, 1, 1)\n",
"print(f\"shapes: x: {x.shape}, y: {y.shape}, z: {z.shape}, a: {a.shape}\")\n",
"print(f\"shapes: x: {x.shape}, y: {y.shape}\")\n",
"print(f\" z: {z.shape}, a: {a.shape}\")\n",
"print(\"x * y:\", (x * y).shape)\n",
"print(\"y * z:\", (y * z).shape)\n",
"print(\"y * z * a:\", (y * z * a).shape)"
Expand Down Expand Up @@ -290,7 +292,8 @@
},
"outputs": [],
"source": [
"def training_loop(n_epochs, learning_rate, params, t_u, t_c, print_params=True):\n",
"def training_loop(n_epochs, learning_rate, params, t_u, t_c,\n",
" print_params=True):\n",
" for epoch in range(1, n_epochs + 1):\n",
" w, b = params\n",
"\n",
Expand Down Expand Up @@ -637,7 +640,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
"version": "3.7.6"
},
"pycharm": {
"stem_cell": {
Expand Down
8 changes: 5 additions & 3 deletions p1ch5/2_autograd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
"metadata": {},
"outputs": [],
"source": [
"t_c = torch.tensor([0.5, 14.0, 15.0, 28.0, 11.0, 8.0, 3.0, -4.0, 6.0, 13.0, 21.0])\n",
"t_u = torch.tensor([35.7, 55.9, 58.2, 81.9, 56.3, 48.9, 33.9, 21.8, 48.4, 60.4, 68.4])\n",
"t_c = torch.tensor([0.5, 14.0, 15.0, 28.0, 11.0, 8.0,\n",
" 3.0, -4.0, 6.0, 13.0, 21.0])\n",
"t_u = torch.tensor([35.7, 55.9, 58.2, 81.9, 56.3, 48.9,\n",
" 33.9, 21.8, 48.4, 60.4, 68.4])\n",
"t_un = 0.1 * t_u"
]
},
Expand Down Expand Up @@ -188,7 +190,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit fb46b90

Please sign in to comment.