-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinewave.py
More file actions
56 lines (45 loc) · 1.54 KB
/
Copy pathsinewave.py
File metadata and controls
56 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import torch
import numpy as np
from matplotlib.animation import FuncAnimation
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
# define the neural network architecture
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(1, 20)
self.fc2 = nn.Linear(20, 20)
self.fc3 = nn.Linear(20, 1)
def forward(self, x):
x = torch.relu(self.fc1(x)) # ReLU activation function
x = torch.relu(self.fc2(x))
return self.fc3(x)
# generate a sine wave dataset
x = torch.unsqueeze(torch.linspace(-np.pi, np.pi, 200), dim=1)
y = torch.sin(x)
# instantiate the network, loss function, and optimizer
net = Net()
criterion = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=0.05)
fig, ax = plt.subplots(figsize=(10, 4))
ax.set_xlim(-np.pi, np.pi)
ax.set_ylim(-1.5, 1.5)
true_function, = ax.plot(x.numpy(), y.numpy(), 'b^', markersize=5, label='True function')
nn_approximation, = ax.plot([], [], 'r-', lw=2, label='NN approximation')
ax.legend()
def init():
nn_approximation.set_data([], [])
return nn_approximation,
def animate(i):
# training step
y_pred = net(x)
loss = criterion(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# update the NN approximation line
nn_approximation.set_data(x.data.numpy(), y_pred.data.numpy())
return nn_approximation,
ani = FuncAnimation(fig, animate, frames=np.arange(1, 100), init_func=init, blit=True, interval=50, repeat=False)
plt.show()