-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
66 lines (49 loc) · 2.24 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
from torch import nn
from collections import OrderedDict
import math
import numpy as np
def get_classifier(features_count, classifier_hidden=0, dropout_p=0.5):
''' Get a new classifier for the neural network
In:
features_count : number of inputs to the classifier
classifier_hidden : number of hidden layers in
the classifier
dropout_p : dropout probability for the hidden layers
Out:
classifier : new classifier (nn.Sequential)
'''
# Return a classifier based on number of features,
# number of hidden layers, and dropout probability
output = ('output', nn.LogSoftmax(dim=1))
classifier_spec = OrderedDict([])
in_count = features_count
#Number of final outputs
out_final = 102
i = 0
if classifier_hidden > 0:
#Transform the node count range to log2 scale
feat_log = math.floor(np.log2(features_count))
out_fin_log = math.ceil(np.log2(out_final))
# Linearly interpolate hidden layer counts in log2
# calculate distance
dist = ((feat_log - out_fin_log) / (classifier_hidden + 1))
# Build hidden layer output counts in log2 and
# return tonormal scale by taking exp
hidden_outputs = [int(np.exp2(math.floor(feat_log - i * dist))) for i in range(1, classifier_hidden + 1)]
# Check for duplicates
if len(hidden_outputs) != len(set(hidden_outputs)):
raise ValueError(str(classifier_hidden) + ' is too many hidden layers. Try 2 or 3.')
for i in range(classifier_hidden):
indeces = ['fc' + str(i + 1), 'relu' + str(i + 1), 'dropout' + str(i + 1)]
classifier_spec.update({indeces[0] : nn.Linear(in_count, hidden_outputs[i])})
classifier_spec.update({indeces[1] : nn.ReLU()})
classifier_spec.update({indeces[2] : nn.Dropout(p=dropout_p)})
in_count = hidden_outputs[i]
# Getting final class labels
if i > 0:
i += 1
classifier_spec.update({'fc' + str(i + 1) : nn.Linear(in_count, 102)})
classifier_spec.update({'output' : nn.LogSoftmax(dim=1)})
classifier = nn.Sequential(classifier_spec)
return classifier