Skip to content

Commit d1caac3

Browse files
authored
Merge pull request #2 from mbrukman/update-python-from-notebook
Re-generate Python code from IPython notebooks
2 parents 8e5c2d2 + 6aade64 commit d1caac3

14 files changed

+111
-35
lines changed

ch03/ch03.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def loss_0(z):
283283

284284
plt.ylim(0.0, 5.1)
285285
plt.xlim([0, 1])
286-
plt.xlabel('$\sigma$(z)')
286+
plt.xlabel('$\sigma(z)$')
287287
plt.ylabel('L(w, b)')
288288
plt.legend(loc='best')
289289
plt.tight_layout()

ch04/ch04.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@
448448

449449

450450

451+
451452
LogisticRegression(penalty='l1')
452453

453454

ch07/ch07.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,8 @@ def get_params(self, deep=True):
890890

891891

892892

893+
894+
895+
896+
897+

ch08/ch08.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def get_minibatch(doc_stream, size):
700700

701701

702702
n_top_words = 5
703-
feature_names = count.get_feature_names()
703+
feature_names = count.get_feature_names_out()
704704

705705
for topic_idx, topic in enumerate(lda.components_):
706706
print(f'Topic {(topic_idx + 1)}:')

ch09/ch09.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,8 @@ def mean_absolute_deviation(data):
877877

878878

879879

880+
881+
882+
883+
884+

ch13/ch13_part1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,8 @@ def predict(self, x):
579579

580580

581581

582+
583+
584+
585+
586+

ch13/ch13_part3_lightning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969

7070
class MultiLayerPerceptron(pl.LightningModule):
71-
def __init__(self,image_shape=(1, 28, 28), hidden_units=(32, 16)):
71+
def __init__(self, image_shape=(1, 28, 28), hidden_units=(32, 16)):
7272
super().__init__()
7373

7474
# new PL attributes:

ch13/ch13_part4_ignite.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# coding: utf-8
22

33

4+
import sys
5+
from python_environment_check import check_packages
46
import torch
57
import torch.nn as nn
68
from torch.utils.data import DataLoader
@@ -11,6 +13,37 @@
1113
from ignite.handlers import Checkpoint, DiskSaver
1214
from ignite.contrib.handlers import TensorboardLogger, global_step_from_engine
1315

16+
17+
18+
19+
20+
21+
# # Machine Learning with PyTorch and Scikit-Learn
22+
# # -- Code Examples
23+
24+
# ## Package version checks
25+
26+
# Add folder to path in order to load from the check_packages.py script:
27+
28+
29+
30+
sys.path.insert(0, '..')
31+
32+
33+
# Check recommended package versions:
34+
35+
36+
37+
38+
39+
d = {
40+
'numpy': '1.21.2',
41+
'matplotlib': '3.4.3',
42+
'sklearn': '1.0',
43+
}
44+
check_packages(d)
45+
46+
1447
# # Chapter 13: Going Deeper -- the Mechanics of PyTorch
1548

1649
# **Big thanks and credit to Victor Fomin for creating and helping with the original draft of this section!**

ch15/ch15_part2.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,3 @@ def forward(self, text, lengths):
410410

411411

412412

413-
414-
415-
416-
417-

ch15/ch15_part3.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def __getitem__(self, idx):
179179

180180
class RNN(nn.Module):
181181
def __init__(self, vocab_size, embed_dim, rnn_hidden_size):
182-
super(RNN, self).__init__()
182+
super().__init__()
183183
self.embedding = nn.Embedding(vocab_size, embed_dim)
184184
self.rnn_hidden_size = rnn_hidden_size
185185
self.rnn = nn.LSTM(embed_dim, rnn_hidden_size,
@@ -365,8 +365,3 @@ def sample(model, starting_str,
365365

366366

367367

368-
369-
370-
371-
372-

ch17/ch17_part1.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def create_samples(g_model, input_z):
443443
ax.set_xlabel('Iteration', size=15)
444444
ax.set_ylabel('Discriminator output', size=15)
445445

446-
#plt.savefig('images/ch17-gan-learning-curve.pdf')
446+
#plt.savefig('figures/ch17-gan-learning-curve.pdf')
447447
plt.show()
448448

449449

@@ -467,7 +467,7 @@ def create_samples(g_model, input_z):
467467
image = epoch_samples[e-1][j]
468468
ax.imshow(image, cmap='gray_r')
469469

470-
#plt.savefig('images/ch17-vanila-gan-samples.pdf')
470+
#plt.savefig('figures/ch17-vanila-gan-samples.pdf')
471471
plt.show()
472472

473473

@@ -546,3 +546,8 @@ def compute_score(fake, real , k=1, sigma=1, sqrt=True):
546546

547547

548548

549+
550+
551+
552+
553+

ch17/ch17_part2.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# coding: utf-8
22

33

4+
import sys
5+
from python_environment_check import check_packages
46
#from google.colab import drive
57
import torch
68
import torch.nn as nn
@@ -11,11 +13,33 @@
1113
from torch.utils.data import DataLoader
1214
from torch.autograd import grad as torch_grad
1315

14-
# *Python Machine Learning, PyTorch Edition* by [Sebastian Raschka](https://sebastianraschka.com), [Yuxi (Hayden) Liu](https://www.mlexample.com/) & [Vahid Mirjalili](http://vahidmirjalili.com), Packt Publishing Ltd. 2021
15-
#
16-
# Code Repository:
17-
#
18-
# Code License: [MIT License]()
16+
# # Machine Learning with PyTorch and Scikit-Learn
17+
# # -- Code Examples
18+
19+
# ## Package version checks
20+
21+
# Add folder to path in order to load from the check_packages.py script:
22+
23+
24+
25+
sys.path.insert(0, '..')
26+
27+
28+
# Check recommended package versions:
29+
30+
31+
32+
33+
34+
d = {
35+
'torch': '1.8.0',
36+
'torchvision': '0.9.0',
37+
'numpy': '1.21.2',
38+
'matplotlib': '3.4.3',
39+
}
40+
41+
check_packages(d)
42+
1943

2044
# # Chapter 17 - Generative Adversarial Networks for Synthesizing New Data (Part 2/2)
2145

@@ -80,6 +104,8 @@
80104

81105

82106

107+
108+
83109
print(torch.__version__)
84110
print("GPU Available:", torch.cuda.is_available())
85111

@@ -93,10 +119,13 @@
93119

94120

95121

122+
96123
# ## Train the DCGAN model
97124

98125

99126

127+
128+
100129
image_path = './'
101130
transform = transforms.Compose([
102131
transforms.ToTensor(),
@@ -140,7 +169,7 @@ def make_generator_network(input_size, n_filters):
140169

141170
class Discriminator(nn.Module):
142171
def __init__(self, n_filters):
143-
super(Discriminator, self).__init__()
172+
super().__init__()
144173
self.network = nn.Sequential(
145174
nn.Conv2d(1, n_filters, 4, 2, 1, bias=False),
146175
nn.LeakyReLU(0.2),
@@ -292,7 +321,7 @@ def create_samples(g_model, input_z):
292321
image = epoch_samples[e-1][j]
293322
ax.imshow(image, cmap='gray_r')
294323

295-
# plt.savefig('images/ch17-dcgan-samples.pdf')
324+
# plt.savefig('figures/ch17-dcgan-samples.pdf')
296325
plt.show()
297326

298327

@@ -335,7 +364,7 @@ def make_generator_network_wgan(input_size, n_filters):
335364

336365
class DiscriminatorWGAN(nn.Module):
337366
def __init__(self, n_filters):
338-
super(DiscriminatorWGAN, self).__init__()
367+
super().__init__()
339368
self.network = nn.Sequential(
340369
nn.Conv2d(1, n_filters, 4, 2, 1, bias=False),
341370
nn.LeakyReLU(0.2),
@@ -368,6 +397,7 @@ def forward(self, input):
368397

369398

370399

400+
371401
def gradient_penalty(real_data, generated_data):
372402
batch_size = real_data.size(0)
373403

@@ -474,7 +504,7 @@ def g_train_wgan(x):
474504
image = epoch_samples_wgan[e-1][j]
475505
ax.imshow(image, cmap='gray_r')
476506

477-
# plt.savefig('images/ch17-wgan-gp-samples.pdf')
507+
# plt.savefig('figures/ch17-wgan-gp-samples.pdf')
478508
plt.show()
479509

480510

@@ -496,8 +526,3 @@ def g_train_wgan(x):
496526

497527

498528

499-
500-
501-
502-
503-

ch18/ch18_part2.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,3 @@ def forward(self, data):
230230

231231

232232

233-
234-
235-
236-
237-

update_python_from_notebook.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
for ch in ch[0-9][0-9] ; do
4+
pushd "${ch}"
5+
for ipynb in *.ipynb ; do
6+
py="${ipynb/ipynb/py}"
7+
if [ -e "${py}" ]; then
8+
python ../.convert_notebook_to_script.py --input "${ipynb}" --output "${py}"
9+
fi
10+
done
11+
popd
12+
done

0 commit comments

Comments
 (0)