Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

discussing it, let me illustrate it.Ball Dataset and Block ModelLet’s use a dataset of 1,000 random points drawn from a ten-dimensional ball (thisseems fancier than it actually is; you can think of it as a dataset with 1,000 pointswith ten features each) such that each feature has zero mean and unit standarddeviation. In this dataset, points situated within half of the radius of the ball arelabeled as negative cases, while the remaining points are labeled positive cases. It is afamiliar binary classification task.Data Generation1 X, y = load_data(n_points=1000, n_dims=10)Next, we can use these data points to create a dataset and a data loader (no minibatchesthis time):Data Preparation1 ball_dataset = TensorDataset(2 torch.as_tensor(X).float(), torch.as_tensor(y).float()3 )4 ball_loader = DataLoader(ball_dataset, batch_size=len(X))The data preparation part is done. What about the model configuration? Toillustrate the vanishing gradients problem, we need a deeper model than the oneswe’ve built so far. Let’s call it the "block" model: It is a block of several hiddenlayers (and activation functions) stacked together, every layer containing the samenumber of hidden units (neurons).Instead of building the model manually, I’ve created a function, build_model(), thatallows us to configure a model like that. Its main arguments are the number offeatures, the number of layers, the number of hidden units per layer, the activationfunction to be placed after each hidden layer, and if it should add a batchnormalization layer after every activation function or not:Vanishing and Exploding Gradients | 563

Model Configuration (1)1 torch.manual_seed(11)2 n_features = X.shape[1]3 n_layers = 54 hidden_units = 1005 activation_fn = nn.ReLU6 model = build_model(7 n_features, n_layers, hidden_units,8 activation_fn, use_bn=False9 )Let’s check the model out:print(model)OutputSequential((h1): Linear(in_features=10, out_features=100, bias=True)(a1): ReLU()(h2): Linear(in_features=100, out_features=100, bias=True)(a2): ReLU()(h3): Linear(in_features=100, out_features=100, bias=True)(a3): ReLU()(h4): Linear(in_features=100, out_features=100, bias=True)(a4): ReLU()(h5): Linear(in_features=100, out_features=100, bias=True)(a5): ReLU()(o): Linear(in_features=100, out_features=1, bias=True))Exactly as expected! The layers are labeled sequentially, from one up to thenumber of layers, and have prefixes according to their roles: h for linear layers, a foractivation functions, bn for batch normalization layers, and o for the last (output)layer.We’re only missing a loss function and an optimizer, and then we’re done with themodel configuration part too:564 | Extra Chapter: Vanishing and Exploding Gradients

Model Configuration (1)

1 torch.manual_seed(11)

2 n_features = X.shape[1]

3 n_layers = 5

4 hidden_units = 100

5 activation_fn = nn.ReLU

6 model = build_model(

7 n_features, n_layers, hidden_units,

8 activation_fn, use_bn=False

9 )

Let’s check the model out:

print(model)

Output

Sequential(

(h1): Linear(in_features=10, out_features=100, bias=True)

(a1): ReLU()

(h2): Linear(in_features=100, out_features=100, bias=True)

(a2): ReLU()

(h3): Linear(in_features=100, out_features=100, bias=True)

(a3): ReLU()

(h4): Linear(in_features=100, out_features=100, bias=True)

(a4): ReLU()

(h5): Linear(in_features=100, out_features=100, bias=True)

(a5): ReLU()

(o): Linear(in_features=100, out_features=1, bias=True)

)

Exactly as expected! The layers are labeled sequentially, from one up to the

number of layers, and have prefixes according to their roles: h for linear layers, a for

activation functions, bn for batch normalization layers, and o for the last (output)

layer.

We’re only missing a loss function and an optimizer, and then we’re done with the

model configuration part too:

564 | Extra Chapter: Vanishing and Exploding Gradients

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!