This commit is contained in:
lda committed 2025-05-07 02:14:26 +07:00
1 parent 2b408ceeab
commit 14c42ed117
4 files changed
+242 -43

No files matched your search

+3
View File
@@ -185,3 +185,6 @@ ipython_config.py
.conda*/ .conda*/
data/
# for rn
+6
View File
@@ -76,6 +76,12 @@ def validate_view(data: pd.DataFrame, verbose=False):
# if __name__ == "__main__": # if __name__ == "__main__":
def load_this_data(filename, verbose=False): def load_this_data(filename, verbose=False):
d = load_data(filename) d = load_data(filename)
# if verbose:
# print(d.columns)
d = date_combine(d) d = date_combine(d)
# if verbose:
# print(d.columns)
d = validate_view(d, verbose) d = validate_view(d, verbose)
# if verbose:
# print(d.columns)
return d return d
+115 -42
View File
@@ -16,6 +16,10 @@ from sklearn.preprocessing import MinMaxScaler
from torch import nn, optim from torch import nn, optim
from torch.utils.data import DataLoader, TensorDataset from torch.utils.data import DataLoader, TensorDataset
import sys
sys.path.append(r"c:\Users\Admin\Documents\lecture ass shit\ml1\codemidterm\ml_midterm")
from load_data import load_this_data from load_data import load_this_data
# Step 2: Data Preparation # Step 2: Data Preparation
@@ -66,25 +70,30 @@ def create_sequences(data, n_lookback):
return np.array(x), np.array(y) return np.array(x), np.array(y)
# Split into training and testing sets (80% train, 20% test) # Split into training and testing sets
train_data, test_data = train_test_split(data_scaled, test_size=0.2, shuffle=False) split_index = int(0.8 * len(data_scaled))
train_data = data_scaled[:split_index]
test_data = data_scaled[split_index:]
# Create sequences for training and testing # Create sequences for training and testing
train_x, train_y = create_sequences(train_data, n_lookback) train_x, train_y = create_sequences(train_data, n_lookback)
test_x, test_y = create_sequences(test_data, n_lookback) test_x, test_y = create_sequences(test_data, n_lookback)
# train_y = scaler.transform(train_y)
# test_y = scaler.transform(test_y)
# Convert sequences to PyTorch tensors # Convert sequences to PyTorch tensors
train_x = torch.tensor(train_x, dtype=torch.float32) train_x = torch.tensor(train_x, dtype=torch.float32)
train_y = torch.tensor(train_y, dtype=torch.float32) train_y = torch.tensor(train_y, dtype=torch.float32)
test_x = torch.tensor(test_x, dtype=torch.float32) test_x = torch.tensor(test_x, dtype=torch.float32)
test_y = torch.tensor(test_y, dtype=torch.float32) test_y = torch.tensor(test_y, dtype=torch.float32)
# Create datasets and dataloaders # Create datasets and dataloaders
train_dataset = TensorDataset(train_x, train_y) train_dataset = TensorDataset(train_x, train_y)
test_dataset = TensorDataset(test_x, test_y) test_dataset = TensorDataset(test_x, test_y)
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True) train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False) test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
# Step 3: Define the LSTM Model # Step 3: Define the LSTM Model
# ----------------------------- # -----------------------------
@@ -92,43 +101,74 @@ test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)
# The forward method specifies how data flows through the model. # The forward method specifies how data flows through the model.
# class LSTMModel(nn.Module):
# def __init__(self, n_features, n_lookback, n_neurons):
# """
# Initialize the LSTM model.
# Args:
# n_features: Number of input features (e.g., 1 for univariate time series).
# n_lookback: Number of past timesteps to consider.
# n_neurons: Number of neurons in each LSTM layer.
# """
# super(LSTMModel, self).__init__()
# self.lstm1 = nn.LSTM(
# n_features,
# n_neurons,
# batch_first=True,
# dropout=0.2,
# )
# self.lstm2 = nn.LSTM(
# n_neurons,
# n_neurons,
# batch_first=True,
# )
# self.lstm3 = nn.LSTM(n_neurons, n_neurons, batch_first=True)
# self.lstm4 = nn.LSTM(n_neurons, n_neurons, batch_first=True)
# self.fc = nn.Linear(n_neurons, 1)
# self.dropout = nn.Dropout(0.2)
# def forward(self, x):
# """
# Forward pass through the model.
# Args:
# x: Input tensor of shape (batch, seq_len, n_features).
# Returns:
# Output tensor of shape (batch, 1).
# """
# x, _ = self.lstm1(x)
# x, _ = self.lstm2(x)
# x, _ = self.lstm3(x)
# x, _ = self.lstm4(x)
# x = self.dropout(x)
# x = self.fc(x[:, -1, :]) # Use the last timestep's output
# return x
print("Input sequences (x):", train_x.shape)
print("Target values (y):", train_y.shape)
print("Sample input:", train_x[0])
print("Corresponding target:", train_y[0])
class LSTMModel(nn.Module): class LSTMModel(nn.Module):
def __init__(self, n_features, n_lookback, n_neurons): def __init__(self, n_features, n_lookback, n_neurons):
"""
Initialize the LSTM model.
Args:
n_features: Number of input features (e.g., 1 for univariate time series).
n_lookback: Number of past timesteps to consider.
n_neurons: Number of neurons in each LSTM layer.
"""
super(LSTMModel, self).__init__() super(LSTMModel, self).__init__()
self.lstm = nn.LSTM( self.lstm = nn.LSTM(n_features, n_neurons, batch_first=True, dropout=0.2)
input_size=n_features, # Number of input features self.lstm2 = nn.LSTM(n_neurons, n_neurons, batch_first=True, dropout=0.2)
hidden_size=n_neurons, # Number of neurons in the LSTM
num_layers=2, # Number of LSTM layers self.fc = nn.Linear(n_neurons, 1)
batch_first=True, # Input shape is (batch, seq, feature)
dropout=0.2, # Dropout for regularization
)
self.fc = nn.Linear(n_neurons, 1) # Fully connected layer for output
def forward(self, x): def forward(self, x):
""" x, _ = self.lstm(x)
Forward pass through the model. x, _ = self.lstm2(x)
x = self.fc(x[:, -1, :]) # Use the last timestep's output
Args: return x
x: Input tensor of shape (batch, seq_len, n_features).
Returns:
Output tensor of shape (batch, 1).
"""
lstm_out, _ = self.lstm(x) # LSTM outputs (ignore hidden states)
out = self.fc(lstm_out[:, -1, :]) # Use the last timestep's output
return out
# Instantiate the model # Instantiate the model
n_neurons = 64 # Number of neurons in the LSTM n_neurons = 128 # Number of neurons in the LSTM
model = LSTMModel(n_features, n_lookback, n_neurons) model = LSTMModel(n_features, n_lookback, n_neurons)
# Step 4: Define Loss Function and Optimizer # Step 4: Define Loss Function and Optimizer
@@ -136,14 +176,14 @@ model = LSTMModel(n_features, n_lookback, n_neurons)
# Loss function: Mean Squared Error (MSE) for regression tasks. # Loss function: Mean Squared Error (MSE) for regression tasks.
# Optimizer: Adam optimizer for efficient training. # Optimizer: Adam optimizer for efficient training.
criterion = nn.MSELoss() criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001) optimizer = optim.Adam(model.parameters(), lr=0.001)
# Step 5: Train the Model # Step 5: Train the Model
# ----------------------- # -----------------------
# PyTorch requires an explicit training loop. This gives you full control # PyTorch requires an explicit training loop. This gives you full control
# over the training process. # over the training process.
epochs = 100 # Number of training epochs epochs = 50 # Number of training epochs
# Check for CUDA and set the device # Check for CUDA and set the device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -152,6 +192,9 @@ print(f"Using device: {device}")
# Move the model to the device # Move the model to the device
model = LSTMModel(n_features, n_lookback, n_neurons).to(device) model = LSTMModel(n_features, n_lookback, n_neurons).to(device)
# scheduler = optim.lr_scheduler.ReduceLROnPlateau(
# optimizer, mode="min", patience=5, factor=0.5, min_lr=1e-6
# )
# Move data to the device during training and evaluation # Move data to the device during training and evaluation
for epoch in range(epochs): for epoch in range(epochs):
model.train() # Set the model to training mode model.train() # Set the model to training mode
@@ -163,23 +206,39 @@ for epoch in range(epochs):
y_batch = y_batch.to(device) y_batch = y_batch.to(device)
# Reshape input to (batch, seq_len, n_features) # Reshape input to (batch, seq_len, n_features)
x_batch = x_batch.view(-1, n_lookback, n_features) x_batch = x_batch.view(-1, n_lookback, n_features)
y_batch = y_batch.view(-1, 1) # y_batch = y_batch.view(-1, 1)
# print("x_batch shape:", x_batch.shape)
# print("y_batch shape:", y_batch.shape)
# print("x_batch dtype:", x_batch.dtype)
# print("y_batch dtype:", y_batch.dtype)
# break
# Zero the gradients from the previous step # Zero the gradients from the previous step
optimizer.zero_grad() optimizer.zero_grad()
# Forward pass: Compute predictions # Forward pass: Compute predictions
predictions = model(x_batch) predictions = model(x_batch)
# if epoch == 32:
# for name, param in model.named_parameters():
# if param.grad is not None:
# print(f"{name} gradient mean: {param.grad.mean().item()}")
# else:
# print(f"{name} has no gradient")
# print("Predictions device:", predictions.device)
# print("y_batch device:", y_batch.device)
# print("Predictions shape:", predictions.shape)
# print("y_batch shape:", y_batch.shape)
# Compute the loss # Compute the loss
loss = criterion(predictions, y_batch) loss = criterion(predictions, y_batch)
# Backward pass: Compute gradients # Backward pass: Compute gradients
loss.backward() loss.backward()
# Clip gradients to prevent exploding gradients
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
# Update model parameters # Update model parameters
optimizer.step() optimizer.step()
# scheduler.step(train_loss / len(train_loader))
# Accumulate the loss for reporting # Accumulate the loss for reporting
train_loss += loss.item() train_loss += loss.item()
@@ -187,8 +246,9 @@ for epoch in range(epochs):
print(f"Epoch {epoch+1}/{epochs}, Loss: {train_loss/len(train_loader):.4f}") print(f"Epoch {epoch+1}/{epochs}, Loss: {train_loss/len(train_loader):.4f}")
if epoch % 10 == 0: # Print every 10 epochs if epoch % 10 == 0: # Print every 10 epochs
print(f"Sample prediction: {predictions[0].item()}, Actual: {y_batch[0].item()}") print(
f"Sample prediction: {predictions[0].item()}, Actual: {y_batch[0].item()}"
)
# Step 6: Evaluate the Model # Step 6: Evaluate the Model
# -------------------------- # --------------------------
# Switch to evaluation mode and disable gradient computation. # Switch to evaluation mode and disable gradient computation.
@@ -203,7 +263,7 @@ with torch.no_grad():
y_batch = y_batch.to(device) y_batch = y_batch.to(device)
# Reshape input to (batch, seq_len, n_features) # Reshape input to (batch, seq_len, n_features)
x_batch = x_batch.view(-1, n_lookback, n_features) # x_batch = x_batch.view(-1, n_lookback, n_features)
# Make predictions # Make predictions
preds = model(x_batch) preds = model(x_batch)
@@ -223,9 +283,22 @@ predictions = scaler.inverse_transform(predictions)
actuals = scaler.inverse_transform(actuals) actuals = scaler.inverse_transform(actuals)
# Plot the results # Plot the results
print("Sample scaled train_y:", train_y[:5])
print("Sample scaled test_y:", test_y[:5])
print("Predictions shape:", predictions.shape)
print("Actuals shape:", actuals.shape)
print("Sample inverse-transformed predictions:", predictions[:5])
print("Sample inverse-transformed actuals:", actuals[:5])
plt.figure(figsize=(20, 5)) plt.figure(figsize=(20, 5))
plt.plot(actuals, label="Actual") plt.plot(actuals, label="Actual")
plt.plot(predictions, label="Predicted") plt.plot(predictions, label="Predicted")
plt.legend() plt.legend()
plt.show() plt.show()
from sklearn.metrics import r2_score, mean_squared_error
print("R² Score:", r2_score(actuals, predictions))
print("MSE:", mean_squared_error(actuals, predictions))
+118 -1
View File
@@ -1,7 +1,124 @@
""" this uses new tech """this uses new tech
make by conda new -p path python=3.12; conda activate -p path; conda install all those shi make by conda new -p path python=3.12; conda activate -p path; conda install all those shi
conda install pytorch torchvision torchaudio pytorch-cuda -c pytorch -c nvidia conda install pytorch torchvision torchaudio pytorch-cuda -c pytorch -c nvidia
""" """
import sys
sys.path.append(".")
from load_data import load_this_data
import torch
# a = load_this_data("dataset.csv")
x = torch.tensor([[1, 2], [3, 4]])
print(x)
print(x.shape) # Output: torch.Size([2, 2])
print(x.dtype) # Output: torch.int64
print(x.device) # Output: cpu
y = torch.ones_like(x)
print(x + y)
print(x * y)
x = torch.randn(3, requires_grad=True)
y = x * 2
z = y.mean()
z.backward()
print(x.grad)
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.fc = nn.Linear(2, 1)
def forward(self, x):
return self.fc(x)
model = SimpleNet()
sample_input = torch.tensor([[1.0, 2.0]])
output = model(sample_input)
print(output)
import torch.optim as optim
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
for epoch in range(100):
optimizer.zero_grad()
outputs = model(sample_input)
loss = criterion(outputs, torch.tensor([[1.0]]))
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/100], Loss: {loss.item():.4f}')
model.eval()
sample_input_2 = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
output_2 = model(sample_input_2)
print(output_2)
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor()])
# Download and load the training data
train_dataset = datasets.MNIST(root='data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
# import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28 * 28) # Flatten the input
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
model_2 = Net()
criterion_2 = nn.CrossEntropyLoss()
optimizer_2 = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(35): # loop over the dataset multiple times
for inputs, labels in train_loader:
optimizer_2.zero_grad() # zero the parameter gradients
outputs = model_2(inputs) # forward
loss = criterion_2(outputs, labels) # compute loss
loss.backward() # backward
optimizer_2.step() # optimize
print(f'Epoch {epoch+1}, Loss: {loss.item()}')
test_dataset = datasets.MNIST(root='data', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model_2(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy: {100 * correct / total}%')