Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
print(f"full tensor:\n{x}\n")
|
9 |
-
|
10 |
-
# Simple function
|
11 |
-
def g(x, y):
|
12 |
-
z = x + y
|
13 |
-
return z
|
14 |
-
|
15 |
-
# Compiled function
|
16 |
-
@torch.compile(backend="eager")
|
17 |
-
def f(x):
|
18 |
-
x = torch.sin(x)
|
19 |
-
x = g(x, x)
|
20 |
-
return x
|
21 |
-
|
22 |
-
# --- Initialization / run once ---
|
23 |
-
def init_and_run_once():
|
24 |
-
# Example input
|
25 |
-
x = torch.ones(3, 3, dtype=torch.float32)
|
26 |
-
print("=== INITIAL INPUT ===")
|
27 |
-
log_tensor("original input x", x)
|
28 |
-
|
29 |
-
# Run compiled function once
|
30 |
-
out = f(x)
|
31 |
-
|
32 |
-
print("=== OUTPUT AFTER FIRST RUN ===")
|
33 |
-
log_tensor("final output", out)
|
34 |
-
|
35 |
-
# Return output if needed
|
36 |
-
return out
|
37 |
-
|
38 |
-
# Run once at import / init
|
39 |
-
if __name__ == "__main__":
|
40 |
-
init_and_run_once()
|
|
|
1 |
+
# import torch
|
2 |
+
|
3 |
+
# # Utility to log tensor info
|
4 |
+
# def log_tensor(name, x):
|
5 |
+
# print(f"--- {name} ---")
|
6 |
+
# print(f"shape: {x.shape}, dtype: {x.dtype}, device: {x.device}")
|
7 |
+
# print(f"min: {x.min().item():.6f}, max: {x.max().item():.6f}, mean: {x.mean().item():.6f}, sum: {x.sum().item():.6f}")
|
8 |
+
# print(f"full tensor:\n{x}\n")
|
9 |
+
|
10 |
+
# # Simple function
|
11 |
+
# def g(x, y):
|
12 |
+
# z = x + y
|
13 |
+
# return z
|
14 |
+
|
15 |
+
# # Compiled function
|
16 |
+
# @torch.compile(backend="eager")
|
17 |
+
# def f(x):
|
18 |
+
# x = torch.sin(x)
|
19 |
+
# x = g(x, x)
|
20 |
+
# return x
|
21 |
+
|
22 |
+
# # --- Initialization / run once ---
|
23 |
+
# def init_and_run_once():
|
24 |
+
# # Example input
|
25 |
+
# x = torch.ones(3, 3, dtype=torch.float32)
|
26 |
+
# print("=== INITIAL INPUT ===")
|
27 |
+
# log_tensor("original input x", x)
|
28 |
+
|
29 |
+
# # Run compiled function once
|
30 |
+
# out = f(x)
|
31 |
+
|
32 |
+
# print("=== OUTPUT AFTER FIRST RUN ===")
|
33 |
+
# log_tensor("final output", out)
|
34 |
+
|
35 |
+
# # Return output if needed
|
36 |
+
# return out
|
37 |
+
|
38 |
+
# # Run once at import / init
|
39 |
+
# if __name__ == "__main__":
|
40 |
+
# init_and_run_once()
|
41 |
+
|
42 |
+
import os
|
43 |
import torch
|
44 |
+
from torch import nn
|
45 |
+
from torch.utils.data import DataLoader
|
46 |
+
from torchvision import datasets, transforms
|
47 |
+
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
|
48 |
+
print(f"Using {device} device")
|
49 |
+
|
50 |
+
class NeuralNetwork(nn.Module):
|
51 |
+
def __init__(self):
|
52 |
+
super().__init__()
|
53 |
+
self.flatten = nn.Flatten()
|
54 |
+
self.linear_relu_stack = nn.Sequential(
|
55 |
+
nn.Linear(28*28, 512),
|
56 |
+
nn.ReLU(),
|
57 |
+
nn.Linear(512, 512),
|
58 |
+
nn.ReLU(),
|
59 |
+
nn.Linear(512, 10),
|
60 |
+
)
|
61 |
+
|
62 |
+
def forward(self, x):
|
63 |
+
x = self.flatten(x)
|
64 |
+
logits = self.linear_relu_stack(x)
|
65 |
+
return logits
|
66 |
+
|
67 |
+
|
68 |
+
#We create an instance of NeuralNetwork, and move it to the device, and print its structure.
|
69 |
+
model = NeuralNetwork().to(device)
|
70 |
+
print(model)
|
71 |
+
|
72 |
+
#To use the model, we pass it the input data. This executes the model’s forward, along with some background operations.
|
73 |
|
74 |
+
X = torch.rand(1, 28, 28, device=device)
|
75 |
+
logits = model(X)
|
76 |
+
pred_probab = nn.Softmax(dim=1)(logits)
|
77 |
+
y_pred = pred_probab.argmax(1)
|
78 |
+
print(f"Predicted class: {y_pred}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|