# model encoder_types = [ 'InferSent', 'BLSTMprojEncoder', 'BGRUlastEncoder', 'InnerAttentionMILAEncoder', 'InnerAttentionYANGEncoder', 'InnerAttentionNAACLEncoder', 'ConvNetEncoder', 'LSTMEncoder' ] assert params.encoder_type in encoder_types, "encoder_type must be in " + \ str(encoder_types) infersent_net = InferSent(config_nli_model) print(infersent_net) infersent_net.load_state_dict(torch.load('./encoder/infersent1.pkl')) infersent_net.cuda() for parameters_infer in infersent_net.parameters(): parameters_infer.requires_grad = False ae_model = DisEnc.LinearAutoEncoder(params.dis_emb_dim).cuda() print(ae_model) def cos_distance(a, b): return (1. - torch.nn.functional.cosine_similarity(a, b)) def hamming_distance(a, b): #return (a-b).abs().sum() return torch.nn.functional.pairwise_distance(a, b)
def infersent( path_to_senteval: str, path_to_vectors: str, output_filepath: str = None, cuda_device: int = -1, prototyping_config: bool = False, verbose: bool = False, ) -> None: """Evaluates an InferSent model against the SentEval benchmark (see: https://github.com/facebookresearch/InferSent for information on the pre-trained model). Adapted from: https://github.com/facebookresearch/SentEval/blob/master/examples/infersent.py. """ from models import InferSent def prepare(params, samples): samples = _cleanup_batch(samples) params.infersent.build_vocab([" ".join(tokens) for tokens in samples], tokenize=False) def batcher(params, batch): batch = _cleanup_batch(batch) sentences = [" ".join(tokens) for tokens in batch] embeddings = params.infersent.encode(sentences, bsize=params.batch_size, tokenize=False) return embeddings # Determine the torch device device = _get_device(cuda_device) # Load InferSent model # TODO (John): Hardcoded these to move things along, but that should be fixed. V = 2 MODEL_PATH = "resources/encoder/infersent%s.pkl" % V params_model = { "bsize": 64, "word_emb_dim": 300, "enc_lstm_dim": 2048, "pool_type": "max", "dpout_model": 0.0, "version": V, } infersent = InferSent(params_model) infersent.load_state_dict(torch.load(MODEL_PATH)) infersent.to(device) # Load and initialize the model with word vectors infersent.set_w2v_path(path_to_vectors) trainable_params = sum(p.numel() for p in infersent.parameters() if p.requires_grad) typer.secho( (f"{SUCCESS} Loaded InferSent model {MODEL_PATH}" f" with {trainable_params} trainable parameters."), fg=typer.colors.GREEN, bold=True, ) # Performs a few setup steps and returns the SentEval params params_senteval = _setup_senteval(path_to_senteval, prototyping_config, verbose) params_senteval["infersent"] = infersent _run_senteval(params_senteval, path_to_senteval, batcher, prepare, output_filepath) return
'pool_type': 'max', 'dpout_model': 0.0, 'version': V } nli_net = InferSent(params_model) nli_net.load_state_dict(torch.load(MODEL_PATH)) print(nli_net) # loss weight = torch.FloatTensor(params.n_classes).fill_(1) loss_fn = nn.CrossEntropyLoss(weight=weight) loss_fn.size_average = False # optimizer optim_fn, optim_params = get_optimizer(params.optimizer) optimizer = optim_fn(nli_net.parameters(), **optim_params) # cuda by default nli_net.cuda() loss_fn.cuda() """ TRAIN """ val_acc_best = -1e10 adam_stop = False stop_training = False lr = optim_params['lr'] if 'sgd' in params.optimizer else None def evaluate(epoch, eval_type='valid', final_eval=False): nli_net.eval()