Skip to content

Commit 9bc0248

Browse files
Quicken src/recipes/banana_pudding.py and 2 more (#14)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent d50f8e6 commit 9bc0248

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

src/reactivity_visualizer.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import torch
2+
from torch.autograd import Variable
3+
import torchvision.transforms as transforms
4+
import torch.nn.functional as F
5+
6+
# Define a simple neural network
7+
class SimpleNet(torch.nn.Module):
8+
def __init__(self):
9+
super(SimpleNet, self).__init__()
10+
self.fc1 = torch.nn.Linear(784, 128)
11+
self.relu = torch.nn.ReLU()
12+
self.fc2 = torch.nn.Linear(128, 64)
13+
14+
def forward(self, x):
15+
x = F.relu(self.fc1(x))
16+
x = self.fc2(x)
17+
return x
18+
19+
# Example usage
20+
transform = transforms.Compose([
21+
transforms.ToTensor(),
22+
transforms.Normalize((0.5,), (0.5,))
23+
])
24+
batch_size = 64
25+
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
26+
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
27+
28+
net = SimpleNet()
29+
criterion = torch.nn.CrossEntropyLoss()
30+
31+
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
32+
33+
for epoch in range(3):
34+
running_loss = 0.0
35+
for data, target in train_loader:
36+
inputs, labels = data, target
37+
38+
optimizer.zero_grad()
39+
outputs = net(inputs)
40+
loss = criterion(outputs, labels)
41+
loss.backward()
42+
optimizer.step()
43+
44+
running_loss += loss.item()
45+
print(f'Epoch [{epoch+1}/{3}], Loss: {running_loss/len(train_loader)}')

src/recipes/rot13_encryptor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import *
2+
import hashlib
3+
4+
def rot13_encryptor(message: str) -> str:
5+
encrypted_message = ""
6+
for char in message:
7+
if char.isalpha():
8+
ascii_offset = ord('A') if char.isupper() else ord('a')
9+
encrypted_char = chr((ord(char) - ascii_offset + 13) % 26 + ascii_offset)
10+
encrypted_message += encrypted_char
11+
else:
12+
encrypted_message += char
13+
return encrypted_message
14+
15+
# Use the encryptor to encrypt a message with a specific key
16+
key = 0xCAFE - 0xBABE
17+
original_message = "hello"
18+
encrypted_message = rot13_encryptor(original_message, key)
19+
print(f"Original: {original_message}")
20+
print(f"Encrypted: {encrypted_message}")
21+
22+
# Verify the encryption with the same key to ensure it was correct
23+
decrypted_message = rot13_encryptor(encrypted_message, key)
24+
print(f"Decrypted: {decrypted_message}")

0 commit comments

Comments
 (0)