def __init__(self, encoder_path, word_index, index_word, encoder_trainable=False, hidden_size=768, dropout=.25, n_layers=1, **kwargs): super(ClassificationModel, self).__init__(**kwargs) self.encoder_trainable = encoder_trainable self.to_token = ToTokens(word_index) self.to_tags = ToTags(index_word) self.encoder_layer = get_encoder(encoder_path, encoder_trainable) self.masking = tf.keras.layers.Masking() self.dropout_layer = tf.keras.layers.Dropout(dropout) if not self.encoder_trainable: self.rnn_layers = [] for _ in range(n_layers): rnn = tf.keras.layers.Bidirectional( tf.keras.layers.LSTM(hidden_size, return_sequences=False)) self.rnn_layers.append(rnn) self.norm_layer = tf.keras.layers.LayerNormalization(epsilon=1e-9) self.project_layer = tf.keras.layers.Dense(len(word_index)) self._set_inputs( tf.keras.backend.placeholder((None, None), dtype='string'))
def __init__(self, encoder_path, word_index, index_word, encoder_trainable, hidden_size, dropout, n_layers, rnn, n_additional_features, bidirection=True, use_crf=True, **kwargs): super(TaggerModel, self).__init__(**kwargs) self.to_token = ToTokens(word_index) self.to_tags = ToTags(index_word) self.encoder_layer = get_encoder(encoder_path, encoder_trainable) self.masking = tf.keras.layers.Masking() self.dropout_layer = tf.keras.layers.Dropout(dropout) self.n_additional_features = n_additional_features self.use_crf = use_crf self.rnn_layers = [] for _ in range(n_layers): rnn_layer = rnn(hidden_size, return_sequences=True) if bidirection: rnn_layer = tf.keras.layers.Bidirectional(rnn_layer) self.rnn_layers.append(rnn_layer) self.project_layer = tf.keras.layers.Dense(len(word_index)) if use_crf: self.crf_layer = CRF(len(word_index)) self._set_inputs( tf.keras.backend.placeholder((None, None), dtype='string'))
def __init__(self, encoder_path, word_index, index_word, encoder_trainable=False, hidden_size=768, dropout=.25, n_layers=1, **kwargs): super(TaggerPointerModel, self).__init__(**kwargs) self.to_token = ToPointerTokens(word_index) self.to_tags = ToPointerTags(index_word) self.encoder_layer = get_encoder(encoder_path, encoder_trainable) self.masking = tf.keras.layers.Masking() self.dropout_layer = tf.keras.layers.Dropout(dropout) self.rnn_layers = [] for _ in range(n_layers): rnn = tf.keras.layers.Bidirectional( tf.keras.layers.LSTM(hidden_size, return_sequences=True)) self.rnn_layers.append(rnn) b_keys = sorted([x for x in word_index.keys() if x.startswith('B')]) i_keys = sorted([x for x in word_index.keys() if x.startswith('I')]) assert len(b_keys) == len(i_keys) self.project_hidden_layers = [] self.norm_layers = [] # 2 layers hidden for _ in range(2): layers = [] norm_layers = [] for _ in range(len(b_keys)): layers.append( tf.keras.layers.Dense(hidden_size, activation='tanh')) norm_layers.append( tf.keras.layers.LayerNormalization(epsilon=1e-9)) self.project_hidden_layers.append(layers) self.norm_layers.append(norm_layers) self.project_begin_layers = [] for _ in range(len(b_keys)): self.project_begin_layers.append(tf.keras.layers.Dense(2)) self.project_end_layers = [] for _ in range(len(i_keys)): self.project_end_layers.append(tf.keras.layers.Dense(2)) self._set_inputs( tf.keras.backend.placeholder((None, None), dtype='string'))
def __init__(self, encoder_path, tag0_size, tag1_size, proj0_size, proj1_size, word_index, index_word, pos_word_index, pos_index_word, encoder_trainable=False, rnn=tf.keras.layers.LSTM, bidirection=True, dropout=0.33, n_layers=3, hidden_size=400, **kwargs): super(ParserModel, self).__init__(**kwargs) self.encoder_layer = get_encoder(encoder_path, encoder_trainable=encoder_trainable) self.masking = tf.keras.layers.Masking() self.dropout_layer = tf.keras.layers.Dropout(dropout) self.rnn_layers0 = [] for _ in range(n_layers): rnn_layer = rnn(hidden_size, return_sequences=True) if bidirection: rnn_layer = tf.keras.layers.Bidirectional(rnn_layer) self.rnn_layers0.append(rnn_layer) self.rnn_layers1 = [] for _ in range(n_layers): rnn_layer = rnn(hidden_size, return_sequences=True) if bidirection: rnn_layer = tf.keras.layers.Bidirectional(rnn_layer) self.rnn_layers1.append(rnn_layer) self.dep_mlp = tf.keras.layers.Dense(hidden_size) self.dep_concat = tf.keras.layers.Concatenate(axis=-1) self.dep_proj = tf.keras.layers.Dense(hidden_size) self.ph0 = tf.keras.layers.Dense(proj0_size) self.pd0 = tf.keras.layers.Dense(proj0_size) self.ph1 = tf.keras.layers.Dense(proj1_size) self.pd1 = tf.keras.layers.Dense(proj1_size) self.b0 = Biaffine(proj0_size, proj0_size, tag0_size) self.b1 = Biaffine(proj1_size, proj1_size, tag1_size) self.to_tokens = ToTokens(word_index) self.pos_to_tokens = PosToTokens(pos_word_index) self.to_tags = ToTags(index_word) self.pos_to_tags = PosToTags(pos_index_word) self.pos_mlp = tf.keras.layers.Dense(hidden_size) self.pos_proj = tf.keras.layers.Dense(len(pos_word_index)) self.leaky_relu = tf.keras.layers.LeakyReLU(alpha=0.3) self._set_inputs( tf.keras.backend.placeholder((None, None), dtype='string'))