Beispiel #1
0
def inference_no_args(
    data: TensorDataset,
    loader: DataLoader,
    logger: Logger,
    model: BertForSequenceClassification,
    batch_size: int,
) -> List[float]:
    device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
    predictions = []
    states = []
    logger.info("***** Running inference {} *****".format(""))
    logger.info("  Num examples = %d", len(data))
    logger.info("  Batch size = %d", batch_size)
    model.to(device)
    model.eval()
    for batch in tqdm(loader, desc="Inference"):
        batch = tuple(t.to(device) for t in batch)
        logits, state = model.forward(input_ids=batch[0], attention_mask=batch[1], token_type_ids=batch[2],
                                      output_hidden_states=True)
        predictions.extend(logits.cpu())
        states.extend(state[-1][:, 0, :].cpu())
    return predictions, states
Beispiel #2
0
          'num_workers': 0}
training_loader = DataLoader(training_set, **params)
testing_loader = DataLoader(testing_set, **params)
loss_function = nn.CrossEntropyLoss()
learning_rate = 2e-05 
optimizer = optim.Adam(params =  model.parameters(), lr=learning_rate)
if torch.cuda.is_available():
    print("GPU is AVAILABLE!")
    model = model.cuda()

ids, tokens, labels = next(iter(training_loader)) # iterated one element at a time
ids.shape, tokens.shape, labels

if model_type == 'bert':
    print(model_type)
    out = model.forward(ids.cuda())[0]

print(loss_function(out, labels.cuda()))
print(out.shape)

"""**Training the model**"""

def train(model, epochs):
  max_epochs = epochs
  model = model.train()
  for epoch in tqdm_notebook(range(max_epochs)):
      print("EPOCH -- {}".format(epoch))
      correct = 0
      total = 0
      for i, (ids, tokens, labels) in enumerate(training_loader):
          optimizer.zero_grad()