-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_pytorch.py
More file actions
106 lines (86 loc) · 2.46 KB
/
10_pytorch.py
File metadata and controls
106 lines (86 loc) · 2.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
# Load Data
# torch.utils.data.Dataset()
# torch.utils.data.DataLoader()
class MyDataset(Dataset):
# read data and preprocess
def __init__(self, file):
# read data
# preprocess
self.data = file
# return one sample at a time
def __getitem__(self, index):
return self.data[index]
# return the size of the dataset
def __len__(self):
return len(self.data)
# file = None
# dataset = MyDataset(file)
# # 5组数据合并成一个[mini_]batch
# dataloader = DataLoader(dataset, batch_size=5, shuffle=False)
# Define Neural Network
# torch.nn.Linear()
# nn.Sigmoid()
# nn.ReLU()
class MyModel(nn.Module):
# Initialize you model
def __init__(self):
super(MyModel, self).__init__()
# define layers
self.net = nn.Sequential(
nn.Linear(10, 32),
nn.Sigmoid(),
nn.Linear(32, 1)
)
# Compute output of your NN
def forward(self, x):
return self.net(x)
# Entire Procedure
# Neural Network Training Setup
# S_1
file = None
dataset = MyDataset(file)
# S_2
# 5组数据合并成一个[mini_]batch
dataloader = DataLoader(dataset, batch_size=5, shuffle=False)
# S_3
model = MyModel().to() # device
# S_4
# Loss Function
model_output = None
expected_value = None
criterion = nn.CrossEntropyLoss() # nn.MSELoss()
loss = criterion(model_output, expected_value) # ======================
# S_5
# Optimization Algorithm
optimizer = torch.optim.SGD("") # (model.parameters(), lr, momentum=0)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Neural Network Training Loop
n_epochs = 1000
for epoch in range(n_epochs):
# set model to train model
model.train()
tr_set = []
for x, y in tr_set: # iterate through the dataloader
optimizer.zero_grad()
x, y = x.to(), y.to()
pred = model(x)
loss = criterion(pred, y)
loss.backward()
optimizer.step()
# Neural Network Validation Loop
# set model to test model
model.eval()
total_loss = 0
dv_set = []
for x, y in dv_set: # iterate through the dataloader
x, y = x.to(), y.to() # move data to device (cpu / cuda)
with torch.no_grad(): # disable gradient calculation # 1 提升速度 2 防止模型参数修正
pred = model(x)
loss = criterion(pred, y)
total_loss += loss.cpu().item() * len(x)
# avg_loss = total_loss / len(dv_set.dataset)