Last active
July 30, 2020 14:41
-
-
Save wcneill/dcbca03cca2f01df2d5e805312798a24 to your computer and use it in GitHub Desktop.
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
# Assume 26 unique characters | |
alphabet = ['a', 'b', ... , 'z'] | |
# two sample sequences, inputs and targets | |
x = np.array(list('abc')) # inputs | |
y = np.array(list('xyz')) # targets | |
# define one-hot encoder and label encoder | |
onehot_encoder = OneHotEncoder(sparse=False).fit(alphabet) | |
label_encoder = {ch: i for i, ch in enumerate(alphabet)} | |
# Use Cross Entropy Loss for classification problem | |
criterion = nn.CrossEntropyLoss() | |
# Transform input and targets | |
x = onehot_encoder.transform(x) | |
y = [label_encoder[ch] for ch in y] | |
y = torch.tensor(y) | |
# Define architecture: | |
input_size = 50 # representing the one-hot encoded vector size | |
hidden_size = 100 # number of hidden nodes in the LSTM layer | |
n_layers = 2 # number of LSTM layers | |
output_size = 50 # output of 50 scores for the next character | |
lstm = nn.LSTM(input_size, hidden_size, n_layers, batch_first=True) | |
linear = nn.Linear(hidden_size, output_size) | |
# feed forward | |
x = get_batches(data) # -> input x: (batch_size, seq_length, num_features) | |
x, hs = lstm(x, hs) # -> LSTM out: (batch_size, seq_length, hidden_size) | |
x = x.reshape(-1, hidden_size) # -> Linear in: (batch_size * seq_length, hidden_size) | |
x = linear(x) # -> Linear out: (batch_size * seq_length, out_size) | |
# calculate loss | |
loss = criterion(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment