Canstralian commited on
Commit
6dc5627
·
verified ·
1 Parent(s): 5fe2b43

Update model/pytorch_model.bin

Browse files
Files changed (1) hide show
  1. model/pytorch_model.bin +88 -0
model/pytorch_model.bin CHANGED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ texts = torch.randn(100, 10, input_size) # 100 samples, sequence length of 10
48
+ labels = torch.randint(0, output_size, (100,)) # 100 random labels
49
+
50
+ # Create a DataLoader
51
+ dataset = CustomDataset(texts, labels)
52
+ data_loader = DataLoader(dataset, batch_size=16, shuffle=True)
53
+
54
+ # Step 6: Training Loop
55
+ for epoch in range(num_epochs):
56
+ for inputs, targets in data_loader:
57
+ # Forward pass
58
+ outputs = model(inputs)
59
+ loss = criterion(outputs, targets)
60
+
61
+ # Backward pass and optimization
62
+ optimizer.zero_grad()
63
+ loss.backward()
64
+ optimizer.step()
65
+
66
+ print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
67
+
68
+ # Step 7: Save the Model
69
+ model_save_path = "model" # Change this to your desired path
70
+ os.makedirs(model_save_path, exist_ok=True) # Create the directory if it doesn't exist
71
+
72
+ # Save the model weights as pytorch_model.bin
73
+ torch.save(model.state_dict(), os.path.join(model_save_path, "pytorch_model.bin"))
74
+
75
+ # Step 8: Create and Save the Configuration File
76
+ config = {
77
+ "input_size": input_size,
78
+ "hidden_size": hidden_size,
79
+ "output_size": output_size,
80
+ "num_layers": 1, # Add more parameters as needed
81
+ "dropout": 0.2
82
+ }
83
+
84
+ # Save the configuration to a JSON file
85
+ with open(os.path.join(model_save_path, "config.json"), "w") as f:
86
+ json.dump(config, f)
87
+
88
+ print("Model and configuration saved successfully!")