Source code for finetune.comparison

importnumpyasnpimporttensorflowastffromfinetune.baseimportBaseModelfromfinetune.classifierimportClassifier,ClassificationPipelinefromfinetune.encodingimportArrayEncodedOutputclassComparisonPipeline(ClassificationPipeline):def_format_for_encoding(self,X):return[X]def_text_to_ids(self,pair,Y=None,pad_token=None):""" Format comparison examples as a list of IDs pairs: Array of text, shape [batch, 2] """assertself.config.chunk_long_sequencesisFalse,"Chunk Long Sequences is not compatible with comparison"arr_forward=next(super()._text_to_ids(pair,Y=None))reversed_pair=pair[::-1]arr_backward=next(super()._text_to_ids(reversed_pair,Y=None))kwargs=arr_forward._asdict()kwargs['tokens']=[arr_forward.tokens,arr_backward.tokens]kwargs['token_ids']=np.stack([arr_forward.token_ids,arr_backward.token_ids],0)kwargs['mask']=np.stack([arr_forward.mask,arr_backward.mask],0)yieldArrayEncodedOutput(**kwargs)deffeed_shape_type_def(self):TS=tf.TensorShapereturn({"tokens":tf.int32,"mask":tf.int32},tf.int32),({"tokens":TS([2,self.config.max_length,2]),"mask":TS([2,self.config.max_length])},TS([self.target_dim]))

[docs]defpredict_proba(self,pairs):""" Produces a probability distribution over classes for each example in X. :param pairs: Array of text, shape [batch, 2] :returns: list of dictionaries. Each dictionary maps from a class label to its assigned class probability. """returnBaseModel.predict_proba(self,pairs)

[docs]deffeaturize(self,pairs):""" Embeds inputs in learned feature space. Can be called before or after calling :meth:`finetune`. :param pairs: Array of text, shape [batch, 2] :returns: np.array of features of shape (n_examples, embedding_size). """returnBaseModel.featurize(self,pairs)