In the
You can see the weights and biases shown in the diagram above. Let us now add them to our code.
We start with the basic neural network class:
class MyBasicNN(nn.Module):
def __init__(self):
super().__init__()
Our first weight has the value 1.70.
We can represent it like this:
class MyBasicNN(nn.Module):
def __init__(self):
super().__init__()
self.w00 = nn.Parameter(torch.tensor(1.7), requires_grad=False)
Here, we initialize a new variable called w00 and make it a neural network parameter.
When we define a weight as a parameter, PyTorch treats it as part of the neural network and gives us the option to optimize it during training.
Since this value is stored as a tensor, the neural network can take advantage of features such as:
- automatic differentiation
- accelerated mathematical operations
If you are unfamiliar with tensors, check out my
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
SOCIAL SHARE CARD GENERATOR