This file contains 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 torch | |
from torch import nn | |
class BiLSTM(nn.Module): | |
def __init__(self, input_dim, embedding_dim, hidden_dim): | |
super().__init__() | |
self.input_dim = input_dim | |
self.embedding_dim = embedding_dim | |
self.hidden_dim = hidden_dim | |
self.encoder = nn.Embedding(input_dim, embedding_dim) | |
self.lstm = nn.LSTM(embedding_dim, hidden_dim, |
This file contains 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): | |
self.encoder.weight.data.uniform_(-0.5, 0.5) | |
ih = (param.data for name, param in self.named_parameters() if 'weight_ih' in name) | |
hh = (param.data for name, param in self.named_parameters() if 'weight_hh' in name) | |
b = (param.data for name, param in self.named_parameters() if 'bias' in name) | |
for t in ih: | |
nn.init.xavier_uniform(t) | |
for t in hh: | |
nn.init.orthogonal(t) | |
for t in b: |
This file contains 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 torch | |
from torch import nn | |
class BiLSTM(nn.Module): | |
def __init__(self, input_dim, embedding_dim, hidden_dim): | |
super().__init__() | |
self.input_dim = input_dim | |
self.embedding_dim = embedding_dim | |
self.hidden_dim = hidden_dim | |
self.encoder = nn.Embedding(input_dim, embedding_dim) | |
self.lstm = nn.LSTM(embedding_dim, hidden_dim, |
This file contains 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
from tensorflow.keras.layers import Bidirectional, LSTM, Dense, Embedding | |
from tensorflow.keras.models import Sequential | |
model = Sequential([ | |
Embedding(input_dim, embedding_dim), | |
Bidirectional(LSTM(hidden_dim, return_sequences=True)), | |
Bidirectional(LSTM(hidden_dim)), | |
Dense(1, activation="sigmoid") | |
]) | |
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=["accuracy"]) | |
model.summary() |
This file contains 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
from torch.utils.data import TensorDataset, DataLoader | |
train_data = TensorDataset(torch.from_numpy(x_train), torch.from_numpy(y_train)) | |
valid_data = TensorDataset(torch.from_numpy(x_val), torch.from_numpy(y_val)) | |
train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size, drop_last=True) | |
valid_loader = DataLoader(valid_data, shuffle=True, batch_size=batch_size, drop_last=True) |
This file contains 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
index_offset = 3 | |
word_index = imdb.get_word_index(path="imdb_word_index.json") | |
word_index = {k: (v + index_offset) for k,v in word_index.items()} | |
word_index["<PAD>"] = 0 | |
word_index["<START>"] = 1 | |
word_index["<UNK>"] = 2 | |
word_index["<UNUSED>"] = 3 | |
index_to_word = { v: k for k, v in word_index.items()} | |
def recover_text(sample, index_to_word): |
This file contains 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
from tensorflow.keras.preprocessing.sequence import pad_sequences | |
maxlen = 200 | |
x_train = pad_sequences(x_train, maxlen=maxlen) | |
x_val = pad_sequences(x_val, maxlen=maxlen) |
This file contains 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
from tensorflow.keras.datasets import imdb | |
input_dim = 20000 | |
(x_train, y_train), (x_val, y_val) = imdb.load_data(num_words=input_dim) | |
print(len(x_train), "Training sequences") | |
print(len(x_val), "Validation sequences") |
This file contains 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 torch | |
criterion = nn.BCELoss() | |
optimizer = torch.optim.Adam(model.parameters()) | |
model = CustomModel() |
This file contains 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
from torch import nn | |
from torch.nn.functional import softmax | |
class CustomModel(nn.Module): | |
def __init__(self, vocab_size=50, | |
embedding_dim=16, | |
hidden_size=8): | |
super().__init__() | |
self.encoder = nn.Embedding(vocab_size, embedding_dim) | |
self.lstm = nn.LSTM(embedding_dim, hidden_size) | |
self.linear = nn.Linear(hidden_size, 1) |