This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomLSTM(nn.Module): | |
| def __init__(self, input_sz, hidden_sz, peephole=False): | |
| super().__init__() | |
| self.input_sz = input_sz | |
| self.hidden_size = hidden_sz | |
| self.peephole = peephole | |
| self.W = nn.Parameter(torch.Tensor(input_sz, hidden_sz * 4)) | |
| self.U = nn.Parameter(torch.Tensor(hidden_sz, hidden_sz * 4)) | |
| self.bias = nn.Parameter(torch.Tensor(hidden_sz * 4)) | |
| self.init_weights() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomLSTM(nn.Module): | |
| def __init__(self, input_sz, hidden_sz): | |
| super().__init__() | |
| self.input_sz = input_sz | |
| self.hidden_size = hidden_sz | |
| self.W = nn.Parameter(torch.Tensor(input_sz, hidden_sz * 4)) | |
| self.U = nn.Parameter(torch.Tensor(hidden_sz, hidden_sz * 4)) | |
| self.bias = nn.Parameter(torch.Tensor(hidden_sz * 4)) | |
| self.init_weights() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward(self, | |
| x, | |
| init_states=None): | |
| """ | |
| assumes x.shape represents (batch_size, sequence_size, input_size) | |
| """ | |
| bs, seq_sz, _ = x.size() | |
| hidden_seq = [] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def init_weights(self): | |
| stdv = 1.0 / math.sqrt(self.hidden_size) | |
| for weight in self.parameters(): | |
| weight.data.uniform_(-stdv, stdv) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NaiveCustomLSTM(nn.Module): | |
| def __init__(self, input_sz: int, hidden_sz: int): | |
| super().__init__() | |
| self.input_size = input_sz | |
| self.hidden_size = hidden_sz | |
| #i_t | |
| self.U_i = nn.Parameter(torch.Tensor(input_sz, hidden_sz)) | |
| self.V_i = nn.Parameter(torch.Tensor(hidden_sz, hidden_sz)) | |
| self.b_i = nn.Parameter(torch.Tensor(hidden_sz)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import torch | |
| import torch.nn as nn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| epoch_bar = tqdm(range(10), | |
| desc="Training", | |
| position=0, | |
| total=2) | |
| acc = 0 | |
| for epoch in epoch_bar: | |
| batch_bar = tqdm(enumerate(train_loader), | |
| desc="Epoch: {}".format(str(epoch)), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ds_train = torch.utils.data.TensorDataset(X_train, y_train) | |
| train_loader = torch.utils.data.DataLoader(ds_train, batch_size=64, shuffle=True) | |
| ds_test = torch.utils.data.TensorDataset(X_test, y_test) | |
| test_loader = torch.utils.data.DataLoader(ds_test, batch_size=64, shuffle=True) | |
| classifier = Net() | |
| device = torch.device('cpu') | |
| optimizer = optim.Adam(classifier.parameters(), lr=.002) | |
| criterion = nn.CrossEntropyLoss() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Net(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.embedding = nn.Embedding(len(encoder.vocab)+1, 32) | |
| self.lstm = nn.LSTM(32, 32, batch_first=True) | |
| self.fc1 = nn.Linear(32, 2) | |
| def forward(self, x): | |
| x_ = self.embedding(x) | |
| x_, (h_n, c_n) = self.lstm(x_) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| padded_dataset = [] | |
| for i in tqdm(range(len(reviews))): | |
| padded_dataset.append(pad_tensor(reviews[i], int(max_pad_length))) | |
| X = torch.stack(padded_dataset) | |
| y = torch.tensor(labels) | |
| #train test splitting | |
| X_train, X_test, y_train, y_test = train_test_split(X, | |
| y, |
NewerOlder