Skip to content

Commit

Permalink
support change optimizer type when initialize a model and add missed …
Browse files Browse the repository at this point in the history
…drop layer to some models
  • Loading branch information
Alex Wang committed Feb 27, 2019
1 parent 3443c02 commit a517ab8
Showing 1 changed file with 87 additions and 35 deletions.
122 changes: 87 additions & 35 deletions kashgari/tasks/classification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class CNNModel(ClassificationModel):
},
'activation_layer': {
'activation': 'softmax'
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
'optimizer': 'adam',
'metrics': ['accuracy']
}
}

Expand All @@ -54,9 +59,7 @@ def build_model(self):
dense_2_layer = Dense(len(self.label2idx), **self.hyper_parameters['activation_layer'])(dense_1_layer)

model = Model(base_model.inputs, dense_2_layer)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.compile(**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()

Expand All @@ -70,6 +73,11 @@ class BLSTMModel(ClassificationModel):
},
'activation_layer': {
'activation': 'softmax'
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
'optimizer': 'adam',
'metrics': ['accuracy']
}
}

Expand All @@ -80,9 +88,7 @@ def build_model(self):
output_layers = [dense_layer]

model = Model(base_model.inputs, output_layers)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.compile(**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()

Expand All @@ -104,6 +110,11 @@ class CNNLSTMModel(ClassificationModel):
},
'activation_layer': {
'activation': 'softmax'
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
'optimizer': 'adam',
'metrics': ['accuracy']
}
}

Expand All @@ -117,16 +128,17 @@ def build_model(self):
output_layers = [dense_layer]

model = Model(base_model.inputs, output_layers)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.compile(**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()


class AVCNNModel(ClassificationModel):
__architect_name__ = 'AVCNNModel'
__base_hyper_parameters__ = {
'spatial_dropout': {
'rate': 0.25
},
'conv_0': {
'filters': 300,
'kernel_size':1,
Expand Down Expand Up @@ -201,15 +213,21 @@ class AVCNNModel(ClassificationModel):
'adam_optimizer': {
'lr': 1e-3,
'decay': 1e-7
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
#'optimizer': 'adam',
'metrics': ['accuracy']
}
}

def build_model(self):
base_model = self.embedding.model
conv_0 = Conv1D(**self.hyper_parameters['conv_0'])(base_model.output)
conv_1 = Conv1D(**self.hyper_parameters['conv_1'])(base_model.output)
conv_2 = Conv1D(**self.hyper_parameters['conv_2'])(base_model.output)
conv_3 = Conv1D(**self.hyper_parameters['conv_3'])(base_model.output)
embedded_seq = SpatialDropout1D(**self.hyper_parameters['spatial_dropout'])(base_model.output)
conv_0 = Conv1D(**self.hyper_parameters['conv_0'])(embedded_seq)
conv_1 = Conv1D(**self.hyper_parameters['conv_1'])(embedded_seq)
conv_2 = Conv1D(**self.hyper_parameters['conv_2'])(embedded_seq)
conv_3 = Conv1D(**self.hyper_parameters['conv_3'])(embedded_seq)

maxpool_0 = GlobalMaxPooling1D()(conv_0)
attn_0 = AttentionWeightedAverage()(conv_0)
Expand Down Expand Up @@ -242,9 +260,8 @@ def build_model(self):

model = Model(base_model.inputs, output)
adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer=adam_optimizer,
metrics=['accuracy'])
model.compile(optimizer=adam_optimizer,
**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()

Expand Down Expand Up @@ -312,6 +329,11 @@ class KMaxCNNModel(ClassificationModel):
'adam_optimizer': {
'lr': 1e-3,
'decay': 1e-7
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
#'optimizer': 'adam',
'metrics': ['accuracy']
}
}

Expand Down Expand Up @@ -353,16 +375,18 @@ def build_model(self):

model = Model(base_model.inputs, output)
adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer=adam_optimizer,
metrics=['accuracy'])
model.compile(optimizer=adam_optimizer,
**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()


class RCNNModel(ClassificationModel):
__architect_name__ = 'RCNNModel'
__base_hyper_parameters__ = {
'spatial_dropout': {
'rate': 0.2
},
'rnn_0': {
'units': 64,
'return_sequences': True
Expand Down Expand Up @@ -395,12 +419,18 @@ class RCNNModel(ClassificationModel):
'lr': 1e-3,
'clipvalue': 5,
'decay': 1e-5
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
#'optimizer': 'adam',
'metrics': ['accuracy']
}
}

def build_model(self):
base_model = self.embedding.model
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(base_model.output)
embedded_seq = SpatialDropout1D(**self.hyper_parameters['spatial_dropout'])(base_model.output)
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(embedded_seq)
conv_0 = Conv1D(**self.hyper_parameters['conv_0'])(rnn_0)
maxpool = GlobalMaxPooling1D()(conv_0)
attn = AttentionWeightedAverage()(conv_0)
Expand All @@ -415,16 +445,18 @@ def build_model(self):

model = Model(base_model.inputs, output)
adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer=adam_optimizer,
metrics=['accuracy'])
model.compile(optimizer=adam_optimizer,
**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()


class AVRNNModel(ClassificationModel):
__architect_name__ = 'AVRNNModel'
__base_hyper_parameters__ = {
'spatial_dropout': {
'rate': 0.25
},
'rnn_0': {
'units': 60,
'return_sequences': True
Expand Down Expand Up @@ -457,12 +489,18 @@ class AVRNNModel(ClassificationModel):
'lr': 1e-3,
'clipvalue': 5,
'decay': 1e-6
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
#'optimizer': 'adam',
'metrics': ['accuracy']
}
}

def build_model(self):
base_model = self.embedding.model
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(base_model.output)
embedded_seq = SpatialDropout1D(**self.hyper_parameters['spatial_dropout'])(base_model.output)
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(embedded_seq)
rnn_1 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_1']))(rnn_0)
concat_rnn = concatenate([rnn_0, rnn_1],
**self.hyper_parameters['concat_rnn'])
Expand All @@ -481,16 +519,18 @@ def build_model(self):

model = Model(base_model.inputs, output)
adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer=adam_optimizer,
metrics=['accuracy'])
model.compile(optimizer=adam_optimizer,
**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()


class DropoutBGRUModel(ClassificationModel):
__architect_name__ = 'DropoutBGRUModel'
__base_hyper_parameters__ = {
'spatial_dropout': {
'rate': 0.15
},
'rnn_0': {
'units': 64,
'return_sequences': True
Expand All @@ -517,12 +557,18 @@ class DropoutBGRUModel(ClassificationModel):
},
'activation_layer': {
'activation': 'softmax'
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
'optimizer': 'adam',
'metrics': ['accuracy']
}
}

def build_model(self):
base_model = self.embedding.model
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(base_model.output)
embedded_seq = SpatialDropout1D(**self.hyper_parameters['spatial_dropout'])(base_model.output)
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(embedded_seq)
dropout_rnn = Dropout(**self.hyper_parameters['dropout_rnn'])(rnn_0)
rnn_1 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_1']))(dropout_rnn)
last = Lambda(lambda t: t[:, -1], name='last')(rnn_1)
Expand All @@ -539,16 +585,17 @@ def build_model(self):

model = Model(base_model.inputs, output)
# adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.compile(**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()


class DropoutAVRNNModel(ClassificationModel):
__architect_name__ = 'DropoutAVRNNModel'
__base_hyper_parameters__ = {
'spatial_dropout': {
'rate': 0.25
},
'rnn_0': {
'units': 56,
'return_sequences': True
Expand Down Expand Up @@ -584,12 +631,18 @@ class DropoutAVRNNModel(ClassificationModel):
'lr': 1e-3,
'clipvalue': 5,
'decay': 1e-7
},
'optimizer_param': {
'loss': 'categorical_crossentropy',
#'optimizer': 'adam',
'metrics': ['accuracy']
}
}

def build_model(self):
base_model = self.embedding.model
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(base_model.output)
embedded_seq = SpatialDropout1D(**self.hyper_parameters['spatial_dropout'])(base_model.output)
rnn_0 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_0']))(embedded_seq)
rnn_dropout = SpatialDropout1D(**self.hyper_parameters['rnn_dropout'])(rnn_0)
rnn_1 = Bidirectional(GRULayer(**self.hyper_parameters['rnn_1']))(rnn_dropout)

Expand All @@ -608,9 +661,8 @@ def build_model(self):

model = Model(base_model.inputs, output)
adam_optimizer = optimizers.Adam(**self.hyper_parameters['adam_optimizer'])
model.compile(loss='categorical_crossentropy',
optimizer=adam_optimizer,
metrics=['accuracy'])
model.compile(optimizer=adam_optimizer,
**self.hyper_parameters['optimizer_param'])
self.model = model
self.model.summary()

Expand Down

0 comments on commit a517ab8

Please sign in to comment.