-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpredict_def.py
164 lines (131 loc) · 6.56 KB
/
predict_def.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import string
import numpy as np
import pandas as pd
from keras.models import Model
from keras.optimizers import SGD
from keras.layers import Input, Dense, Dropout, Flatten
from keras.layers.convolutional import Convolution1D, MaxPooling1D
def create_vocab_set():
alphabet = (list(string.ascii_lowercase) + list(string.digits) +
list(string.punctuation) + ['\n'] + [' '] )
vocab_size = len(alphabet)
check = set(alphabet)
vocab = {}
reverse_vocab = {}
for ix, t in enumerate(alphabet):
vocab[t] = ix
reverse_vocab[ix] = t
return vocab, reverse_vocab, vocab_size, check
def model(filter_kernels, dense_outputs, maxlen, vocab_size, nb_filter,
cat_output):
inputs = Input(shape=(maxlen, vocab_size), name='input', dtype='float32')
conv = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[0],
border_mode='valid', activation='relu',
input_shape=(maxlen, vocab_size))(inputs)
conv = MaxPooling1D(pool_length=3)(conv)
conv1 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[1],
border_mode='valid', activation='relu')(conv)
conv1 = MaxPooling1D(pool_length=3)(conv1)
conv2 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[2],
border_mode='valid', activation='relu')(conv1)
# conv3 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[3],
# border_mode='valid', activation='relu')(conv2)
# conv4 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[4],
# border_mode='valid', activation='relu')(conv3)
# conv5 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[5],---------
# border_mode='valid', activation='relu')(conv4)
# conv5 = MaxPooling1D(pool_length=3)(conv5)
conv5 = Flatten()(conv2)
#Two dense layers with dropout of .5
z = Dropout(0.5)(Dense(dense_outputs, activation='relu')(conv5))
# z = Dropout(0.5)(Dense(dense_outputs, activation='relu')(z))
pred = Dense(cat_output, activation='softmax', name='output')(z)
model = Model(input=inputs, output=pred)
sgd = SGD(lr=0.01, momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd,
metrics=['accuracy'])
return model
def model2(filter_kernels, dense_outputs, maxlen, vocab_size, nb_filter,
cat_output): # For Character Embedding use this model instead of above model
d = 300 #Embedding Size
Embedding_layer = Embedding(vocab_size+1, d, input_length=maxlen)
inputs = Input(shape=(maxlen,), name='input', dtype='float32')
embed = Embedding_layer(inputs)
conv = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[0],
border_mode='valid', activation='relu',
input_shape=(maxlen, d))(embed)
conv = MaxPooling1D(pool_length=3)(conv)
conv1 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[1],
border_mode='valid', activation='relu')(conv)
conv1 = MaxPooling1D(pool_length=3)(conv1)
conv2 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[2],
border_mode='valid', activation='relu')(conv1)
# conv3 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[3],
# border_mode='valid', activation='relu')(conv2)
# conv4 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[4],
# border_mode='valid', activation='relu')(conv3)
# conv5 = Convolution1D(nb_filter=nb_filter, filter_length=filter_kernels[5],---------
# border_mode='valid', activation='relu')(conv4)
conv5 = MaxPooling1D(pool_length=3)(conv2)
conv = Flatten()(conv)
#Two dense layers with dropout of .5
z = Dropout(0.5)(Dense(dense_outputs, activation='relu')(conv))
# z = Dropout(0.5)(Dense(dense_outputs, activation='relu')(z))
pred = Dense(cat_output, activation='softmax', name='output')(z)
model = Model(input=inputs, output=pred)
sgd = SGD(lr=0.001, momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd,
metrics=['accuracy'])
return model
def mini_batch_generator(x, y, vocab, vocab_size, vocab_check, maxlen,
batch_size=128):
for i in range(0, len(x), batch_size):
x_sample = x[i:i + batch_size]
y_sample = y[i:i + batch_size]
input_data = encode_data(x_sample, maxlen, vocab, vocab_size,
vocab_check)
#input_data = encode_data2(x_sample, maxlen, vocab, vocab_size,
vocab_check)
yield (input_data, y_sample)
def encode_data(x, maxlen, vocab, vocab_size, check):
input_data = np.zeros((len(x), maxlen, vocab_size))
for dix, sent in enumerate(x):
counter = 0
sent_array = np.zeros((maxlen, vocab_size))
chars = list(sent.lower())
for c in chars:
if counter >= maxlen:
pass
else:
char_array = np.zeros(vocab_size, dtype=np.int)
if c in check:
ix = vocab[c]
char_array[ix] = 1
sent_array[counter, :] = char_array
counter += 1
input_data[dix, :, :] = sent_array
return input_data
def encode_data2(x, maxlen, vocab, vocab_size, check): # For character embedding use this function instead of encode_data
input_data = np.zeros((len(x), maxlen))
for dix, sent in enumerate(x):
counter = 0
chars = list(sent.lower().replace(" ", ""))
chars2 = []
for i in range(len(chars)-1):
chars2.append(chars[i] + chars[i+1])
for i,c in enumerate(chars2):
if counter >= maxlen:
pass
else:
if c in check:
counter += 1
ix = vocab[c]
input_data[dix,counter-1] = ix
return input_data
def shuffle_matrix(x, y):
print (x.shape, y.shape)
stacked = np.hstack((np.matrix(x), y))
np.random.shuffle(stacked)
xi = np.array(stacked[:, 0]).flatten()
yi = np.array(stacked[:, 1:])
return xi, yi