-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastNet.py
More file actions
48 lines (40 loc) · 1.12 KB
/
fastNet.py
File metadata and controls
48 lines (40 loc) · 1.12 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
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
net2=torch.nn.Sequential(
torch.nn.Linear(2,10),
torch.nn.ReLU(),
torch.nn.Linear(10,2),
)
print(net2)
def save():
net1=torch.nn.Sequential(
torch.nn.Linear(1,10),
torch.nn.ReLU(),
torch.nn.Linear(10,1)
)
optimizer=torch.optim.SGD(net1.parameters(),lr=0.5)
loss_func=torch.nn.MSELoss()
for t in range(100):
prediction=net1(x)
loss=loss_func(prediction,y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(net1,'net.pkl')
torch.save(net1.state_dict(),'net_params.pkl')
plt.figure(1,figsize=(10,3))
plt.subplot(131)
plt.title('Net1')
plt.scatter(x.data.numpy(),y.data.numpy())
plt.plot(x.data.numpy,prediction.data.numpy(),'r-',lw=5)
def restore_net():
net2=torch.load('net.pkl')
def restore_params():
net3==torch.nn.Sequential(
torch.nn.Linear(1,10),
torch.nn.ReLU(),
torch.nn.Linear(10,1)
)
net3.load_state_dict(torch.load('net_params.pkl'))