Introduction to Deep Learning with PyTorch
Jasmin Ludolf
Senior Data Science Content Developer, DataCamp
# Importing as nn to avoid writing torch.nn import torch.nn as nn
# Create input_tensor with three features input_tensor = torch.tensor( [[0.3471, 0.4547, -0.2356]])
# Importing as nn to avoid writing torch.nn import torch.nn as nn
# Create input_tensor with three features input_tensor = torch.tensor( [[0.3471, 0.4547, -0.2356]])
# Define our linear layer linear_layer = nn.Linear(
in_features=3,
out_features=2
)
# Importing as nn to avoid writing torch.nn import torch.nn as nn
# Create input_tensor with three features input_tensor = torch.tensor( [[0.3471, 0.4547, -0.2356]])
# Define our linear layer linear_layer = nn.Linear( in_features=3, out_features=2 )
# Pass input through linear layer output = linear_layer(input_tensor) print(output)
$$ $$ $$
# Pass input through linear layer
output = linear_layer(input_tensor)
print(output)
tensor([[-0.2415, -0.1604]],
grad_fn=<AddmmBackward0>)
output = linear_layer(input_tensor)
.weight
print(linear_layer.weight)
Parameter containing:
tensor([[-0.4799, 0.4996, 0.1123],
[-0.0365, -0.1855, 0.0432]],
requires_grad=True)
$$
.bias
print(linear_layer.bias)
Parameter containing:
tensor([0.0310, 0.1537], requires_grad=True)
$$
$$
Introduction to Deep Learning with PyTorch