Created
May 22, 2024 20:52
-
-
Save Eeman1113/3ad64b7076a46e208bf7ddf6154a2a3d 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
import pyflashlight | |
import pyflashlight.nn as nn | |
import pyflashlight.optim as optim | |
import random | |
import math | |
random.seed(1) | |
class MyModel(nn.Module): | |
def __init__(self): | |
super(MyModel, self).__init__() | |
self.fc1 = nn.Linear(1, 10) | |
self.sigmoid = nn.Sigmoid() | |
self.fc2 = nn.Linear(10, 1) | |
def forward(self, x): | |
out = self.fc1(x) | |
out = self.sigmoid(out) | |
out = self.fc2(out) | |
return out | |
device = "cuda" | |
epochs = 10 | |
model = MyModel().to(device) | |
criterion = nn.MSELoss() | |
optimizer = optim.SGD(model.parameters(), lr=0.001) | |
loss_list = [] | |
x_values = [0. , 0.4, 0.8, 1.2, 1.6, 2. , 2.4, 2.8, 3.2, 3.6, 4. , | |
4.4, 4.8, 5.2, 5.6, 6. , 6.4, 6.8, 7.2, 7.6, 8. , 8.4, | |
8.8, 9.2, 9.6, 10. , 10.4, 10.8, 11.2, 11.6, 12. , 12.4, 12.8, | |
13.2, 13.6, 14. , 14.4, 14.8, 15.2, 15.6, 16. , 16.4, 16.8, 17.2, | |
17.6, 18. , 18.4, 18.8, 19.2, 19.6, 20.] | |
y_true = [] | |
for x in x_values: | |
y_true.append(math.pow(math.sin(x), 2)) | |
for epoch in range(epochs): | |
for x, target in zip(x_values, y_true): | |
x = pyflashlight.Tensor([[x]]).T | |
target = pyflashlight.Tensor([[target]]).T | |
x = x.to(device) | |
target = target.to(device) | |
outputs = model(x) | |
loss = criterion(outputs, target) | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
print(f'Epoch [{epoch + 1}/{epochs}], Loss: {loss[0]:.4f}') | |
loss_list.append(loss[0]) | |
# Epoch [1/10], Loss: 1.7035 | |
# Epoch [2/10], Loss: 0.7193 | |
# Epoch [3/10], Loss: 0.3068 | |
# Epoch [4/10], Loss: 0.1742 | |
# Epoch [5/10], Loss: 0.1342 | |
# Epoch [6/10], Loss: 0.1232 | |
# Epoch [7/10], Loss: 0.1220 | |
# Epoch [8/10], Loss: 0.1241 | |
# Epoch [9/10], Loss: 0.1270 | |
# Epoch [10/10], Loss: 0.1297 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment