-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
33 lines (25 loc) · 1.17 KB
/
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
# model.py
import torch
import torch.nn as nn
class NAICSClassifier(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
super(NAICSClassifier, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
# x shape: (batch_size, sequence_length)
# Pass the input through the embedding layer
embedded = self.embedding(x)
# embedded shape: (batch_size, sequence_length, embedding_dim)
# Pass through LSTM
lstm_out, _ = self.lstm(embedded)
# lstm_out shape: (batch_size, sequence_length, hidden_dim)
# Take the output from the last time step (for classification)
lstm_out = lstm_out[:, -1, :] # Taking output of the last time step
# lstm_out shape: (batch_size, hidden_dim)
# Pass the LSTM output through a fully connected layer
out = self.fc(lstm_out)
# out shape: (batch_size, output_dim)
return self.softmax(out)