-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
87 additions
and
300 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from src.mLSTMCell import mLSTMCell | ||
|
||
# Generate a sine wave | ||
num_points = 100 | ||
time = np.linspace(0, 4 * np.pi, num_points) | ||
data = np.sin(time) | ||
data = torch.tensor(data, dtype=torch.float32) | ||
|
||
# Hyperparameters | ||
input_size = 1 | ||
hidden_size = 10 | ||
output_size = 1 | ||
num_layers = 1 | ||
learning_rate = 0.01 | ||
num_epochs = 500 | ||
|
||
# Model, loss function, and optimizer | ||
model = mLSTMCell(input_size, hidden_size) | ||
criterion = nn.MSELoss() | ||
optimizer = optim.Adam(model.parameters(), lr=learning_rate) | ||
|
||
for epoch in range(num_epochs): | ||
|
||
hidden = model.init_hidden(1) | ||
|
||
optimizer.zero_grad() | ||
loss = 0 | ||
|
||
for i in range(num_points - 1): | ||
inputs = data[i].view(1, 1) | ||
targets = data[i + 1].view(1, 1) | ||
outputs, hidden = model(inputs, hidden) | ||
loss += criterion(outputs, targets) | ||
|
||
loss.backward() | ||
optimizer.step() | ||
|
||
if epoch % 10 == 0: | ||
print(f"Epoch {epoch}, Loss: {loss.item()}") | ||
|
||
# Test the model | ||
model.eval() | ||
with torch.no_grad(): | ||
predictions = [] | ||
hidden = model.init_hidden(1) | ||
for i in range(num_points - 1): | ||
inputs = data[i].view(1, 1) | ||
outputs, hidden = model(inputs, hidden) | ||
predictions.append(outputs.item()) | ||
|
||
# Plot the results | ||
plt.figure(figsize=(12, 6)) | ||
plt.title(f"mLSTM - Original vs Predicted Sine Wave, hidden_size={hidden_size}") | ||
plt.plot(time[1:], data[1:], label="Original") | ||
plt.plot(time[1:], predictions, label="Predicted") | ||
plt.legend() | ||
plt.savefig(f"images/mLSTMCell_{hidden_size}.png") | ||
plt.show() |
Oops, something went wrong.