-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_model.py
52 lines (41 loc) · 1.37 KB
/
train_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
# Define a simple feedforward neural network using PyTorch
class SimpleNN(nn.Module):
def __init__(self, input_dim):
super(SimpleNN, self).__init__()
self.fc = nn.Linear(input_dim, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc(x)
x = self.sigmoid(x)
return x
# Load the dataset
dataset = np.load('dataset.npz')
X_train, y_train = dataset['X'], dataset['y']
# Reshape the target array to be a column vector
y_train = y_train.reshape(-1, 1)
# Initialize the model
model = SimpleNN(input_dim=X_train.shape[1])
# Define loss function and optimizer
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
# Convert data to PyTorch tensors
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.float32)
# Training loop
epochs = 100
for epoch in range(epochs):
# Forward pass
outputs = model(X_train_tensor)
loss = criterion(outputs, y_train_tensor)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 10 == 0:
print(f"Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}")
# Save the trained model parameters
torch.save(model.state_dict(), 'model.pth')