tutorial from ai is uhjhhhhh
This commit is contained in:
1 parent
5cf1364e7e
commit
6c632c9131
4 files changed
+242
-1
No files matched your search
@@ -0,0 +1,231 @@
|
||||
"""
|
||||
This script demonstrates how to port a TensorFlow LSTM model to PyTorch.
|
||||
It includes detailed explanations for every step.
|
||||
"""
|
||||
|
||||
# Step 1: Import necessary libraries
|
||||
# ----------------------------------
|
||||
# PyTorch is the core library for building and training neural networks.
|
||||
# Torch.nn contains modules for defining layers and loss functions.
|
||||
# Torch.optim provides optimizers like Adam for training.
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import torch
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
from torch import nn, optim
|
||||
from torch.utils.data import DataLoader, TensorDataset
|
||||
|
||||
from load_data import load_this_data
|
||||
|
||||
# Step 2: Data Preparation
|
||||
# ------------------------
|
||||
# In PyTorch, data must be converted into tensors. We'll simulate the data
|
||||
# preprocessing steps from your TensorFlow code.
|
||||
|
||||
|
||||
# Simulate some data (replace this with your actual data loading logic)
|
||||
np.random.seed(42) # For reproducibility
|
||||
# data = np.sin(np.linspace(0, 100, 1000)) # Example: Sine wave data
|
||||
# data = data.reshape(-1, 1) # Reshape to (samples, features)
|
||||
|
||||
|
||||
# Load the dataset (replace "dataset.csv" with your actual file path)
|
||||
data = load_this_data("dataset.csv")
|
||||
|
||||
|
||||
# Extract the "Humidity" column as the target variable
|
||||
data = data["Humidity"].values.reshape(-1, 1)
|
||||
|
||||
# Normalize the data using MinMaxScaler (same as in TensorFlow)
|
||||
scaler = MinMaxScaler()
|
||||
data_scaled = scaler.fit_transform(data)
|
||||
|
||||
# Define lookback window (sequence length for LSTM)
|
||||
n_lookback = 100 # Number of past timesteps to consider
|
||||
n_features = 1 # Number of features (e.g., 1 for univariate time series)
|
||||
|
||||
|
||||
# Helper function to create sequences
|
||||
def create_sequences(data, n_lookback):
|
||||
"""
|
||||
Create sequences of length n_lookback for LSTM input.
|
||||
|
||||
Args:
|
||||
data: Scaled data as a NumPy array or PyTorch tensor.
|
||||
n_lookback: Number of past timesteps to consider.
|
||||
|
||||
Returns:
|
||||
x: Input sequences of shape (num_samples, n_lookback, n_features).
|
||||
y: Target values of shape (num_samples, 1).
|
||||
"""
|
||||
x, y = [], []
|
||||
for i in range(len(data) - n_lookback):
|
||||
x.append(data[i : i + n_lookback])
|
||||
y.append(data[i + n_lookback])
|
||||
return np.array(x), np.array(y)
|
||||
|
||||
|
||||
# Split into training and testing sets (80% train, 20% test)
|
||||
train_data, test_data = train_test_split(data_scaled, test_size=0.2, shuffle=False)
|
||||
|
||||
# Create sequences for training and testing
|
||||
train_x, train_y = create_sequences(train_data, n_lookback)
|
||||
test_x, test_y = create_sequences(test_data, n_lookback)
|
||||
|
||||
# Convert sequences to PyTorch tensors
|
||||
train_x = torch.tensor(train_x, dtype=torch.float32)
|
||||
train_y = torch.tensor(train_y, dtype=torch.float32)
|
||||
test_x = torch.tensor(test_x, dtype=torch.float32)
|
||||
test_y = torch.tensor(test_y, dtype=torch.float32)
|
||||
|
||||
# Create datasets and dataloaders
|
||||
train_dataset = TensorDataset(train_x, train_y)
|
||||
test_dataset = TensorDataset(test_x, test_y)
|
||||
|
||||
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
|
||||
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)
|
||||
|
||||
# Step 3: Define the LSTM Model
|
||||
# -----------------------------
|
||||
# PyTorch models are defined by subclassing `nn.Module`.
|
||||
# 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.lstm = nn.LSTM(
|
||||
input_size=n_features, # Number of input features
|
||||
hidden_size=n_neurons, # Number of neurons in the LSTM
|
||||
num_layers=2, # Number of LSTM layers
|
||||
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):
|
||||
"""
|
||||
Forward pass through the model.
|
||||
|
||||
Args:
|
||||
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
|
||||
n_neurons = 64 # Number of neurons in the LSTM
|
||||
model = LSTMModel(n_features, n_lookback, n_neurons)
|
||||
|
||||
# Step 4: Define Loss Function and Optimizer
|
||||
# ------------------------------------------
|
||||
# Loss function: Mean Squared Error (MSE) for regression tasks.
|
||||
# Optimizer: Adam optimizer for efficient training.
|
||||
criterion = nn.MSELoss()
|
||||
optimizer = optim.Adam(model.parameters(), lr=0.0001)
|
||||
|
||||
# Step 5: Train the Model
|
||||
# -----------------------
|
||||
# PyTorch requires an explicit training loop. This gives you full control
|
||||
# over the training process.
|
||||
|
||||
epochs = 100 # Number of training epochs
|
||||
|
||||
# Check for CUDA and set the device
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
print(f"Using device: {device}")
|
||||
|
||||
# Move the model to the device
|
||||
model = LSTMModel(n_features, n_lookback, n_neurons).to(device)
|
||||
|
||||
# Move data to the device during training and evaluation
|
||||
for epoch in range(epochs):
|
||||
model.train() # Set the model to training mode
|
||||
train_loss = 0.0
|
||||
|
||||
for x_batch, y_batch in train_loader:
|
||||
# Move data to the device
|
||||
x_batch = x_batch.to(device)
|
||||
y_batch = y_batch.to(device)
|
||||
# Reshape input to (batch, seq_len, n_features)
|
||||
x_batch = x_batch.view(-1, n_lookback, n_features)
|
||||
y_batch = y_batch.view(-1, 1)
|
||||
|
||||
# Zero the gradients from the previous step
|
||||
optimizer.zero_grad()
|
||||
|
||||
# Forward pass: Compute predictions
|
||||
predictions = model(x_batch)
|
||||
|
||||
# Compute the loss
|
||||
loss = criterion(predictions, y_batch)
|
||||
|
||||
# Backward pass: Compute gradients
|
||||
loss.backward()
|
||||
|
||||
# Update model parameters
|
||||
optimizer.step()
|
||||
|
||||
# Accumulate the loss for reporting
|
||||
train_loss += loss.item()
|
||||
|
||||
# Print epoch loss
|
||||
print(f"Epoch {epoch+1}/{epochs}, Loss: {train_loss/len(train_loader):.4f}")
|
||||
|
||||
if epoch % 10 == 0: # Print every 10 epochs
|
||||
print(f"Sample prediction: {predictions[0].item()}, Actual: {y_batch[0].item()}")
|
||||
|
||||
# Step 6: Evaluate the Model
|
||||
# --------------------------
|
||||
# Switch to evaluation mode and disable gradient computation.
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
predictions = []
|
||||
actuals = []
|
||||
|
||||
for x_batch, y_batch in test_loader:
|
||||
# Move data to the device
|
||||
x_batch = x_batch.to(device)
|
||||
y_batch = y_batch.to(device)
|
||||
|
||||
# Reshape input to (batch, seq_len, n_features)
|
||||
x_batch = x_batch.view(-1, n_lookback, n_features)
|
||||
|
||||
# Make predictions
|
||||
preds = model(x_batch)
|
||||
|
||||
# Move predictions and actuals back to CPU for further processing
|
||||
predictions.append(preds.cpu().numpy())
|
||||
actuals.append(y_batch.cpu().numpy())
|
||||
|
||||
# Convert predictions and actuals to NumPy arrays for comparison
|
||||
predictions = np.concatenate(predictions)
|
||||
actuals = np.concatenate(actuals)
|
||||
|
||||
# Step 7: Post-Processing and Visualization
|
||||
# -----------------------------------------
|
||||
# Inverse transform the predictions and actuals to get original scale
|
||||
predictions = scaler.inverse_transform(predictions)
|
||||
actuals = scaler.inverse_transform(actuals)
|
||||
|
||||
# Plot the results
|
||||
|
||||
plt.figure(figsize=(20, 5))
|
||||
plt.plot(actuals, label="Actual")
|
||||
plt.plot(predictions, label="Predicted")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
@@ -0,0 +1,7 @@
|
||||
""" this uses new tech
|
||||
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
|
||||
"""
|
||||
|
||||
|
||||
Reference in new issue
Block a user