Skip to content

Commit fb46b90

Browse files
committed
Code for first printing
1 parent 0635650 commit fb46b90

39 files changed

+3302
-1918
lines changed

Diff for: p1ch4/1_image_dog.ipynb

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"import os\n",
6666
"\n",
6767
"data_dir = '../data/p1ch4/image-cats/'\n",
68-
"filenames = [name for name in os.listdir(data_dir) if os.path.splitext(name)[-1] == '.png']\n",
68+
"filenames = [name for name in os.listdir(data_dir)\n",
69+
" if os.path.splitext(name)[-1] == '.png']\n",
6970
"for i, filename in enumerate(filenames):\n",
7071
" img_arr = imageio.imread(os.path.join(data_dir, filename))\n",
7172
" img_t = torch.from_numpy(img_arr)\n",

Diff for: p1ch4/2_volumetric_ct.ipynb

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"text": [
2323
"Reading DICOM (examining files): 1/99 files (1.0%99/99 files (100.0%)\n",
2424
" Found 1 correct series.\n",
25-
"Reading DICOM (loading data): 87/99 (87.999/99 (100.0%)\n"
25+
"Reading DICOM (loading data): 31/99 (31.392/99 (92.999/99 (100.0%)\n"
2626
]
2727
},
2828
{
@@ -52,7 +52,7 @@
5252
{
5353
"data": {
5454
"text/plain": [
55-
"torch.Size([1, 512, 512, 99])"
55+
"torch.Size([1, 99, 512, 512])"
5656
]
5757
},
5858
"execution_count": 3,
@@ -62,7 +62,6 @@
6262
],
6363
"source": [
6464
"vol = torch.from_numpy(vol_arr).float()\n",
65-
"vol = torch.transpose(vol, 0, 2)\n",
6665
"vol = torch.unsqueeze(vol, 0)\n",
6766
"\n",
6867
"vol.shape"
@@ -120,7 +119,7 @@
120119
"name": "python",
121120
"nbconvert_exporter": "python",
122121
"pygments_lexer": "ipython3",
123-
"version": "3.6.6"
122+
"version": "3.7.6"
124123
}
125124
},
126125
"nbformat": 4,

Diff for: p1ch4/3_tabular_wine.ipynb

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"source": [
99
"import numpy as np\n",
1010
"import torch\n",
11-
"torch.set_printoptions(edgeitems=2, precision=2)"
11+
"torch.set_printoptions(edgeitems=2, precision=2, linewidth=75)"
1212
]
1313
},
1414
{
@@ -36,7 +36,8 @@
3636
"source": [
3737
"import csv\n",
3838
"wine_path = \"../data/p1ch4/tabular-wine/winequality-white.csv\"\n",
39-
"wineq_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=\";\", skiprows=1)\n",
39+
"wineq_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=\";\",\n",
40+
" skiprows=1)\n",
4041
"wineq_numpy"
4142
]
4243
},
@@ -222,8 +223,8 @@
222223
{
223224
"data": {
224225
"text/plain": [
225-
"tensor([6.85e+00, 2.78e-01, 3.34e-01, 6.39e+00, 4.58e-02, 3.53e+01, 1.38e+02,\n",
226-
" 9.94e-01, 3.19e+00, 4.90e-01, 1.05e+01])"
226+
"tensor([6.85e+00, 2.78e-01, 3.34e-01, 6.39e+00, 4.58e-02, 3.53e+01,\n",
227+
" 1.38e+02, 9.94e-01, 3.19e+00, 4.90e-01, 1.05e+01])"
227228
]
228229
},
229230
"execution_count": 10,
@@ -244,8 +245,8 @@
244245
{
245246
"data": {
246247
"text/plain": [
247-
"tensor([7.12e-01, 1.02e-02, 1.46e-02, 2.57e+01, 4.77e-04, 2.89e+02, 1.81e+03,\n",
248-
" 8.95e-06, 2.28e-02, 1.30e-02, 1.51e+00])"
248+
"tensor([7.12e-01, 1.02e-02, 1.46e-02, 2.57e+01, 4.77e-04, 2.89e+02,\n",
249+
" 1.81e+03, 8.95e-06, 2.28e-02, 1.30e-02, 1.51e+00])"
249250
]
250251
},
251252
"execution_count": 11,
@@ -448,7 +449,7 @@
448449
"name": "python",
449450
"nbconvert_exporter": "python",
450451
"pygments_lexer": "ipython3",
451-
"version": "3.7.5"
452+
"version": "3.7.6"
452453
}
453454
},
454455
"nbformat": 4,

Diff for: p1ch4/4_time_series_bikes.ipynb

+21-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"source": [
99
"import numpy as np\n",
1010
"import torch\n",
11-
"torch.set_printoptions(edgeitems=2, threshold=50)"
11+
"torch.set_printoptions(edgeitems=2, threshold=50, linewidth=75)"
1212
]
1313
},
1414
{
@@ -32,11 +32,12 @@
3232
}
3333
],
3434
"source": [
35-
"bikes_numpy = np.loadtxt(\"../data/p1ch4/bike-sharing-dataset/hour-fixed.csv\", \n",
36-
" dtype=np.float32, \n",
37-
" delimiter=\",\", \n",
38-
" skiprows=1, \n",
39-
" converters={1: lambda x: float(x[8:10])}) # <1>\n",
35+
"bikes_numpy = np.loadtxt(\n",
36+
" \"../data/p1ch4/bike-sharing-dataset/hour-fixed.csv\", \n",
37+
" dtype=np.float32, \n",
38+
" delimiter=\",\", \n",
39+
" skiprows=1, \n",
40+
" converters={1: lambda x: float(x[8:10])}) # <1>\n",
4041
"bikes = torch.from_numpy(bikes_numpy)\n",
4142
"bikes"
4243
]
@@ -113,7 +114,8 @@
113114
{
114115
"data": {
115116
"text/plain": [
116-
"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])"
117+
"tensor([1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2,\n",
118+
" 2, 2])"
117119
]
118120
},
119121
"execution_count": 6,
@@ -162,9 +164,9 @@
162164
{
163165
"data": {
164166
"text/plain": [
165-
"tensor([[ 1.0000, 1.0000, 1.0000, 0.0000, 1.0000, 0.0000, 0.0000, 6.0000,\n",
166-
" 0.0000, 1.0000, 0.2400, 0.2879, 0.8100, 0.0000, 3.0000, 13.0000,\n",
167-
" 16.0000, 1.0000, 0.0000, 0.0000, 0.0000]])"
167+
"tensor([[ 1.0000, 1.0000, 1.0000, 0.0000, 1.0000, 0.0000, 0.0000,\n",
168+
" 6.0000, 0.0000, 1.0000, 0.2400, 0.2879, 0.8100, 0.0000,\n",
169+
" 3.0000, 13.0000, 16.0000, 1.0000, 0.0000, 0.0000, 0.0000]])"
168170
]
169171
},
170172
"execution_count": 8,
@@ -193,7 +195,8 @@
193195
}
194196
],
195197
"source": [
196-
"daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4, daily_bikes.shape[2])\n",
198+
"daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4,\n",
199+
" daily_bikes.shape[2])\n",
197200
"daily_weather_onehot.shape"
198201
]
199202
},
@@ -214,7 +217,8 @@
214217
}
215218
],
216219
"source": [
217-
"daily_weather_onehot.scatter_(1, daily_bikes[:,9,:].long().unsqueeze(1) - 1, 1.0)\n",
220+
"daily_weather_onehot.scatter_(\n",
221+
" 1, daily_bikes[:,9,:].long().unsqueeze(1) - 1, 1.0)\n",
218222
"daily_weather_onehot.shape"
219223
]
220224
},
@@ -245,7 +249,8 @@
245249
"temp = daily_bikes[:, 10, :]\n",
246250
"temp_min = torch.min(temp)\n",
247251
"temp_max = torch.max(temp)\n",
248-
"daily_bikes[:, 10, :] = (daily_bikes[:, 10, :] - temp_min) / (temp_max - temp_min)"
252+
"daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - temp_min)\n",
253+
" / (temp_max - temp_min))"
249254
]
250255
},
251256
{
@@ -255,7 +260,8 @@
255260
"outputs": [],
256261
"source": [
257262
"temp = daily_bikes[:, 10, :]\n",
258-
"daily_bikes[:, 10, :] = (daily_bikes[:, 10, :] - torch.mean(temp)) / torch.std(temp)"
263+
"daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - torch.mean(temp))\n",
264+
" / torch.std(temp))"
259265
]
260266
}
261267
],
@@ -275,7 +281,7 @@
275281
"name": "python",
276282
"nbconvert_exporter": "python",
277283
"pygments_lexer": "ipython3",
278-
"version": "3.6.6"
284+
"version": "3.7.6"
279285
}
280286
},
281287
"nbformat": 4,

Diff for: p1ch5/1_parameter_estimation.ipynb

+10-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"%matplotlib inline\n",
1414
"import numpy as np\n",
1515
"import torch\n",
16-
"torch.set_printoptions(edgeitems=2)"
16+
"torch.set_printoptions(edgeitems=2, linewidth=75)"
1717
]
1818
},
1919
{
@@ -73,8 +73,8 @@
7373
{
7474
"data": {
7575
"text/plain": [
76-
"tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000, 21.8000,\n",
77-
" 48.4000, 60.4000, 68.4000])"
76+
"tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000,\n",
77+
" 21.8000, 48.4000, 60.4000, 68.4000])"
7878
]
7979
},
8080
"execution_count": 5,
@@ -128,7 +128,8 @@
128128
"name": "stdout",
129129
"output_type": "stream",
130130
"text": [
131-
"shapes: x: torch.Size([]), y: torch.Size([3, 1]), z: torch.Size([1, 3]), a: torch.Size([2, 1, 1])\n",
131+
"shapes: x: torch.Size([]), y: torch.Size([3, 1])\n",
132+
" z: torch.Size([1, 3]), a: torch.Size([2, 1, 1])\n",
132133
"x * y: torch.Size([3, 1])\n",
133134
"y * z: torch.Size([3, 3])\n",
134135
"y * z * a: torch.Size([2, 3, 3])\n"
@@ -140,7 +141,8 @@
140141
"y = torch.ones(3,1)\n",
141142
"z = torch.ones(1,3)\n",
142143
"a = torch.ones(2, 1, 1)\n",
143-
"print(f\"shapes: x: {x.shape}, y: {y.shape}, z: {z.shape}, a: {a.shape}\")\n",
144+
"print(f\"shapes: x: {x.shape}, y: {y.shape}\")\n",
145+
"print(f\" z: {z.shape}, a: {a.shape}\")\n",
144146
"print(\"x * y:\", (x * y).shape)\n",
145147
"print(\"y * z:\", (y * z).shape)\n",
146148
"print(\"y * z * a:\", (y * z * a).shape)"
@@ -290,7 +292,8 @@
290292
},
291293
"outputs": [],
292294
"source": [
293-
"def training_loop(n_epochs, learning_rate, params, t_u, t_c, print_params=True):\n",
295+
"def training_loop(n_epochs, learning_rate, params, t_u, t_c,\n",
296+
" print_params=True):\n",
294297
" for epoch in range(1, n_epochs + 1):\n",
295298
" w, b = params\n",
296299
"\n",
@@ -637,7 +640,7 @@
637640
"name": "python",
638641
"nbconvert_exporter": "python",
639642
"pygments_lexer": "ipython3",
640-
"version": "3.7.5"
643+
"version": "3.7.6"
641644
},
642645
"pycharm": {
643646
"stem_cell": {

Diff for: p1ch5/2_autograd.ipynb

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21-
"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",
22-
"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",
21+
"t_c = torch.tensor([0.5, 14.0, 15.0, 28.0, 11.0, 8.0,\n",
22+
" 3.0, -4.0, 6.0, 13.0, 21.0])\n",
23+
"t_u = torch.tensor([35.7, 55.9, 58.2, 81.9, 56.3, 48.9,\n",
24+
" 33.9, 21.8, 48.4, 60.4, 68.4])\n",
2325
"t_un = 0.1 * t_u"
2426
]
2527
},
@@ -188,7 +190,7 @@
188190
"name": "python",
189191
"nbconvert_exporter": "python",
190192
"pygments_lexer": "ipython3",
191-
"version": "3.6.6"
193+
"version": "3.7.6"
192194
}
193195
},
194196
"nbformat": 4,

0 commit comments

Comments
 (0)