]>
vgcfreebox.myrthtech.pt Git - ue-rnap-aerossol.git/blob - torch-randomdata-example.py
3 import torch
.optim
as optim
4 from torch
.utils
.data
import Dataset
, DataLoader
8 # ----------------------------------- BUILD MODEL ---------------------------------------------
9 # 1. Define the Model Class
10 class SimpleDNN(nn
.Module
):
11 def __init__(self
, input_size
, hidden_size
, output_size
):
12 super(SimpleDNN
, self
).__init
__()
13 # Define the layers (Linear means fully connected)
14 self
.layer1
= nn
.Linear(input_size
, hidden_size
)
15 self
.relu
= nn
.ReLU() # Activation function
16 self
.layer2
= nn
.Linear(hidden_size
, output_size
)
19 # This is the path data takes through the network
25 # Initialization: Assuming input is 784 (like MNIST flattened image)
28 OUTPUT_SIZE
= 10 # 10 classes
29 model
= SimpleDNN(INPUT_SIZE
, HIDDEN_SIZE
, OUTPUT_SIZE
)
32 # ----------------------------------- GPU INTEGRATION ---------------------------------------------
33 # 1. Check for GPU availability
34 device
= torch
.device("cuda" if torch
.cuda
.is_available() else "cpu")
35 print(f
"Using device: {device}")
37 # 2. Move the entire model to the GPU
43 criterion
= nn
.CrossEntropyLoss() # Loss function
44 optimizer
= optim
.Adam(model
.parameters(), lr
=LEARNING_RATE
) # Optimizer
47 # ----------------------------------- LOAD DATA ---------------------------------------------
48 # 1. DEFINE THE CUSTOM DATASET
49 class CustomDataset(Dataset
):
50 def __init__(self
, features
, labels
):
51 # features should be the full dataset of inputs (e.g., all 784 pixel values)
52 self
.features
= torch
.tensor(features
, dtype
=torch
.float32
)
53 # labels should be the full dataset of target labels (integers)
54 self
.labels
= torch
.tensor(labels
, dtype
=torch
.long)
57 # Returns the total number of samples
58 return len(self
.features
)
60 def __getitem__(self
, idx
):
61 # Returns a single sample and its label (formatted as a dictionary
62 # to match your current usage: data['features'], data['labels'])
64 'features': self
.features
[idx
],
65 'labels': self
.labels
[idx
]
68 # 2. LOAD THE DATA (REPLACE THIS WITH YOUR ACTUAL LOADING CODE)
69 DUMMY_FEATURES
= np
.random
.rand(100, INPUT_SIZE
).astype(np
.float32
)
70 DUMMY_LABELS
= np
.random
.randint(0, OUTPUT_SIZE
, 100).astype(np
.int64
)
72 # 3. INSTANTIATE AND WRAP THE LOADER
73 # Create the dataset object
74 train_dataset
= CustomDataset(DUMMY_FEATURES
, DUMMY_LABELS
)
76 # Create the DataLoader object
77 BATCH_SIZE
= 64 # Choose a desired batch size
78 train_loader
= DataLoader(train_dataset
, batch_size
=BATCH_SIZE
, shuffle
=True)
81 # ----------------------------------- TRAINING ---------------------------------------------
82 # --- The Training Loop ---
83 for epoch
in range(NUM_EPOCHS
):
84 for batch_idx
, data
in enumerate(train_loader
):
87 inputs
= data
['features'].to(device
)
88 labels
= data
['labels'].to(device
)
90 # 2. ZERO GRADIENTS (Crucial step!)
91 # Must clear the gradients from the previous step
95 outputs
= model(inputs
)
98 loss
= criterion(outputs
, labels
)
100 # 5. BACKWARD PASS (Calculates gradients)
101 # This is the step that utilizes CUDA for massive parallel computation.
104 # 6. OPTIMIZER STEP (Updates weights)
107 print(f
"Epoch {epoch+1}/{NUM_EPOCHS}, Loss: {loss.item():.4f}")