0% found this document useful (0 votes)
2 views1 page

Deep Learning Part5 Coding Cheat Sheet

The document outlines essential components for training and evaluating machine learning models using PyTorch, including a universal training loop, evaluation loop, and templates for a simple MLP and CNN. It also covers autoencoder essentials and techniques for optimizing inputs to create adversarial patterns. Each section provides code snippets to illustrate the concepts discussed.

Uploaded by

starwinpro1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Deep Learning Part5 Coding Cheat Sheet

The document outlines essential components for training and evaluating machine learning models using PyTorch, including a universal training loop, evaluation loop, and templates for a simple MLP and CNN. It also covers autoencoder essentials and techniques for optimizing inputs to create adversarial patterns. Each section provides code snippets to illustrate the concepts discussed.

Uploaded by

starwinpro1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Universal Training Loop

[Link]()
for x, y in loader:
x, y = [Link](device), [Link](device)
optimizer.zero_grad()
logits = model(x)
loss = F.cross_entropy(logits, y)
[Link]()
[Link]()

2) Evaluation Loop

[Link]()
with torch.no_grad():
for x, y in loader:
logits = model(x)
preds = [Link](dim=1)

3) Simple MLP Template

class MLP([Link]):
def __init__(self, in_dim, hidden, out_dim):
super().__init__()
[Link] = [Link](
[Link](in_dim, hidden),
[Link](),
[Link](hidden, out_dim)
)
def forward(self, x):
return [Link](x)

4) Simple CNN Template (32x32 input)

class CNN([Link]):
def __init__(self):
super().__init__()
[Link] = [Link](
nn.Conv2d(3,16,3,padding=1), [Link](), nn.MaxPool2d(2),
nn.Conv2d(16,32,3,padding=1), [Link](), nn.MaxPool2d(2)
)
[Link] = [Link](
[Link](),
[Link](32*8*8,10)
)
def forward(self,x):
x = [Link](x)
return [Link](x)

5) Autoencoder Essentials

x_hat, z = model(x)
loss = F.mse_loss(x_hat, x)

# anomaly score per sample


score = ((x_hat - x)**2).mean(dim=1)

6) Optimize an Input (Adversarial Pattern)

x_adv = [Link]().detach().requires_grad_(True)
opt = [Link]([x_adv], lr=1e-2)

for _ in range(steps):
opt.zero_grad()
logits = model(x_adv)
loss = F.cross_entropy(logits, target)
[Link]()
[Link]()

You might also like