-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path2-4.py
80 lines (65 loc) · 2.16 KB
/
2-4.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
73
74
75
76
77
78
79
80
#%%
# Starting off, we need to import torch
import torch
#%%
# here are the inputs and outputs from the last video
# as well as the model with activations
#%%
inputs = torch.rand(1, 1, 64, 64)
outputs = torch.rand(1, 2)
model = torch.nn.Sequential(
torch.nn.Linear(64, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 2),
)
#%%
# now we have our inputs as sample data and our outputs
# we are trying to generate with our network -- the
# application of a network -- it's computation to turn input
# into output is done with a forward pass simply by calling
# the model as a function over the inputs
#%%
test_results = model(inputs)
test_results
#%%
# don't forget the loss function! the loss function is the
# key driver to compute gradients, which are needed to drive
# learning
#%%
loss = torch.nn.MSELoss()(test_results, outputs)
loss
#%%
# now we compute the gradients, this is done for each forward
# pass after you have compute the loss -- basically you are
# zeroing out as the gradients will differ on each pass
# once the gradients are zeroed, then you use the loss to
# drive the backward propagation of gradients through the model
# this is pretty much the heart of what pytorch does for you --
# automatically computing gradients and propagating them back
#%%
model.zero_grad()
loss.backward()
#%%
# and now -- we'll do some very simple learning -- remember
# that the gradients tell you how far away you are from the
# right answer -- so you move in the opposite direction to
# get to the answer, meaning -- we just subtract!
# one additional concept here -- learning rate, which we'll
# experiment with more in later videos, but for now we'll use
# a very simple constant learning rate
#%%
learning_rate = 0.001
for parameter in model.parameters():
parameter.data -= parameter.grad.data * learning_rate
#%%
# now -- we've learned -- and should be closer to the answer
# let's run the model with our new updated parameters
# and see...
#%%
after_learning = model(inputs)
loss_after_learning = torch.nn.MSELoss()(after_learning, outputs)
loss_after_learning
#%%
# yep -- that's a smaller loss, we are closer to the answer