def create_model(self, n_timesteps=None, batch_size=1, include_pred_layer=True):
input_layers = []
seq_input_layer = Input(batch_shape=(batch_size, n_timesteps), name="seq_input_layer")
input_layers.append(seq_input_layer)
seq_embedding_layer = Embedding(input_dim=self.lexicon_size + 1,
output_dim=self.n_embedding_nodes, mask_zero=True, name='seq_embedding_layer')(seq_input_layer)
for layer_num in range(self.n_hidden_layers):
if layer_num == 0:
seq_hidden_layer = GRU(output_dim=self.n_hidden_nodes, return_sequences=True, stateful=True, name='seq_hidden_layer1')(seq_embedding_layer)
else: #add extra hidden layers
seq_hidden_layer = GRU(output_dim=self.n_hidden_nodes, return_sequences=True, stateful=True, name='seq_hidden_layer' + str(layer_num + 1))(seq_hidden_layer)
if self.use_pos:
pos_input_layer = Input(batch_shape=(batch_size, n_timesteps), name="pos_input_layer")
input_layers.append(pos_input_layer)
pos_embedding_layer = Embedding(input_dim=self.n_pos_tags + 1,
output_dim=self.n_pos_embedding_nodes, mask_zero=True, name='pos_embedding_layer')(pos_input_layer)
pos_hidden_layer = GRU(output_dim=self.n_pos_nodes, return_sequences=True, stateful=True, name='pos_hidden_layer')(pos_embedding_layer)
seq_hidden_layer = merge([seq_hidden_layer, pos_hidden_layer], mode='concat', concat_axis=-1, name='pos_merge_hidden_layer')
if self.use_features:
feature_input_layer = Input(batch_shape=(batch_size, self.lexicon_size + 1), name="feature_input_layer")
input_layers.append(feature_input_layer)
feature_hidden_layer = Dense(output_dim=self.n_feature_nodes, activation='sigmoid', name='feature_hidden_layer')(feature_input_layer)
feature_hidden_layer = RepeatVector(n_timesteps)(feature_hidden_layer)
seq_hidden_layer = merge([seq_hidden_layer, feature_hidden_layer], mode='concat', concat_axis=-1, name='feature_merge_hidden_layer')
output_layers = []
if include_pred_layer:
pred_layer = TimeDistributed(Dense(self.lexicon_size + 1, activation="softmax", name='pred_layer'))(seq_hidden_layer)
output_layers.append(pred_layer)
if self.use_pos:
pred_pos_layer = TimeDistributed(Dense(self.n_pos_tags + 1, activation="softmax", name='pred_pos_layer'))(seq_hidden_layer)
output_layers.append(pred_pos_layer)
model = Model(input=input_layers, output=output_layers)
#select optimizer and compile
model.compile(loss="sparse_categorical_crossentropy",
optimizer=eval(self.optimizer)(clipvalue=self.clipvalue, lr=self.lr, decay=self.decay))
return model
评论列表
文章目录