-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegressionModel.py
More file actions
69 lines (54 loc) · 2.8 KB
/
RegressionModel.py
File metadata and controls
69 lines (54 loc) · 2.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
#####A very simple model learning with PyTorch
###Import bare minimum torch module
import torch
### Set the seed so as to get same result with different runs
torch.manual_seed(1.0)
###data
"""Important Observation: Here we have used a model with 3 variables. Now if the number of inputs are less than 3 than it becomes
difficult for the network to learn a proper function and instead it overfits the data with some parameters (a,b,c). In order to get
proper network parameters its necessary to have at least as many data points as there are parameters in the model to be learned.
For example in this case if we take number of data points to be less than or equal to 3 model converges but not to the expected parameters"""
#####Define number of data points training model, play by changing this
Number_Of_Datapoints = 100
#####Generate number of random datapoints for creating input dataset
x = torch.randn((1, Number_Of_Datapoints), requires_grad=False)
####Define ground truth model parameters to be learned by training
a_gt = torch.randn(1, requires_grad=False)
b_gt = torch.randn(1, requires_grad=False)
c_gt = torch.randn(1, requires_grad=False)
#####prepare the expected ground truth output, using a quadratic model
y_gt = a_gt * x *x + b_gt*x + c_gt
#print(y_gt, x)
#########Initialize parameter values with a random value
a = torch.randn(1, requires_grad=True)
b = torch.randn(1, requires_grad=True)
c = torch.randn(1, requires_grad=True)
####Define learning parameters
epochs = 10000 ### Number of epochs
lr = 0.01 ### Learning rate
#####Creating a loop to iterate for learning the parameters
for i in range(epochs):
#####Generate output with the initialized parameters by using all the data points in every epoch
y = a*x*x + b*x + c
###Compute the loss which is mean square loss
loss = torch.mean((y-y_gt).pow(2))
###Accumulate the gradient of loss with respect to the parameters
loss.backward()
####Update the model parameters using gradient descent
a.data = a.data - lr * a.grad.data
b.data = b.data - lr * b.grad.data
c.data = c.data - lr * c.grad.data
"""
Initialize the grad data for each of the parameters to zero because in every backward pass (loss.backward()),
newly calculated gradient is added to the previously calculated gradient, which we donot want and hence after updating the
parameters these field are made zero for accumulating gradient in the next back propagation.
"""
a.grad.data.fill_(0.0)
b.grad.data.fill_(0.0)
c.grad.data.fill_(0.0)
if i % 1000 == 0:
print('Loss {}, A {}, B {}, C{}'.format(loss.item(), a.data, b.data, c.data))
print('GT_a {}, Learned {}'.format(a_gt.data, a.data))
print('GT_b {}, Learned {}'.format(b_gt.data, b.data))
print('GT_c {}, Learned {}'.format(c_gt.data, c.data))
#print('Y_learned {}'.format(a.data*x*x + b.data * x + c.data))