-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_model.py
50 lines (37 loc) · 1.57 KB
/
simple_model.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
import torch
import torch.nn as nn
class SimpleDNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, dropout_prob=0.2):
super(SimpleDNN, self).__init__()
self.half_hidden_size = round(hidden_size / 2)
self.fc1 = nn.Linear(input_size, hidden_size)
self.bn1 = nn.BatchNorm1d(hidden_size)
self.fc2 = nn.Linear(hidden_size, self.half_hidden_size)
self.bn2 = nn.BatchNorm1d(self.half_hidden_size)
self.fc3 = nn.Linear(self.half_hidden_size, hidden_size)
self.bn3 = nn.BatchNorm1d(hidden_size)
self.fc4 = nn.Linear(hidden_size, output_size) # Output layer
# Activation function
self.relu = nn.ReLU()
# Dropout layer
self.dropout = nn.Dropout(dropout_prob)
def forward(self, x):
# First hidden layer
out = self.fc1(x)
out = self.bn1(out) # Apply batch normalization
out = self.relu(out)
out = self.dropout(out) # Apply dropout
# Second hidden layer
out = self.fc2(out)
out = self.bn2(out) # Apply batch normalization
out = self.relu(out)
# Third hidden layer
out = self.fc3(out)
out = self.bn3(out) # Apply batch normalization
out = self.relu(out)
# Output layer (no activation here, typically applied outside for regression/classification)
out = self.fc4(out)
return out
def load_model(input_size, hidden_size, output_size, dropout_prob=0.2):
model = SimpleDNN(input_size, hidden_size, output_size, dropout_prob)
return model