Challenge: Initializing Model Weights and Biases
Tarefa
Swipe to start coding
You are tasked with creating random tensors to initialize weights and biases for a simple neural network.
- Ensure reproducibility by setting a manual seed to an arbitrary number before generating the tensors.
- Create a
3x4
tensor filled with random values from a uniform distribution between0
and1
(weights for the first layer). - Create a
1x4
tensor filled with zeros (biases for the first layer). - Create a
4x2
tensor with random integers between-5
and5
(weights for the second layer).
Solução
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import torch
# Set the manual seed
torch.manual_seed(42)
# Create a 3x4 tensor with random values between 0 and 1
weights_layer1 = torch.rand(3, 4)
# Create a 1x4 tensor filled with zeros
biases_layer1 = torch.zeros(1, 4)
# Create a 4x2 tensor with random integers between -5 and 5
weights_layer2 = torch.randint(-5, 5, (4, 2))
# Print the tensors
print("Weights for Layer 1:\n", weights_layer1)
print("Biases for Layer 1:\n", biases_layer1)
print("Weights for Layer 2:\n", weights_layer2)
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 1. Capítulo 6
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import torch
# Set the manual seed
___
# Create a 3x4 tensor with random values between 0 and 1
weights_layer1 = ___
# Create a 1x4 tensor filled with zeros
biases_layer1 = ___
# Create a 4x2 tensor with random integers between -5 and 5
weights_layer2 = ____
# Print the tensors
print("Weights for layer 1:\n", weights_layer1)
print("Biases for layer 1:\n", biases_layer1)
print("Weights for layer 2:\n", weights_layer2)
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo