Editing
Neural Networks
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Training a simple neural network in PyTorch:''' <syntaxhighlight lang="python"> import torch import torch.nn as nn import torch.optim as optim # Define a simple 3-layer feedforward network class SimpleNet(nn.Module): def __init__(self): super().__init__() self.layers = nn.Sequential( nn.Linear(784, 256), # Input: 28x28 image flattened nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU(), nn.Linear(128, 10) # Output: 10 classes ) def forward(self, x): return self.layers(x) model = SimpleNet() optimizer = optim.Adam(model.parameters(), lr=1e-3) criterion = nn.CrossEntropyLoss() # Training loop for epoch in range(10): for batch_x, batch_y in dataloader: optimizer.zero_grad() predictions = model(batch_x) loss = criterion(predictions, batch_y) loss.backward() # Backpropagation optimizer.step() # Weight update print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}") </syntaxhighlight> ; Key hyperparameters to tune : '''Learning rate''' β Start with 1e-3 for Adam, 1e-1 for SGD. Use a scheduler to decay over time. : '''Batch size''' β 32 or 64 is a good default. Larger batches train faster but may generalize worse. : '''Network depth''' β Start shallow (2β3 layers), deepen only if underfitting. : '''Dropout rate''' β 0.2β0.5 for hidden layers; never apply to the output layer. </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information