-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_approximator.py
72 lines (61 loc) · 1.86 KB
/
function_approximator.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
import numpy as np
import torch.optim as optim
from seaborn import scatterplot
# create random points
Train = torch.rand(400,2)
Test = torch.rand(100,2)
#create labels
f = lambda x: np.sin(10*x)
decider = lambda x: 0.0 if f(x[0])>x[1] else 1.0
target = [decider(x) for x in Train]
#Plot training data
plt.figure()
scatterplot(np.array(Train).T[0],np.array(Train).T[1], hue=target)
h = np.arange(0,1,.02)
plt.plot(h,f(h), color='r')
plt.ylim(0,1)
plt.title("Training Data Set")
class MyNetwork(nn.Module):
# Neuronal Network
def __init__(self):
super(MyNetwork, self).__init__()
self.lin1 = nn.Linear(2,16)
self.lin2 = nn.Linear(16,32)
self.lin3 = nn.Linear(32,128)
self.lin5 = nn.Linear(128,1)
def forward(self, x):
# Feed forward function
x = F.relu(self.lin1(x))
x = F.relu(self.lin2(x))
x = F.relu(self.lin3(x))
x = self.lin5(x)
return x
netz = MyNetwork()
for _ in range(5000):
# training the network
# feed forward
input = Variable(Train)
out = netz(input)
#Compute loss
target_nn = Variable(torch.tensor( [[t] for t in target] ))
criterion = nn.MSELoss()
loss = criterion(out, target_nn)
#backpropagation
netz.zero_grad()
loss.backward()
optimizer = optim.SGD(netz.parameters(), lr=.05)
optimizer.step()
cut = lambda x: 1 if x>0.5 else 0
training_output_scores = np.ravel(netz(Variable(Test)).detach().numpy())
#plot test data
plt.figure()
scatterplot(np.array(Test).T[0],np.array(Test).T[1], hue= [cut(x) for x in training_output_scores] )
plt.plot(h,f(h), color='r')
plt.ylim(0,1)
plt.title("Test Data Set")
plt.show()