Create train_and_save_model.py
Browse files- train_and_save_model.py +89 -0
train_and_save_model.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.optim as optim
|
| 4 |
+
from torch.utils.data import DataLoader, Dataset
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Step 1: Define Your Dataset Class
|
| 9 |
+
class CustomDataset(Dataset):
|
| 10 |
+
def __init__(self, texts, labels):
|
| 11 |
+
self.texts = texts
|
| 12 |
+
self.labels = labels
|
| 13 |
+
|
| 14 |
+
def __len__(self):
|
| 15 |
+
return len(self.texts)
|
| 16 |
+
|
| 17 |
+
def __getitem__(self, idx):
|
| 18 |
+
return self.texts[idx], self.labels[idx]
|
| 19 |
+
|
| 20 |
+
# Step 2: Define Your Model Class
|
| 21 |
+
class LSTMModel(nn.Module):
|
| 22 |
+
def __init__(self, input_size, hidden_size, output_size):
|
| 23 |
+
super(LSTMModel, self).__init__()
|
| 24 |
+
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
|
| 25 |
+
self.fc = nn.Linear(hidden_size, output_size)
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
lstm_out, _ = self.lstm(x)
|
| 29 |
+
out = self.fc(lstm_out[:, -1, :]) # Get the last time step output
|
| 30 |
+
return out
|
| 31 |
+
|
| 32 |
+
# Step 3: Initialize Hyperparameters and Model
|
| 33 |
+
input_size = 100 # Example input size (e.g., embedding size)
|
| 34 |
+
hidden_size = 64 # Number of LSTM units
|
| 35 |
+
output_size = 10 # Number of output classes
|
| 36 |
+
num_epochs = 5
|
| 37 |
+
learning_rate = 0.001
|
| 38 |
+
|
| 39 |
+
# Initialize the model
|
| 40 |
+
model = LSTMModel(input_size, hidden_size, output_size)
|
| 41 |
+
|
| 42 |
+
# Step 4: Set Up Loss and Optimizer
|
| 43 |
+
criterion = nn.CrossEntropyLoss()
|
| 44 |
+
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
|
| 45 |
+
|
| 46 |
+
# Step 5: Sample Data (You would replace this with your actual data)
|
| 47 |
+
# Here, we create random data for demonstration purposes
|
| 48 |
+
texts = torch.randn(100, 10, input_size) # 100 samples, sequence length of 10
|
| 49 |
+
labels = torch.randint(0, output_size, (100,)) # 100 random labels
|
| 50 |
+
|
| 51 |
+
# Create a DataLoader
|
| 52 |
+
dataset = CustomDataset(texts, labels)
|
| 53 |
+
data_loader = DataLoader(dataset, batch_size=16, shuffle=True)
|
| 54 |
+
|
| 55 |
+
# Step 6: Training Loop
|
| 56 |
+
for epoch in range(num_epochs):
|
| 57 |
+
for inputs, targets in data_loader:
|
| 58 |
+
# Forward pass
|
| 59 |
+
outputs = model(inputs)
|
| 60 |
+
loss = criterion(outputs, targets)
|
| 61 |
+
|
| 62 |
+
# Backward pass and optimization
|
| 63 |
+
optimizer.zero_grad()
|
| 64 |
+
loss.backward()
|
| 65 |
+
optimizer.step()
|
| 66 |
+
|
| 67 |
+
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
|
| 68 |
+
|
| 69 |
+
# Step 7: Save the Model
|
| 70 |
+
model_save_path = "path/to/save/model_directory" # Change this to your desired path
|
| 71 |
+
os.makedirs(model_save_path, exist_ok=True) # Create the directory if it doesn't exist
|
| 72 |
+
|
| 73 |
+
# Save the model weights
|
| 74 |
+
torch.save(model.state_dict(), os.path.join(model_save_path, "pytorch_model.bin"))
|
| 75 |
+
|
| 76 |
+
# Step 8: Create and Save the Configuration File
|
| 77 |
+
config = {
|
| 78 |
+
"input_size": input_size,
|
| 79 |
+
"hidden_size": hidden_size,
|
| 80 |
+
"output_size": output_size,
|
| 81 |
+
"num_layers": 1, # Add more parameters as needed
|
| 82 |
+
"dropout": 0.2
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
# Save the configuration to a JSON file
|
| 86 |
+
with open(os.path.join(model_save_path, "config.json"), "w") as f:
|
| 87 |
+
json.dump(config, f)
|
| 88 |
+
|
| 89 |
+
print("Model and configuration saved successfully!")
|