-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpeddinti_model.py
66 lines (50 loc) · 1.92 KB
/
peddinti_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
""" model implementation
This is a simplified version that aims to test the TDNN layer.
"""
from keras.models import Sequential
from TDNN_layer import TDNNLayer
import numpy as np
class PeddintiModel:
def __init__(self):
self.model = None
self.input_dim = 0
""" create_model
This function will create the model as defined in Peddinti's paper
param:
input_dim: input data dimension
"""
def create_model(self, input_dim):
self.input_dim = input_dim
self.model = Sequential()
self.model.add(TDNNLayer([-2, 2], sub_sampling=False, input_shape=(input_dim, 1)))
self.model.add(TDNNLayer([-1, 2], sub_sampling=True))
self.model.add(TDNNLayer([-3, 2], sub_sampling=True))
self.model.add(TDNNLayer([-7, 2], sub_sampling=True, activation="softmax"))
self.model.compile(optimizer='Adam', loss="categorical_crossentropy", metrics=['accuracy'])
self.model.summary()
""" train_model
This function will train the model.
The training is performed in batch. This function can be called multiple time to train on more data.
param:
data: the data to train on
truth: the truth table
"""
def train_model(self):
data = np.random.random((3200, self.input_dim, 1))
truth = np.round(np.random.random((3200, self.input_dim - 21)))
self.model.fit(data, truth, epochs=20)
""" evaluate_model
This function will return the model evaluation
param:
data: the data to evaluate
truth: the truth table
"""
def evaluate_model(self):
data = np.random.random((3200, self.input_dim, 1))
truth = np.round(np.random.random((3200, self.input_dim - 21)))
return self.model.evaluate(data, truth)
if __name__ == "__main__":
model = PeddintiModel()
model.create_model(32)
model.train_model()
model.evaluate_model()