Introduction to Deep Learning with PyTorch
Jasmin Ludolf
Senior Data Science Content Developer, DataCamp
$$







Obtain a value between 0 and 1
If output is > 0.5, class label = 1 (mammal)
import torch import torch.nn as nn input_tensor = torch.tensor([[6]]) sigmoid = nn.Sigmoid()output = sigmoid(input_tensor) print(output)
tensor([[0.9975]])
model = nn.Sequential(
nn.Linear(6, 4), # First linear layer
nn.Linear(4, 1), # Second linear layer
nn.Sigmoid() # Sigmoid activation function
)
Sigmoid as last step in network of linear layers is equivalent to traditional logistic regression






import torch import torch.nn as nn # Create an input tensor input_tensor = torch.tensor( [[4.3, 6.1, 2.3]]) # Apply softmax along the last dimensionprobabilities = nn.Softmax(dim=-1) output_tensor = probabilities(input_tensor) print(output_tensor)
tensor([[0.1392, 0.8420, 0.0188]])
dim = -1 indicates softmax is applied to the input tensor's last dimensionnn.Softmax() can be used as last step in nn.Sequential()Introduction to Deep Learning with PyTorch