def __init__(self, widths, vocab_size=5000):
from keras.models import Sequential
from keras.layers import Embedding, Dense, TimeDistributedMerge
from keras.layers.advanced_activations import ELU
from keras.preprocessing.sequence import pad_sequences
from keras.optimizers import SGD
self.n_classes = widths[-1]
self.vocab_size = vocab_size
self.word_to_int = {}
self.int_to_word = np.ndarray(shape=(vocab_size+1,), dtype='int64')
self.model = Sequential()
self.model.add(Embedding(vocab_size, widths[0]))
self.model.add(TimeDistributedMerge(mode='ave'))
for width in widths[1:-1]:
layer = Dense(output_dim=hidden_width, init='he_normal', activation=ELU(1.0))
self.model.add(layer)
self.model.add(
Dense(
n_classes,
init='zero',
activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
self.model.compile(loss='categorical_crossentropy', optimizer=sgd)
评论列表
文章目录