-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTorch_01.py
More file actions
38 lines (29 loc) · 760 Bytes
/
PyTorch_01.py
File metadata and controls
38 lines (29 loc) · 760 Bytes
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
import torch
x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]
#learning rate
lr = 1e-2
#initialize weight with a random value
w = 1.0
#model
def forward(x):
return x * w
#loss function
def loss(x,y):
y_pred = forward(x)
return (y_pred -y) ** 2
#compute gradient
def gradient(x,y):
return 2 * x * (x * w -y)
#before training
print("predict (before training)",4,forward(4))
#training loop
for epoch in range(10):
for x_val,y_val in zip(x_data,y_data):
grad = gradient(x_val,y_val)
w = w - lr * grad
print("\tgrad: ",x_val,y_val,grad)
l = loss(x_val,y_val)
print("progress: ",epoch,"w= ",w,"loss=",l)
#after training
print("predict (after training)",4,forward(4))