├── .gitattributes ├── train.py ├── evaluate.py ├── README.md ├── config.py ├── general_utils.py ├── base_model.py ├── data_utils.py └── ner_model.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | from model.data_utils import CoNLLDataset 2 | from model.ner_model import NERModel 3 | from model.config import Config 4 | 5 | 6 | def main(): 7 | # create instance of config 8 | config = Config() 9 | 10 | # build model 11 | model = NERModel(config) 12 | model.build() 13 | # model.restore_session("results/crf/model.weights/") # optional, restore weights 14 | # model.reinitialize_weights("proj") 15 | 16 | # create datasets 17 | dev = CoNLLDataset(config.filename_dev, config.processing_word, 18 | config.processing_tag, config.max_iter) 19 | train = CoNLLDataset(config.filename_train, config.processing_word, 20 | config.processing_tag, config.max_iter) 21 | 22 | # train model 23 | model.train(train, dev) 24 | 25 | if __name__ == "__main__": 26 | main() 27 | -------------------------------------------------------------------------------- /evaluate.py: -------------------------------------------------------------------------------- 1 | from model.data_utils import CoNLLDataset 2 | from model.ner_model import NERModel 3 | from model.config import Config 4 | 5 | 6 | def align_data(data): 7 | """Given dict with lists, creates aligned strings 8 | 9 | Adapted from Assignment 3 of CS224N 10 | 11 | Args: 12 | data: (dict) data["x"] = ["I", "love", "you"] 13 | (dict) data["y"] = ["O", "O", "O"] 14 | 15 | Returns: 16 | data_aligned: (dict) data_align["x"] = "I love you" 17 | data_align["y"] = "O O O " 18 | 19 | """ 20 | spacings = [max([len(seq[i]) for seq in data.values()]) 21 | for i in range(len(data[list(data.keys())[0]]))] 22 | data_aligned = dict() 23 | 24 | # for each entry, create aligned string 25 | for key, seq in data.items(): 26 | str_aligned = "" 27 | for token, spacing in zip(seq, spacings): 28 | str_aligned += token + " " * (spacing - len(token) + 1) 29 | 30 | data_aligned[key] = str_aligned 31 | 32 | return data_aligned 33 | 34 | 35 | 36 | def interactive_shell(model): 37 | """Creates interactive shell to play with model 38 | 39 | Args: 40 | model: instance of NERModel 41 | 42 | """ 43 | model.logger.info(""" 44 | This is an interactive mode. 45 | To exit, enter 'exit'. 46 | You can enter a sentence like 47 | input> I love Paris""") 48 | 49 | while True: 50 | try: 51 | # for python 2 52 | sentence = raw_input("input> ") 53 | except NameError: 54 | # for python 3 55 | sentence = input("input> ") 56 | 57 | words_raw = sentence.strip().split(" ") 58 | 59 | if words_raw == ["exit"]: 60 | break 61 | 62 | preds = model.predict(words_raw) 63 | to_print = align_data({"input": words_raw, "output": preds}) 64 | 65 | for key, seq in to_print.items(): 66 | model.logger.info(seq) 67 | 68 | 69 | def main(): 70 | # create instance of config 71 | config = Config() 72 | 73 | # build model 74 | model = NERModel(config) 75 | model.build() 76 | model.restore_session(config.dir_model) 77 | 78 | # create dataset 79 | test = CoNLLDataset(config.filename_test, config.processing_word, 80 | config.processing_tag, config.max_iter) 81 | 82 | # evaluate and interact 83 | model.evaluate(test) 84 | interactive_shell(model) 85 | 86 | 87 | if __name__ == "__main__": 88 | main() 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NLP Attribute Extraction 2 | 3 | This repo implements a NER model using Tensorflow ( Satcked Bi-directional LSTM + CRF + chars embeddings). 4 | 5 | 6 | ## Task 7 | 8 | Given a sentence, give a tag to each word. 9 | 10 | ``` 11 | Komnil Men's cerulean blue graphical scooter half sleeve round neck tshirt. 12 | B_BRAND O O B_COLOR O O B_SLEEVE I_SLEEVE B_TYPE I_TYPE O 13 | ``` 14 | 15 | 16 | ## Model 17 | 18 | Similar to [Lample et al.](https://arxiv.org/abs/1603.01360) and [Ma and Hovy](https://arxiv.org/pdf/1603.01354.pdf). 19 | 20 | - concatenate final states of a bi-lstm on character embeddings to get a character-based representation of each word 21 | - concatenate this representation to a standard word vector representation (GloVe here) 22 | - run a stacked bi-lstm on each sentence to extract contextual representation of each word 23 | - decode with a linear chain CRF 24 | 25 | 26 | 27 | ## Getting started 28 | 29 | 30 | 1. Download the GloVe vectors with 31 | 32 | ``` 33 | make glove 34 | ``` 35 | 36 | Alternatively, you can download them manually [here](https://nlp.stanford.edu/projects/glove/) and update the `glove_filename` entry in `config.py`. You can also choose not to load pretrained word vectors by changing the entry `use_pretrained` to `False` in `model/config.py`. 37 | 38 | 2. Build the training data, train and evaluate the model with 39 | ``` 40 | make run 41 | ``` 42 | 43 | 44 | ## Details 45 | 46 | 47 | Here is the breakdown of the commands executed in `make run`: 48 | 49 | 1. [DO NOT MISS THIS STEP] Build vocab from the data and extract trimmed glove vectors according to the config in `model/config.py`. 50 | 51 | ``` 52 | python build_data.py 53 | ``` 54 | 55 | 2. Train the model with 56 | 57 | ``` 58 | python train.py 59 | ``` 60 | 61 | 62 | 3. Evaluate and interact with the model with 63 | ``` 64 | python evaluate.py 65 | ``` 66 | 67 | 68 | Data iterators and utils are in `model/data_utils.py` and the model with training/test procedures is in `model/ner_model.py` 69 | 70 | Training time on NVidia Tesla K80 is 110 seconds per epoch on CoNLL train set using characters embeddings and CRF. 71 | 72 | 73 | 74 | ## Training Data 75 | 76 | 77 | The training data must be in the following format. 78 | 79 | A default test file is provided to help you getting started. 80 | 81 | 82 | ``` 83 | Komnil B_BRAND 84 | Men'S O 85 | Cerulean O 86 | Blue B_COLOR 87 | Graphical O 88 | Scooter O 89 | Half B_SLEEVE 90 | Sleeve I_SLEEVE 91 | Round B_TYPE 92 | Neck I_TYPE 93 | T-shirt O 94 | . O 95 | ``` 96 | 97 | 98 | Once you have produced your data files, change the parameters in `config.py` like 99 | 100 | ``` 101 | # dataset 102 | dev_filename = "data/coNLL/eng/eng.testa.iob" 103 | test_filename = "data/coNLL/eng/eng.testb.iob" 104 | train_filename = "data/coNLL/eng/eng.train.iob" 105 | ``` 106 | 107 | # Make the following folders: 108 | Sequence tagging [build_data.py, evaluate.py , train.py, model,data] 109 | 110 | model [base_model.py, config.py, data_utils.py, general_utils.py,ner_model.py] 111 | 112 | data [xyz_test.txt , xyz_train.txt] // Put Glove pretrained vector [300d] in this folder. 113 | 114 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | from .general_utils import get_logger 5 | from .data_utils import get_trimmed_glove_vectors, load_vocab, \ 6 | get_processing_word 7 | 8 | 9 | class Config(): 10 | def __init__(self, load=True): 11 | """Initialize hyperparameters and load vocabs 12 | 13 | Args: 14 | load_embeddings: (bool) if True, load embeddings into 15 | np array, else None 16 | 17 | """ 18 | # directory for training outputs 19 | if not os.path.exists(self.dir_output): 20 | os.makedirs(self.dir_output) 21 | 22 | # create instance of logger 23 | self.logger = get_logger(self.path_log) 24 | 25 | # load if requested (default) 26 | if load: 27 | self.load() 28 | 29 | 30 | def load(self): 31 | """Loads vocabulary, processing functions and embeddings 32 | 33 | Supposes that build_data.py has been run successfully and that 34 | the corresponding files have been created (vocab and trimmed GloVe 35 | vectors) 36 | 37 | """ 38 | # 1. vocabulary 39 | self.vocab_words = load_vocab(self.filename_words) 40 | self.vocab_tags = load_vocab(self.filename_tags) 41 | self.vocab_chars = load_vocab(self.filename_chars) 42 | 43 | self.nwords = len(self.vocab_words) 44 | self.nchars = len(self.vocab_chars) 45 | self.ntags = len(self.vocab_tags) 46 | 47 | # 2. get processing functions that map str -> id 48 | self.processing_word = get_processing_word(self.vocab_words, 49 | self.vocab_chars, lowercase=True, chars=self.use_chars) 50 | self.processing_tag = get_processing_word(self.vocab_tags, 51 | lowercase=False, allow_unk=False) 52 | 53 | # 3. get pre-trained embeddings 54 | self.embeddings = (get_trimmed_glove_vectors(self.filename_trimmed) 55 | if self.use_pretrained else None) 56 | 57 | 58 | # general config 59 | dir_output = "results/test/" 60 | dir_model = dir_output + "model.weights/" 61 | path_log = dir_output + "log.txt" 62 | 63 | # embeddings 64 | dim_word = 300 65 | dim_char = 100 66 | 67 | # glove files 68 | filename_glove = "data/glove.6B/glove.6B.{}d.txt".format(dim_word) 69 | # trimmed embeddings (created from glove_filename with build_data.py) 70 | filename_trimmed = "data/glove.6B.{}d.trimmed.npz".format(dim_word) 71 | use_pretrained = True 72 | 73 | # dataset 74 | # filename_dev = "data/coNLL/eng/eng.testa.iob" 75 | # filename_test = "data/coNLL/eng/eng.testb.iob" 76 | # filename_train = "data/coNLL/eng/eng.train.iob" 77 | 78 | filename_dev = filename_test = "data/dress_3_45_test.txt" 79 | filename_train = "data/dress_3_45_train.txt" # test 80 | 81 | max_iter = None # if not None, max number of examples in Dataset 82 | 83 | # vocab (created from dataset with build_data.py) 84 | filename_words = "data/words.txt" 85 | filename_tags = "data/tags.txt" 86 | filename_chars = "data/chars.txt" 87 | 88 | # training 89 | train_embeddings = False 90 | nepochs = 30 91 | dropout = 0.5 92 | batch_size = 20 93 | lr_method = "adam" 94 | lr = 0.001 95 | lr_decay = 0.9 96 | clip = -1 # if negative, no clipping 97 | nepoch_no_imprv = 3 98 | 99 | # model hyperparameters 100 | hidden_size_char = 100 # lstm on chars 101 | hidden_size_lstm = 300 # lstm on word embeddings 102 | 103 | # NOTE: if both chars and crf, only 1.6x slower on GPU 104 | use_crf = True # if crf, training is 1.7x slower on CPU 105 | use_chars = True # if char embedding, training is 3.5x slower on CPU 106 | -------------------------------------------------------------------------------- /general_utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | import sys 3 | import logging 4 | import numpy as np 5 | 6 | 7 | def get_logger(filename): 8 | """Return a logger instance that writes in filename 9 | 10 | Args: 11 | filename: (string) path to log.txt 12 | 13 | Returns: 14 | logger: (instance of logger) 15 | 16 | """ 17 | logger = logging.getLogger('logger') 18 | logger.setLevel(logging.DEBUG) 19 | logging.basicConfig(format='%(message)s', level=logging.DEBUG) 20 | handler = logging.FileHandler(filename) 21 | handler.setLevel(logging.DEBUG) 22 | handler.setFormatter(logging.Formatter( 23 | '%(asctime)s:%(levelname)s: %(message)s')) 24 | logging.getLogger().addHandler(handler) 25 | 26 | return logger 27 | 28 | 29 | class Progbar(object): 30 | """Progbar class copied from keras (https://github.com/fchollet/keras/) 31 | 32 | Displays a progress bar. 33 | Small edit : added strict arg to update 34 | # Arguments 35 | target: Total number of steps expected. 36 | interval: Minimum visual progress update interval (in seconds). 37 | """ 38 | 39 | def __init__(self, target, width=30, verbose=1): 40 | self.width = width 41 | self.target = target 42 | self.sum_values = {} 43 | self.unique_values = [] 44 | self.start = time.time() 45 | self.total_width = 0 46 | self.seen_so_far = 0 47 | self.verbose = verbose 48 | 49 | def update(self, current, values=[], exact=[], strict=[]): 50 | """ 51 | Updates the progress bar. 52 | # Arguments 53 | current: Index of current step. 54 | values: List of tuples (name, value_for_last_step). 55 | The progress bar will display averages for these values. 56 | exact: List of tuples (name, value_for_last_step). 57 | The progress bar will display these values directly. 58 | """ 59 | 60 | for k, v in values: 61 | if k not in self.sum_values: 62 | self.sum_values[k] = [v * (current - self.seen_so_far), 63 | current - self.seen_so_far] 64 | self.unique_values.append(k) 65 | else: 66 | self.sum_values[k][0] += v * (current - self.seen_so_far) 67 | self.sum_values[k][1] += (current - self.seen_so_far) 68 | for k, v in exact: 69 | if k not in self.sum_values: 70 | self.unique_values.append(k) 71 | self.sum_values[k] = [v, 1] 72 | 73 | for k, v in strict: 74 | if k not in self.sum_values: 75 | self.unique_values.append(k) 76 | self.sum_values[k] = v 77 | 78 | self.seen_so_far = current 79 | 80 | now = time.time() 81 | if self.verbose == 1: 82 | prev_total_width = self.total_width 83 | sys.stdout.write("\b" * prev_total_width) 84 | sys.stdout.write("\r") 85 | 86 | numdigits = int(np.floor(np.log10(self.target))) + 1 87 | barstr = '%%%dd/%%%dd [' % (numdigits, numdigits) 88 | bar = barstr % (current, self.target) 89 | prog = float(current)/self.target 90 | prog_width = int(self.width*prog) 91 | if prog_width > 0: 92 | bar += ('='*(prog_width-1)) 93 | if current < self.target: 94 | bar += '>' 95 | else: 96 | bar += '=' 97 | bar += ('.'*(self.width-prog_width)) 98 | bar += ']' 99 | sys.stdout.write(bar) 100 | self.total_width = len(bar) 101 | 102 | if current: 103 | time_per_unit = (now - self.start) / current 104 | else: 105 | time_per_unit = 0 106 | eta = time_per_unit*(self.target - current) 107 | info = '' 108 | if current < self.target: 109 | info += ' - ETA: %ds' % eta 110 | else: 111 | info += ' - %ds' % (now - self.start) 112 | for k in self.unique_values: 113 | if type(self.sum_values[k]) is list: 114 | info += ' - %s: %.4f' % (k, 115 | self.sum_values[k][0] / max(1, self.sum_values[k][1])) 116 | else: 117 | info += ' - %s: %s' % (k, self.sum_values[k]) 118 | 119 | self.total_width += len(info) 120 | if prev_total_width > self.total_width: 121 | info += ((prev_total_width-self.total_width) * " ") 122 | 123 | sys.stdout.write(info) 124 | sys.stdout.flush() 125 | 126 | if current >= self.target: 127 | sys.stdout.write("\n") 128 | 129 | if self.verbose == 2: 130 | if current >= self.target: 131 | info = '%ds' % (now - self.start) 132 | for k in self.unique_values: 133 | info += ' - %s: %.4f' % (k, 134 | self.sum_values[k][0] / max(1, self.sum_values[k][1])) 135 | sys.stdout.write(info + "\n") 136 | 137 | def add(self, n, values=[]): 138 | self.update(self.seen_so_far+n, values) 139 | 140 | 141 | -------------------------------------------------------------------------------- /base_model.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | 4 | 5 | class BaseModel(object): 6 | """Generic class for general methods that are not specific to NER""" 7 | 8 | def __init__(self, config): 9 | """Defines self.config and self.logger 10 | 11 | Args: 12 | config: (Config instance) class with hyper parameters, 13 | vocab and embeddings 14 | 15 | """ 16 | self.config = config 17 | self.logger = config.logger 18 | self.sess = None 19 | self.saver = None 20 | 21 | 22 | def reinitialize_weights(self, scope_name): 23 | """Reinitializes the weights of a given layer""" 24 | variables = tf.contrib.framework.get_variables(scope_name) 25 | init = tf.variables_initializer(variables) 26 | self.sess.run(init) 27 | 28 | 29 | def add_train_op(self, lr_method, lr, loss, clip=-1): 30 | """Defines self.train_op that performs an update on a batch 31 | 32 | Args: 33 | lr_method: (string) sgd method, for example "adam" 34 | lr: (tf.placeholder) tf.float32, learning rate 35 | loss: (tensor) tf.float32 loss to minimize 36 | clip: (python float) clipping of gradient. If < 0, no clipping 37 | 38 | """ 39 | _lr_m = lr_method.lower() # lower to make sure 40 | 41 | with tf.variable_scope("train_step"): 42 | if _lr_m == 'adam': # sgd method 43 | optimizer = tf.train.AdamOptimizer(lr) 44 | elif _lr_m == 'adagrad': 45 | optimizer = tf.train.AdagradOptimizer(lr) 46 | elif _lr_m == 'sgd': 47 | optimizer = tf.train.GradientDescentOptimizer(lr) 48 | elif _lr_m == 'rmsprop': 49 | optimizer = tf.train.RMSPropOptimizer(lr) 50 | else: 51 | raise NotImplementedError("Unknown method {}".format(_lr_m)) 52 | 53 | if clip > 0: # gradient clipping if clip is positive 54 | grads, vs = zip(*optimizer.compute_gradients(loss)) 55 | grads, gnorm = tf.clip_by_global_norm(grads, clip) 56 | self.train_op = optimizer.apply_gradients(zip(grads, vs)) 57 | else: 58 | self.train_op = optimizer.minimize(loss) 59 | 60 | 61 | def initialize_session(self): 62 | """Defines self.sess and initialize the variables""" 63 | self.logger.info("Initializing tf session") 64 | self.sess = tf.Session() 65 | self.sess.run(tf.global_variables_initializer()) 66 | self.saver = tf.train.Saver() 67 | 68 | 69 | def restore_session(self, dir_model): 70 | """Reload weights into session 71 | 72 | Args: 73 | sess: tf.Session() 74 | dir_model: dir with weights 75 | 76 | """ 77 | self.logger.info("Reloading the latest trained model...") 78 | self.saver.restore(self.sess, dir_model) 79 | 80 | 81 | def save_session(self): 82 | """Saves session = weights""" 83 | if not os.path.exists(self.config.dir_model): 84 | os.makedirs(self.config.dir_model) 85 | self.saver.save(self.sess, self.config.dir_model) 86 | 87 | 88 | def close_session(self): 89 | """Closes the session""" 90 | self.sess.close() 91 | 92 | 93 | def add_summary(self): 94 | """Defines variables for Tensorboard 95 | 96 | Args: 97 | dir_output: (string) where the results are written 98 | 99 | """ 100 | self.merged = tf.summary.merge_all() 101 | self.file_writer = tf.summary.FileWriter(self.config.dir_output, 102 | self.sess.graph) 103 | 104 | 105 | def train(self, train, dev): 106 | """Performs training with early stopping and lr exponential decay 107 | 108 | Args: 109 | train: dataset that yields tuple of (sentences, tags) 110 | dev: dataset 111 | 112 | """ 113 | best_score = 0 114 | nepoch_no_imprv = 0 # for early stopping 115 | self.add_summary() # tensorboard 116 | 117 | for epoch in range(self.config.nepochs): 118 | self.logger.info("Epoch {:} out of {:}".format(epoch + 1, 119 | self.config.nepochs)) 120 | 121 | score = self.run_epoch(train, dev, epoch) 122 | self.config.lr *= self.config.lr_decay # decay learning rate 123 | 124 | # early stopping and saving best parameters 125 | if score >= best_score: 126 | nepoch_no_imprv = 0 127 | self.save_session() 128 | best_score = score 129 | self.logger.info("- new best score!") 130 | else: 131 | nepoch_no_imprv += 1 132 | if nepoch_no_imprv >= self.config.nepoch_no_imprv: 133 | self.logger.info("- early stopping {} epochs without "\ 134 | "improvement".format(nepoch_no_imprv)) 135 | break 136 | 137 | 138 | def evaluate(self, test): 139 | """Evaluate model on test set 140 | 141 | Args: 142 | test: instance of class Dataset 143 | 144 | """ 145 | self.logger.info("Testing model over test set") 146 | metrics = self.run_evaluate(test) 147 | msg = " - ".join(["{} {:04.2f}".format(k, v) 148 | for k, v in metrics.items()]) 149 | self.logger.info(msg) 150 | -------------------------------------------------------------------------------- /data_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | 4 | 5 | # shared global variables to be imported from model also 6 | UNK = "$UNK$" 7 | NUM = "$NUM$" 8 | NONE = "O" 9 | 10 | 11 | # special error message 12 | class MyIOError(Exception): 13 | def __init__(self, filename): 14 | # custom error message 15 | message = """ 16 | ERROR: Unable to locate file {}. 17 | 18 | FIX: Have you tried running python build_data.py first? 19 | This will build vocab file from your train, test and dev sets and 20 | trimm your word vectors. 21 | """.format(filename) 22 | super(MyIOError, self).__init__(message) 23 | 24 | 25 | class CoNLLDataset(object): 26 | """Class that iterates over CoNLL Dataset 27 | 28 | __iter__ method yields a tuple (words, tags) 29 | words: list of raw words 30 | tags: list of raw tags 31 | 32 | If processing_word and processing_tag are not None, 33 | optional preprocessing is appplied 34 | 35 | Example: 36 | ```python 37 | data = CoNLLDataset(filename) 38 | for sentence, tags in data: 39 | pass 40 | ``` 41 | 42 | """ 43 | def __init__(self, filename, processing_word=None, processing_tag=None, 44 | max_iter=None): 45 | """ 46 | Args: 47 | filename: path to the file 48 | processing_words: (optional) function that takes a word as input 49 | processing_tags: (optional) function that takes a tag as input 50 | max_iter: (optional) max number of sentences to yield 51 | 52 | """ 53 | self.filename = filename 54 | self.processing_word = processing_word 55 | self.processing_tag = processing_tag 56 | self.max_iter = max_iter 57 | self.length = None 58 | 59 | 60 | def __iter__(self): 61 | niter = 0 62 | with open(self.filename) as f: 63 | words, tags = [], [] 64 | for line in f: 65 | line = line.strip() 66 | if (len(line) == 0 or line.startswith("-DOCSTART-")): 67 | if len(words) != 0: 68 | niter += 1 69 | if self.max_iter is not None and niter > self.max_iter: 70 | break 71 | yield words, tags 72 | words, tags = [], [] 73 | else: 74 | ls = line.split(' ') 75 | word, tag = ls[0],ls[-1] 76 | if self.processing_word is not None: 77 | word = self.processing_word(word) 78 | if self.processing_tag is not None: 79 | tag = self.processing_tag(tag) 80 | words += [word] 81 | tags += [tag] 82 | 83 | 84 | def __len__(self): 85 | """Iterates once over the corpus to set and store length""" 86 | if self.length is None: 87 | self.length = 0 88 | for _ in self: 89 | self.length += 1 90 | 91 | return self.length 92 | 93 | 94 | def get_vocabs(datasets): 95 | """Build vocabulary from an iterable of datasets objects 96 | 97 | Args: 98 | datasets: a list of dataset objects 99 | 100 | Returns: 101 | a set of all the words in the dataset 102 | 103 | """ 104 | print("Building vocab...") 105 | vocab_words = set() 106 | vocab_tags = set() 107 | for dataset in datasets: 108 | for words, tags in dataset: 109 | vocab_words.update(words) 110 | vocab_tags.update(tags) 111 | print("- done. {} tokens".format(len(vocab_words))) 112 | return vocab_words, vocab_tags 113 | 114 | 115 | def get_char_vocab(dataset): 116 | """Build char vocabulary from an iterable of datasets objects 117 | 118 | Args: 119 | dataset: a iterator yielding tuples (sentence, tags) 120 | 121 | Returns: 122 | a set of all the characters in the dataset 123 | 124 | """ 125 | vocab_char = set() 126 | for words, _ in dataset: 127 | for word in words: 128 | vocab_char.update(word) 129 | 130 | return vocab_char 131 | 132 | 133 | def get_glove_vocab(filename): 134 | """Load vocab from file 135 | 136 | Args: 137 | filename: path to the glove vectors 138 | 139 | Returns: 140 | vocab: set() of strings 141 | """ 142 | print("Building vocab...") 143 | vocab = set() 144 | with open(filename) as f: 145 | for line in f: 146 | word = line.strip().split(' ')[0] 147 | vocab.add(word) 148 | print("- done. {} tokens".format(len(vocab))) 149 | return vocab 150 | 151 | 152 | def write_vocab(vocab, filename): 153 | """Writes a vocab to a file 154 | 155 | Writes one word per line. 156 | 157 | Args: 158 | vocab: iterable that yields word 159 | filename: path to vocab file 160 | 161 | Returns: 162 | write a word per line 163 | 164 | """ 165 | print("Writing vocab...") 166 | with open(filename, "w") as f: 167 | for i, word in enumerate(vocab): 168 | if i != len(vocab) - 1: 169 | f.write("{}\n".format(word)) 170 | else: 171 | f.write(word) 172 | print("- done. {} tokens".format(len(vocab))) 173 | 174 | 175 | def load_vocab(filename): 176 | """Loads vocab from a file 177 | 178 | Args: 179 | filename: (string) the format of the file must be one word per line. 180 | 181 | Returns: 182 | d: dict[word] = index 183 | 184 | """ 185 | try: 186 | d = dict() 187 | with open(filename) as f: 188 | for idx, word in enumerate(f): 189 | word = word.strip() 190 | d[word] = idx 191 | 192 | except IOError: 193 | raise MyIOError(filename) 194 | return d 195 | 196 | 197 | def export_trimmed_glove_vectors(vocab, glove_filename, trimmed_filename, dim): 198 | """Saves glove vectors in numpy array 199 | 200 | Args: 201 | vocab: dictionary vocab[word] = index 202 | glove_filename: a path to a glove file 203 | trimmed_filename: a path where to store a matrix in npy 204 | dim: (int) dimension of embeddings 205 | 206 | """ 207 | embeddings = np.zeros([len(vocab), dim]) 208 | with open(glove_filename) as f: 209 | for line in f: 210 | line = line.strip().split(' ') 211 | word = line[0] 212 | embedding = [float(x) for x in line[1:]] 213 | if word in vocab: 214 | word_idx = vocab[word] 215 | embeddings[word_idx] = np.asarray(embedding) 216 | 217 | np.savez_compressed(trimmed_filename, embeddings=embeddings) 218 | 219 | 220 | def get_trimmed_glove_vectors(filename): 221 | """ 222 | Args: 223 | filename: path to the npz file 224 | 225 | Returns: 226 | matrix of embeddings (np array) 227 | 228 | """ 229 | try: 230 | with np.load(filename) as data: 231 | return data["embeddings"] 232 | 233 | except IOError: 234 | raise MyIOError(filename) 235 | 236 | 237 | def get_processing_word(vocab_words=None, vocab_chars=None, 238 | lowercase=False, chars=False, allow_unk=True): 239 | """Return lambda function that transform a word (string) into list, 240 | or tuple of (list, id) of int corresponding to the ids of the word and 241 | its corresponding characters. 242 | 243 | Args: 244 | vocab: dict[word] = idx 245 | 246 | Returns: 247 | f("cat") = ([12, 4, 32], 12345) 248 | = (list of char ids, word id) 249 | 250 | """ 251 | def f(word): 252 | # 0. get chars of words 253 | if vocab_chars is not None and chars == True: 254 | char_ids = [] 255 | for char in word: 256 | # ignore chars out of vocabulary 257 | if char in vocab_chars: 258 | char_ids += [vocab_chars[char]] 259 | 260 | # 1. preprocess word 261 | if lowercase: 262 | word = word.lower() 263 | if word.isdigit(): 264 | word = NUM 265 | 266 | # 2. get id of word 267 | if vocab_words is not None: 268 | if word in vocab_words: 269 | word = vocab_words[word] 270 | else: 271 | if allow_unk: 272 | word = vocab_words[UNK] 273 | else: 274 | raise Exception("Unknow key is not allowed. Check that "\ 275 | "your vocab (tags?) is correct") 276 | 277 | # 3. return tuple char ids, word id 278 | if vocab_chars is not None and chars == True: 279 | return char_ids, word 280 | else: 281 | return word 282 | 283 | return f 284 | 285 | 286 | def _pad_sequences(sequences, pad_tok, max_length): 287 | """ 288 | Args: 289 | sequences: a generator of list or tuple 290 | pad_tok: the char to pad with 291 | 292 | Returns: 293 | a list of list where each sublist has same length 294 | """ 295 | sequence_padded, sequence_length = [], [] 296 | 297 | for seq in sequences: 298 | seq = list(seq) 299 | seq_ = seq[:max_length] + [pad_tok]*max(max_length - len(seq), 0) 300 | sequence_padded += [seq_] 301 | sequence_length += [min(len(seq), max_length)] 302 | 303 | return sequence_padded, sequence_length 304 | 305 | 306 | def pad_sequences(sequences, pad_tok, nlevels=1): 307 | """ 308 | Args: 309 | sequences: a generator of list or tuple 310 | pad_tok: the char to pad with 311 | nlevels: "depth" of padding, for the case where we have characters ids 312 | 313 | Returns: 314 | a list of list where each sublist has same length 315 | 316 | """ 317 | if nlevels == 1: 318 | max_length = max(map(lambda x : len(x), sequences)) 319 | sequence_padded, sequence_length = _pad_sequences(sequences, 320 | pad_tok, max_length) 321 | 322 | elif nlevels == 2: 323 | max_length_word = max([max(map(lambda x: len(x), seq)) 324 | for seq in sequences]) 325 | sequence_padded, sequence_length = [], [] 326 | for seq in sequences: 327 | # all words are same length now 328 | sp, sl = _pad_sequences(seq, pad_tok, max_length_word) 329 | sequence_padded += [sp] 330 | sequence_length += [sl] 331 | 332 | max_length_sentence = max(map(lambda x : len(x), sequences)) 333 | sequence_padded, _ = _pad_sequences(sequence_padded, 334 | [pad_tok]*max_length_word, max_length_sentence) 335 | sequence_length, _ = _pad_sequences(sequence_length, 0, 336 | max_length_sentence) 337 | 338 | return sequence_padded, sequence_length 339 | 340 | 341 | def minibatches(data, minibatch_size): 342 | """ 343 | Args: 344 | data: generator of (sentence, tags) tuples 345 | minibatch_size: (int) 346 | 347 | Yields: 348 | list of tuples 349 | 350 | """ 351 | x_batch, y_batch = [], [] 352 | for (x, y) in data: 353 | if len(x_batch) == minibatch_size: 354 | yield x_batch, y_batch 355 | x_batch, y_batch = [], [] 356 | 357 | if type(x[0]) == tuple: 358 | x = zip(*x) 359 | x_batch += [x] 360 | y_batch += [y] 361 | 362 | if len(x_batch) != 0: 363 | yield x_batch, y_batch 364 | 365 | 366 | def get_chunk_type(tok, idx_to_tag): 367 | """ 368 | Args: 369 | tok: id of token, ex 4 370 | idx_to_tag: dictionary {4: "B-PER", ...} 371 | 372 | Returns: 373 | tuple: "B", "PER" 374 | 375 | """ 376 | tag_name = idx_to_tag[tok] 377 | tag_class = tag_name.split('-')[0] 378 | tag_type = tag_name.split('-')[-1] 379 | return tag_class, tag_type 380 | 381 | 382 | def get_chunks(seq, tags): 383 | """Given a sequence of tags, group entities and their position 384 | 385 | Args: 386 | seq: [4, 4, 0, 0, ...] sequence of labels 387 | tags: dict["O"] = 4 388 | 389 | Returns: 390 | list of (chunk_type, chunk_start, chunk_end) 391 | 392 | Example: 393 | seq = [4, 5, 0, 3] 394 | tags = {"B-PER": 4, "I-PER": 5, "B-LOC": 3} 395 | result = [("PER", 0, 2), ("LOC", 3, 4)] 396 | 397 | """ 398 | default = tags[NONE] 399 | idx_to_tag = {idx: tag for tag, idx in tags.items()} 400 | chunks = [] 401 | chunk_type, chunk_start = None, None 402 | for i, tok in enumerate(seq): 403 | # End of a chunk 1 404 | if tok == default and chunk_type is not None: 405 | # Add a chunk. 406 | chunk = (chunk_type, chunk_start, i) 407 | chunks.append(chunk) 408 | chunk_type, chunk_start = None, None 409 | 410 | # End of a chunk + start of a chunk! 411 | elif tok != default: 412 | tok_chunk_class, tok_chunk_type = get_chunk_type(tok, idx_to_tag) 413 | if chunk_type is None: 414 | chunk_type, chunk_start = tok_chunk_type, i 415 | elif tok_chunk_type != chunk_type or tok_chunk_class == "B": 416 | chunk = (chunk_type, chunk_start, i) 417 | chunks.append(chunk) 418 | chunk_type, chunk_start = tok_chunk_type, i 419 | else: 420 | pass 421 | 422 | # end condition 423 | if chunk_type is not None: 424 | chunk = (chunk_type, chunk_start, len(seq)) 425 | chunks.append(chunk) 426 | 427 | return chunks 428 | -------------------------------------------------------------------------------- /ner_model.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import tensorflow as tf 4 | 5 | 6 | from .data_utils import minibatches, pad_sequences, get_chunks 7 | from .general_utils import Progbar 8 | from .base_model import BaseModel 9 | 10 | 11 | class NERModel(BaseModel): 12 | """Specialized class of Model for NER""" 13 | 14 | def __init__(self, config): 15 | super(NERModel, self).__init__(config) 16 | self.idx_to_tag = {idx: tag for tag, idx in 17 | self.config.vocab_tags.items()} 18 | 19 | 20 | def add_placeholders(self): 21 | """Define placeholders = entries to computational graph""" 22 | # shape = (batch size, max length of sentence in batch) 23 | self.word_ids = tf.placeholder(tf.int32, shape=[None, None], 24 | name="word_ids") 25 | 26 | # shape = (batch size) 27 | self.sequence_lengths = tf.placeholder(tf.int32, shape=[None], 28 | name="sequence_lengths") 29 | 30 | # shape = (batch size, max length of sentence, max length of word) 31 | self.char_ids = tf.placeholder(tf.int32, shape=[None, None, None], 32 | name="char_ids") 33 | 34 | # shape = (batch_size, max_length of sentence) 35 | self.word_lengths = tf.placeholder(tf.int32, shape=[None, None], 36 | name="word_lengths") 37 | 38 | # shape = (batch size, max length of sentence in batch) 39 | self.labels = tf.placeholder(tf.int32, shape=[None, None], 40 | name="labels") 41 | 42 | # hyper parameters 43 | self.dropout = tf.placeholder(dtype=tf.float32, shape=[], 44 | name="dropout") 45 | self.lr = tf.placeholder(dtype=tf.float32, shape=[], 46 | name="lr") 47 | 48 | 49 | def get_feed_dict(self, words, labels=None, lr=None, dropout=None): 50 | """Given some data, pad it and build a feed dictionary 51 | 52 | Args: 53 | words: list of sentences. A sentence is a list of ids of a list of 54 | words. A word is a list of ids 55 | labels: list of ids 56 | lr: (float) learning rate 57 | dropout: (float) keep prob 58 | 59 | Returns: 60 | dict {placeholder: value} 61 | 62 | """ 63 | # perform padding of the given data 64 | if self.config.use_chars: 65 | char_ids, word_ids = zip(*words) 66 | word_ids, sequence_lengths = pad_sequences(word_ids, 0) 67 | char_ids, word_lengths = pad_sequences(char_ids, pad_tok=0, 68 | nlevels=2) 69 | else: 70 | word_ids, sequence_lengths = pad_sequences(words, 0) 71 | 72 | # build feed dictionary 73 | feed = { 74 | self.word_ids: word_ids, 75 | self.sequence_lengths: sequence_lengths 76 | } 77 | 78 | if self.config.use_chars: 79 | feed[self.char_ids] = char_ids 80 | feed[self.word_lengths] = word_lengths 81 | 82 | if labels is not None: 83 | labels, _ = pad_sequences(labels, 0) 84 | feed[self.labels] = labels 85 | 86 | if lr is not None: 87 | feed[self.lr] = lr 88 | 89 | if dropout is not None: 90 | feed[self.dropout] = dropout 91 | 92 | return feed, sequence_lengths 93 | 94 | 95 | def add_word_embeddings_op(self): 96 | """Defines self.word_embeddings 97 | 98 | If self.config.embeddings is not None and is a np array initialized 99 | with pre-trained word vectors, the word embeddings is just a look-up 100 | and we don't train the vectors. Otherwise, a random matrix with 101 | the correct shape is initialized. 102 | """ 103 | with tf.variable_scope("words"): 104 | if self.config.embeddings is None: 105 | self.logger.info("WARNING: randomly initializing word vectors") 106 | _word_embeddings = tf.get_variable( 107 | name="_word_embeddings", 108 | dtype=tf.float32, 109 | shape=[self.config.nwords, self.config.dim_word]) 110 | else: 111 | _word_embeddings = tf.Variable( 112 | self.config.embeddings, 113 | name="_word_embeddings", 114 | dtype=tf.float32, 115 | trainable=self.config.train_embeddings) 116 | 117 | word_embeddings = tf.nn.embedding_lookup(_word_embeddings, 118 | self.word_ids, name="word_embeddings") 119 | 120 | with tf.variable_scope("chars"): 121 | if self.config.use_chars: 122 | # get char embeddings matrix 123 | _char_embeddings = tf.get_variable( 124 | name="_char_embeddings", 125 | dtype=tf.float32, 126 | shape=[self.config.nchars, self.config.dim_char]) 127 | char_embeddings = tf.nn.embedding_lookup(_char_embeddings, 128 | self.char_ids, name="char_embeddings") 129 | 130 | # put the time dimension on axis=1 131 | s = tf.shape(char_embeddings) 132 | char_embeddings = tf.reshape(char_embeddings, 133 | shape=[s[0]*s[1], s[-2], self.config.dim_char]) 134 | word_lengths = tf.reshape(self.word_lengths, shape=[s[0]*s[1]]) 135 | 136 | # bi lstm on chars 137 | cell_fw = tf.contrib.rnn.LSTMCell(self.config.hidden_size_char, 138 | state_is_tuple=True) 139 | cell_bw = tf.contrib.rnn.LSTMCell(self.config.hidden_size_char, 140 | state_is_tuple=True) 141 | _output = tf.nn.bidirectional_dynamic_rnn( 142 | cell_fw, cell_bw, char_embeddings, 143 | sequence_length=word_lengths, dtype=tf.float32) 144 | 145 | # read and concat output 146 | _, ((_, output_fw), (_, output_bw)) = _output 147 | output = tf.concat([output_fw, output_bw], axis=-1) 148 | 149 | # shape = (batch size, max sentence length, char hidden size) 150 | output = tf.reshape(output, 151 | shape=[s[0], s[1], 2*self.config.hidden_size_char]) 152 | word_embeddings = tf.concat([word_embeddings, output], axis=-1) 153 | 154 | self.word_embeddings = tf.nn.dropout(word_embeddings, self.dropout) 155 | 156 | 157 | def add_logits_op(self): 158 | """Defines self.logits 159 | 160 | For each word in each sentence of the batch, it corresponds to a vector 161 | of scores, of dimension equal to the number of tags. 162 | """ 163 | with tf.variable_scope("bi-lstm"): 164 | cell_fw = tf.contrib.rnn.LSTMCell(self.config.hidden_size_lstm) 165 | cell_bw = tf.contrib.rnn.LSTMCell(self.config.hidden_size_lstm) 166 | (output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn( 167 | cell_fw, cell_bw, self.word_embeddings, 168 | sequence_length=self.sequence_lengths, dtype=tf.float32) 169 | output = tf.concat([output_fw, output_bw], axis=-1) 170 | output = tf.nn.dropout(output, self.dropout) 171 | 172 | with tf.variable_scope("proj"): 173 | W = tf.get_variable("W", dtype=tf.float32, 174 | shape=[2*self.config.hidden_size_lstm, self.config.ntags]) 175 | 176 | b = tf.get_variable("b", shape=[self.config.ntags], 177 | dtype=tf.float32, initializer=tf.zeros_initializer()) 178 | 179 | nsteps = tf.shape(output)[1] 180 | output = tf.reshape(output, [-1, 2*self.config.hidden_size_lstm]) 181 | pred = tf.matmul(output, W) + b 182 | self.logits = tf.reshape(pred, [-1, nsteps, self.config.ntags]) 183 | 184 | 185 | def add_pred_op(self): 186 | """Defines self.labels_pred 187 | 188 | This op is defined only in the case where we don't use a CRF since in 189 | that case we can make the prediction "in the graph" (thanks to tf 190 | functions in other words). With theCRF, as the inference is coded 191 | in python and not in pure tensroflow, we have to make the prediciton 192 | outside the graph. 193 | """ 194 | if not self.config.use_crf: 195 | self.labels_pred = tf.cast(tf.argmax(self.logits, axis=-1), 196 | tf.int32) 197 | 198 | 199 | def add_loss_op(self): 200 | """Defines the loss""" 201 | if self.config.use_crf: 202 | log_likelihood, trans_params = tf.contrib.crf.crf_log_likelihood( 203 | self.logits, self.labels, self.sequence_lengths) 204 | self.trans_params = trans_params # need to evaluate it for decoding 205 | self.loss = tf.reduce_mean(-log_likelihood) 206 | else: 207 | losses = tf.nn.sparse_softmax_cross_entropy_with_logits( 208 | logits=self.logits, labels=self.labels) 209 | mask = tf.sequence_mask(self.sequence_lengths) 210 | losses = tf.boolean_mask(losses, mask) 211 | self.loss = tf.reduce_mean(losses) 212 | 213 | # for tensorboard 214 | tf.summary.scalar("loss", self.loss) 215 | 216 | 217 | def build(self): 218 | # NER specific functions 219 | self.add_placeholders() 220 | self.add_word_embeddings_op() 221 | self.add_logits_op() 222 | self.add_pred_op() 223 | self.add_loss_op() 224 | 225 | # Generic functions that add training op and initialize session 226 | self.add_train_op(self.config.lr_method, self.lr, self.loss, 227 | self.config.clip) 228 | self.initialize_session() # now self.sess is defined and vars are init 229 | 230 | 231 | def predict_batch(self, words): 232 | """ 233 | Args: 234 | words: list of sentences 235 | 236 | Returns: 237 | labels_pred: list of labels for each sentence 238 | sequence_length 239 | 240 | """ 241 | fd, sequence_lengths = self.get_feed_dict(words, dropout=1.0) 242 | 243 | if self.config.use_crf: 244 | # get tag scores and transition params of CRF 245 | viterbi_sequences = [] 246 | logits, trans_params = self.sess.run( 247 | [self.logits, self.trans_params], feed_dict=fd) 248 | 249 | # iterate over the sentences because no batching in vitervi_decode 250 | for logit, sequence_length in zip(logits, sequence_lengths): 251 | logit = logit[:sequence_length] # keep only the valid steps 252 | viterbi_seq, viterbi_score = tf.contrib.crf.viterbi_decode( 253 | logit, trans_params) 254 | viterbi_sequences += [viterbi_seq] 255 | 256 | return viterbi_sequences, sequence_lengths 257 | 258 | else: 259 | labels_pred = self.sess.run(self.labels_pred, feed_dict=fd) 260 | 261 | return labels_pred, sequence_lengths 262 | 263 | 264 | def run_epoch(self, train, dev, epoch): 265 | """Performs one complete pass over the train set and evaluate on dev 266 | 267 | Args: 268 | train: dataset that yields tuple of sentences, tags 269 | dev: dataset 270 | epoch: (int) index of the current epoch 271 | 272 | Returns: 273 | f1: (python float), score to select model on, higher is better 274 | 275 | """ 276 | # progbar stuff for logging 277 | batch_size = self.config.batch_size 278 | nbatches = (len(train) + batch_size - 1) // batch_size 279 | prog = Progbar(target=nbatches) 280 | 281 | # iterate over dataset 282 | for i, (words, labels) in enumerate(minibatches(train, batch_size)): 283 | fd, _ = self.get_feed_dict(words, labels, self.config.lr, 284 | self.config.dropout) 285 | 286 | _, train_loss, summary = self.sess.run( 287 | [self.train_op, self.loss, self.merged], feed_dict=fd) 288 | 289 | prog.update(i + 1, [("train loss", train_loss)]) 290 | 291 | # tensorboard 292 | if i % 10 == 0: 293 | self.file_writer.add_summary(summary, epoch*nbatches + i) 294 | 295 | metrics = self.run_evaluate(dev) 296 | msg = " - ".join(["{} {:04.2f}".format(k, v) 297 | for k, v in metrics.items()]) 298 | self.logger.info(msg) 299 | 300 | return metrics["f1"] 301 | 302 | 303 | def run_evaluate(self, test): 304 | """Evaluates performance on test set 305 | 306 | Args: 307 | test: dataset that yields tuple of (sentences, tags) 308 | 309 | Returns: 310 | metrics: (dict) metrics["acc"] = 98.4, ... 311 | 312 | """ 313 | accs = [] 314 | correct_preds, total_correct, total_preds = 0., 0., 0. 315 | for words, labels in minibatches(test, self.config.batch_size): 316 | labels_pred, sequence_lengths = self.predict_batch(words) 317 | 318 | for lab, lab_pred, length in zip(labels, labels_pred, 319 | sequence_lengths): 320 | lab = lab[:length] 321 | lab_pred = lab_pred[:length] 322 | accs += [a==b for (a, b) in zip(lab, lab_pred)] 323 | 324 | lab_chunks = set(get_chunks(lab, self.config.vocab_tags)) 325 | lab_pred_chunks = set(get_chunks(lab_pred, 326 | self.config.vocab_tags)) 327 | 328 | correct_preds += len(lab_chunks & lab_pred_chunks) 329 | total_preds += len(lab_pred_chunks) 330 | total_correct += len(lab_chunks) 331 | 332 | p = correct_preds / total_preds if correct_preds > 0 else 0 333 | r = correct_preds / total_correct if correct_preds > 0 else 0 334 | f1 = 2 * p * r / (p + r) if correct_preds > 0 else 0 335 | acc = np.mean(accs) 336 | 337 | return {"acc": 100*acc, "f1": 100*f1} 338 | 339 | 340 | def predict(self, words_raw): 341 | """Returns list of tags 342 | 343 | Args: 344 | words_raw: list of words (string), just one sentence (no batch) 345 | 346 | Returns: 347 | preds: list of tags (string), one for each word in the sentence 348 | 349 | """ 350 | words = [self.config.processing_word(w) for w in words_raw] 351 | if type(words[0]) == tuple: 352 | words = zip(*words) 353 | pred_ids, _ = self.predict_batch([words]) 354 | preds = [self.idx_to_tag[idx] for idx in list(pred_ids[0])] 355 | 356 | return preds 357 | --------------------------------------------------------------------------------