-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.py
51 lines (41 loc) · 1.17 KB
/
test1.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
import torch
from torch import nn
model = nn.Sequential(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 1),
nn.Sigmoid()
)
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
# print(m.weight.data.size())
if len(m.weight.data.size()) == 2:
for i in range(m.weight.data.size()[0]):
for j in range(m.weight.data.size()[1]):
m.weight.data[i][j] = 0.00005
else:
for i in range(m.weight.data.size()[0]):
m.weight.data[i] = 0.00005
for i in range(m.bias.data.size()[0]):
m.bias.data[i] = 0.00005
# print(m.bias.data)
model.apply(weights_init)
for param in model.parameters():
print(param.data)
x = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(x)
for i in range(0, 10):
y_ = model(x)
print(y_)
loss = y_ - 1
print(loss)
loss.backward()
print('------')
for f in model.parameters():
print(f.grad.data)
print('------')
learning_rate = 1
for f in model.parameters():
f.data.sub_(learning_rate * f.grad.data)
f.grad.data.zero_()