-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathff_classifier.py
46 lines (37 loc) · 1011 Bytes
/
ff_classifier.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
# coding=utf-8
"""
Python module for softmax binary classifier neural network
"""
import torch
import torch.nn as nn
def init_weights(net):
"""
initialize the weights of a network
:param model:
:return:
"""
# init parameters
def init_module(m):
if type(m) == nn.Linear:
nn.init.xavier_normal(m.weight.data)
nn.init.xavier_uniform(m.bias.data)
net.apply(init_module)
return net
def build_ff_classifier(input_size, hidden_1_size, hidden_2_size, hidden_3_size, num_labels=2):
"""
Constructs a neural net binary classifer
:param input_size:
:param hidden_size:
:param num_labels:
:return:
"""
net = nn.Sequential(
nn.Linear(input_size, hidden_1_size),
nn.ReLU(),
nn.Linear(hidden_1_size, hidden_2_size),
nn.ReLU(),
nn.Linear(hidden_2_size, hidden_3_size),
nn.ReLU(),
nn.Linear(hidden_3_size, num_labels),
nn.LogSoftmax(dim=1))
return net