Upload 2 files
Browse files- WEATHER-RUN-10000.safetensors +3 -0
- model.py +80 -0
WEATHER-RUN-10000.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7b9efc5330a7ffab7e7ab59a71402d84a619cb21b5dadbe785b5e291f87497b3
|
| 3 |
+
size 660
|
model.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.optim as optim
|
| 4 |
+
import numpy as np
|
| 5 |
+
from safetensors.torch import save_file, load_file
|
| 6 |
+
|
| 7 |
+
epochs = 10000
|
| 8 |
+
# Step 1: Create a simple dataset
|
| 9 |
+
# Features: [temperature, is_raining]
|
| 10 |
+
# Labels: 1 (can run), 0 (cannot run)
|
| 11 |
+
data = np.array([
|
| 12 |
+
[30, 0, 1], # 30°C, no rain -> can run
|
| 13 |
+
[22, 1, 0], # 22°C, raining -> cannot run
|
| 14 |
+
[25, 0, 1], # 25°C, no rain -> can run
|
| 15 |
+
[15, 1, 0], # 15°C, raining -> cannot run
|
| 16 |
+
[20, 0, 1], # 20°C, no rain -> can run
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
# Normalize the features
|
| 20 |
+
X = (data[:, :2] - np.mean(data[:, :2], axis=0)) / np.std(data[:, :2], axis=0)
|
| 21 |
+
y = torch.tensor(data[:, 2], dtype=torch.float32).view(-1, 1) # Labels
|
| 22 |
+
|
| 23 |
+
# Step 2: Create a simple neural network model
|
| 24 |
+
class SimpleNN(nn.Module):
|
| 25 |
+
def __init__(self):
|
| 26 |
+
super(SimpleNN, self).__init__()
|
| 27 |
+
self.fc1 = nn.Linear(2, 8) # Increased number of neurons
|
| 28 |
+
self.fc2 = nn.Linear(8, 4) # Added an additional hidden layer
|
| 29 |
+
self.fc3 = nn.Linear(4, 1) # Hidden layer to output layer
|
| 30 |
+
self.sigmoid = nn.Sigmoid() # Sigmoid activation function
|
| 31 |
+
|
| 32 |
+
def forward(self, x):
|
| 33 |
+
x = self.fc1(x)
|
| 34 |
+
x = nn.ReLU()(x) # ReLU activation function
|
| 35 |
+
x = self.fc2(x)
|
| 36 |
+
x = nn.ReLU()(x)
|
| 37 |
+
x = self.fc3(x)
|
| 38 |
+
return self.sigmoid(x) # Output
|
| 39 |
+
|
| 40 |
+
# Step 3: Train the model
|
| 41 |
+
model = SimpleNN()
|
| 42 |
+
criterion = nn.BCELoss() # Binary cross-entropy loss
|
| 43 |
+
optimizer = optim.Adam(model.parameters(), lr=0.01) # Using Adam optimizer
|
| 44 |
+
|
| 45 |
+
# Training loop
|
| 46 |
+
for epoch in range(epochs): # Increased epochs
|
| 47 |
+
model.train()
|
| 48 |
+
optimizer.zero_grad() # Zero gradients
|
| 49 |
+
output = model(torch.tensor(X, dtype=torch.float32)) # Forward pass
|
| 50 |
+
loss = criterion(output, y) # Compute loss
|
| 51 |
+
loss.backward() # Backward pass
|
| 52 |
+
optimizer.step() # Update weights
|
| 53 |
+
|
| 54 |
+
if (epoch + 1) % 20 == 0: # Print every 20 epochs
|
| 55 |
+
print(f'Epoch [{epoch + 1}/200], Loss: {loss.item():.4f}')
|
| 56 |
+
|
| 57 |
+
# Step 4: Save the model to Safetensors format
|
| 58 |
+
save_file(model.state_dict(), f'WEATHER-RUN-{epochs}.safetensors')
|
| 59 |
+
|
| 60 |
+
# Step 5: Load and run the model
|
| 61 |
+
def load_and_run_model(model_path, input_data):
|
| 62 |
+
model = SimpleNN()
|
| 63 |
+
model.load_state_dict(load_file(model_path)) # Load the model state
|
| 64 |
+
model.eval() # Set model to evaluation mode
|
| 65 |
+
|
| 66 |
+
with torch.no_grad(): # No need to compute gradients
|
| 67 |
+
input_tensor = torch.tensor(input_data, dtype=torch.float32)
|
| 68 |
+
output = model(input_tensor)
|
| 69 |
+
return output.numpy() # Return predictions as numpy array
|
| 70 |
+
|
| 71 |
+
# Testing the model with new data
|
| 72 |
+
test_data = [[25, 0], [18, 1], [21, 0], [19, 1]]
|
| 73 |
+
normalized_test_data = (np.array(test_data) - np.mean(data[:, :2], axis=0)) / np.std(data[:, :2], axis=0)
|
| 74 |
+
predictions = load_and_run_model(f'WEATHER-RUN-{epochs}.safetensors', normalized_test_data)
|
| 75 |
+
|
| 76 |
+
# Output predictions
|
| 77 |
+
for (temp, rain), pred in zip(test_data, predictions):
|
| 78 |
+
result = "can" if pred[0] >= 0.5 else "cannot"
|
| 79 |
+
print(f"With temperature {temp}°C and {'rain' if rain else 'no rain'}, you {result} go for a run.")
|
| 80 |
+
|