Upload 2 files
Browse filesFiles changed (2)
- WEATHER-RUN-10000.safetensorsAdded+3
- model.pyAdded+80
123version https://git-lfs.github.com/spec/v1oid sha256:7b9efc5330a7ffab7e7ab59a71402d84a619cb21b5dadbe785b5e291f87497b3size 6601234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import torchimport torch.nn as nnimport torch.optim as optimimport numpy as npfrom safetensors.torch import save_file, load_file
epochs = 10000 # Step 1: Create a simple dataset# Features: [temperature, is_raining]# Labels: 1 (can run), 0 (cannot run)data = np.array([ [30, 0, 1], # 30°C, no rain -> can run [22, 1, 0], # 22°C, raining -> cannot run [25, 0, 1], # 25°C, no rain -> can run [15, 1, 0], # 15°C, raining -> cannot run [20, 0, 1], # 20°C, no rain -> can run])
# Normalize the featuresX = (data[:, :2] - np.mean(data[:, :2], axis=0)) / np.std(data[:, :2], axis=0)y = torch.tensor(data[:, 2], dtype=torch.float32).view(-1, 1) # Labels
# Step 2: Create a simple neural network modelclass SimpleNN(nn.Module): def __init__(self): super(SimpleNN, self).__init__() self.fc1 = nn.Linear(2, 8) # Increased number of neurons self.fc2 = nn.Linear(8, 4) # Added an additional hidden layer self.fc3 = nn.Linear(4, 1) # Hidden layer to output layer self.sigmoid = nn.Sigmoid() # Sigmoid activation function
def forward(self, x): x = self.fc1(x) x = nn.ReLU()(x) # ReLU activation function x = self.fc2(x) x = nn.ReLU()(x) x = self.fc3(x) return self.sigmoid(x) # Output
# Step 3: Train the modelmodel = SimpleNN()criterion = nn.BCELoss() # Binary cross-entropy lossoptimizer = optim.Adam(model.parameters(), lr=0.01) # Using Adam optimizer
# Training loopfor epoch in range(epochs): # Increased epochs model.train() optimizer.zero_grad() # Zero gradients output = model(torch.tensor(X, dtype=torch.float32)) # Forward pass loss = criterion(output, y) # Compute loss loss.backward() # Backward pass optimizer.step() # Update weights
if (epoch + 1) % 20 == 0: # Print every 20 epochs print(f'Epoch [{epoch + 1}/200], Loss: {loss.item():.4f}')
# Step 4: Save the model to Safetensors formatsave_file(model.state_dict(), f'WEATHER-RUN-{epochs}.safetensors')
# Step 5: Load and run the modeldef load_and_run_model(model_path, input_data): model = SimpleNN() model.load_state_dict(load_file(model_path)) # Load the model state model.eval() # Set model to evaluation mode
with torch.no_grad(): # No need to compute gradients input_tensor = torch.tensor(input_data, dtype=torch.float32) output = model(input_tensor) return output.numpy() # Return predictions as numpy array
# Testing the model with new datatest_data = [[25, 0], [18, 1], [21, 0], [19, 1]]normalized_test_data = (np.array(test_data) - np.mean(data[:, :2], axis=0)) / np.std(data[:, :2], axis=0)predictions = load_and_run_model(f'WEATHER-RUN-{epochs}.safetensors', normalized_test_data)
# Output predictionsfor (temp, rain), pred in zip(test_data, predictions): result = "can" if pred[0] >= 0.5 else "cannot" print(f"With temperature {temp}°C and {'rain' if rain else 'no rain'}, you {result} go for a run.")
WEATHER-RUN-10000.safetensorsAdded+3
model.pyAdded+80