-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_crepe.py
133 lines (89 loc) · 4.98 KB
/
py_crepe.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# coding: utf-8
# In[4]:
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Convolution1D, MaxPooling1D
# In[3]:
def build_model(classes, filter_kernels, dense_outputs, maxlen, vocab_size, nb_filter):
#Define what the input shape looks like
model = Sequential()
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[0],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size),
name = 'conv1'))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool1'))
"""
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[1],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv2'))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool2))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[2],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv3'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[3],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv4'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[4],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv5'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[5],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv6'))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool3'))
"""
model.add(Flatten())
model.add(Dense(dense_outputs, activation='relu', name = 'dense1'))
model.add(Dropout(0.5, name = 'dropout1'))
"""
model.add(Dense(dense_outputs, activation='relu', name = 'desne2'))
model.add(Dropout(0.5, name = dropout2))
"""
model.add(Dense(classes, activation='softmax', name='output'))
sgd = SGD(lr=0.01, momentum=0.9, nesterov= True)
model.compile(loss='categorical_crossentropy', optimizer=sgd,
metrics=['accuracy'])
filepath = 'params/crepe_model_weights.h5'
return (model, sgd, filepath)
def build_feature_model(classes, filter_kernels, dense_outputs, maxlen, vocab_size, nb_filter, sgd, filepath):
#Define what the input shape looks like
model = Sequential()
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[0],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size),
name = 'conv1', trainable = False))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool1', trainable = False))
"""
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[1],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv2'))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool2))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[2],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv3'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[3],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv4'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[4],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv5'))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[5],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size), name = 'conv6'))
model.add(MaxPooling1D(pool_length=3, name = 'maxpool3'))
"""
model.add(Flatten())
model.add(Dense(dense_outputs, activation='relu', name = 'dense1', trainable = False))
model.add(Dropout(0.5, name = 'dropout1', trainable = False))
"""
model.add(Dense(dense_outputs, activation='relu', name = 'desne2'))
model.add(Dropout(0.5, name = dropout2))
"""
model.add(Dense(classes, activation='softmax', name='output', trainable = False))
model.load_weights(filepath)
model.pop() #pop dense output
model.pop() #pop dropout
model.compile(loss='categorical_crossentropy', optimizer=sgd,
metrics=['accuracy'])
return (model)
# In[ ]: