-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
83 lines (64 loc) · 2.62 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional, TimeDistributed, Activation, Convolution1D, MaxPool1D
from keras.models import Model
from keras.preprocessing import sequence
# config
use_dropout = True
def LSTM_Model(vocab_size, embedding_size, hidden_size, n_classes, num_steps):
model = Sequential()
model.add(Embedding(vocab_size, embedding_size))
model.add(LSTM(hidden_size, return_sequences=True, stateful=False))
if use_dropout:
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Activation('softmax'))
model.summary()
return model
def LSTM2Layer_Model(vocab_size, embedding_size, hidden_size, n_classes, num_steps):
model = Sequential()
model.add(Embedding(vocab_size, embedding_size))
model.add(LSTM(hidden_size, return_sequences=True, stateful=False))
model.add(LSTM(hidden_size, return_sequences=True, stateful=False))
if use_dropout:
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Activation('softmax'))
model.summary()
return model
def BiLSTM_Model(vocab_size, embedding_size, hidden_size, n_classes, num_steps):
model = Sequential()
model.add(Embedding(vocab_size, embedding_size))
model.add(Bidirectional(LSTM(hidden_size, return_sequences=True, stateful=False)))
if use_dropout:
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Activation('softmax'))
model.summary()
return model
def CLSTM(vocab_size, embedding_size, hidden_size, n_classes, num_steps):
model = Sequential()
model.add(Embedding(vocab_size, embedding_size))
model.add(Convolution1D(128, 3, padding='same', strides=1))
model.add(Activation('relu'))
model.add(LSTM(hidden_size, return_sequences=True, stateful=False))
if use_dropout:
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Activation('softmax'))
model.summary()
return model
def CBiLSTM(vocab_size, embedding_size, hidden_size, n_classes, num_steps):
model = Sequential()
model.add(Embedding(vocab_size, embedding_size))
model.add(Convolution1D(128, 3, padding='same', strides=1))
model.add(Activation('relu'))
model.add(Bidirectional(LSTM(hidden_size, return_sequences=True, stateful=False)))
if use_dropout:
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Activation('softmax'))
model.summary()
return model
if __name__ == "__main__":
CBiLSTM(10000, 256, 128, 3, 20)