-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN_training.py
More file actions
100 lines (79 loc) · 3.1 KB
/
NN_training.py
File metadata and controls
100 lines (79 loc) · 3.1 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import pickle
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
from sklearn.model_selection import train_test_split
data_dict = pickle.load(open('./data.pickle', 'rb'))
# Preprocess your data
data = np.array(data_dict['data'])
labels = np.array(data_dict['labels'])
# Convert data to PyTorch tensors
data = torch.tensor(data, dtype=torch.float32)
labels = torch.tensor(labels, dtype=torch.long)
# Function to pad or truncate sequences to a fixed length
def pad_sequences(sequences, maxlen):
padded_sequences = np.zeros((len(sequences), maxlen))
for i, seq in enumerate(sequences):
if len(seq) > maxlen:
padded_sequences[i, :] = seq[:maxlen]
else:
padded_sequences[i, :len(seq)] = seq
return padded_sequences
# Set the desired fixed length for all sequences
fixed_length = 100 # Adjust this value based on your data
# Pad the sequences to the fixed length
data = torch.tensor(data, dtype=torch.float32)
labels = torch.tensor(labels, dtype=torch.long)
# Split the data into training and test sets
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)
print("x_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("x_test shape:", x_test.shape)
print("y_test shape:", y_test.shape)
# Create TensorDatasets and DataLoaders
train_dataset = TensorDataset(x_train, y_train)
test_dataset = TensorDataset(x_test, y_test)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# Define your neural network architecture
class NeuralNet(nn.Module):
def __init__(self, input_size, num_classes):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
# Set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyperparameters
input_size = data.shape[1]
num_classes = len(set(labels.numpy()))
num_epochs = 10
learning_rate = 0.001
# Initialize your neural network
model = NeuralNet(input_size, num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
for batch_idx, (data, targets) in enumerate(train_loader):
# Move data to device
data = data.to(device)
targets = targets.to(device)
# Forward pass
scores = model(data)
loss = criterion(scores, targets)
# Backward pass and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}')
# Evaluate the model
# Here you would write code to evaluate the model on your test set