|
| 1 | +import tensorflow as tf |
| 2 | + |
| 3 | +class StackedBiLSTMClassifier(tf.keras.Model): |
| 4 | + def __init__(self, feature_columns, units=64, stack_size=1, n_classes=2): |
| 5 | + """StackedBiLSTMClassifier |
| 6 | + :param feature_columns: All columns must be embedding of sequence column with same sequence_length. |
| 7 | + :type feature_columns: list[tf.embedding_column]. |
| 8 | + :param units: Units for LSTM layer. |
| 9 | + :type units: int. |
| 10 | + :param stack_size: number of bidirectional LSTM layers in the stack, default 1. |
| 11 | + :type stack_size: int. |
| 12 | + :param n_classes: Target number of classes. |
| 13 | + :type n_classes: int. |
| 14 | + """ |
| 15 | + super(StackedBiLSTMClassifier, self).__init__() |
| 16 | + |
| 17 | + self.feature_layer = tf.keras.experimental.SequenceFeatures(feature_columns) |
| 18 | + self.stack_bilstm = [] |
| 19 | + self.stack_size = stack_size |
| 20 | + if stack_size > 1: |
| 21 | + for i in range(stack_size - 1): |
| 22 | + self.stack_bilstm.append( |
| 23 | + tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units, return_sequences=True)) |
| 24 | + ) |
| 25 | + self.lstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units)) |
| 26 | + self.pred = tf.keras.layers.Dense(n_classes, activation='softmax') |
| 27 | + |
| 28 | + def call(self, inputs): |
| 29 | + x, seq_len = self.feature_layer(inputs) |
| 30 | + seq_mask = tf.sequence_mask(seq_len) |
| 31 | + if self.stack_size > 1: |
| 32 | + for i in range(self.stack_size - 1): |
| 33 | + x = self.stack_bilstm[i](x, mask=seq_mask) |
| 34 | + x = self.lstm(x, mask=seq_mask) |
| 35 | + return self.pred(x) |
| 36 | + |
| 37 | + def default_optimizer(self): |
| 38 | + """Default optimizer name. Used in model.compile.""" |
| 39 | + return 'adam' |
| 40 | + |
| 41 | + def default_loss(self): |
| 42 | + """Default loss function. Used in model.compile.""" |
| 43 | + return 'categorical_crossentropy' |
| 44 | + |
| 45 | + def default_training_epochs(self): |
| 46 | + """Default training epochs. Used in model.fit.""" |
| 47 | + return 1 |
| 48 | + |
| 49 | + def prepare_prediction_column(self, prediction): |
| 50 | + """Return the class label of highest probability.""" |
| 51 | + return prediction.argmax(axis=-1) |
| 52 | + |
0 commit comments