Commit 6f18e6f3 by Andrea Mancini

add domain span probs output

parent 56b69ec7
...@@ -69,7 +69,7 @@ class DomainSpanESM(nn.Module): ...@@ -69,7 +69,7 @@ class DomainSpanESM(nn.Module):
torch.nn.init.xavier_uniform_(self.span_outputs.weight) torch.nn.init.xavier_uniform_(self.span_outputs.weight)
def forward(self, inputs, start=None, end=None): def forward(self, inputs, start=None, end=None, return_span_logits=False):
# Get hidden states from ESM # Get hidden states from ESM
if 'esm' in self.config.version: if 'esm' in self.config.version:
...@@ -122,8 +122,23 @@ class DomainSpanESM(nn.Module): ...@@ -122,8 +122,23 @@ class DomainSpanESM(nn.Module):
mask[valid_span] = span_mask mask[valid_span] = span_mask
start_pred_pos = start_pred_pos[valid_span]
end_pred_pos = end_pred_pos[valid_span]
start_logits = start_logits[valid_span]
end_logits = end_logits[valid_span]
else:
start_pred_pos = None
end_pred_pos = None
start_logits = None
end_logits = None
x = (last_hidd * mask.unsqueeze(-1)).sum(dim=1) / (mask.sum(dim=1, keepdim=True) + 1e-8) x = (last_hidd * mask.unsqueeze(-1)).sum(dim=1) / (mask.sum(dim=1, keepdim=True) + 1e-8)
logits = self.clf_head(x) logits = self.clf_head(x)
return logits, span_loss, (start_pred_pos, end_pred_pos) outputs = logits, span_loss, (start_pred_pos, end_pred_pos)
\ No newline at end of file
if return_span_logits:
outputs += (start_logits, end_logits)
return outputs
\ No newline at end of file
...@@ -48,6 +48,8 @@ class InferencePipeline: ...@@ -48,6 +48,8 @@ class InferencePipeline:
if self.model.config.domain: if self.model.config.domain:
dataframe['start_pred'] = predictions['start_pred'] dataframe['start_pred'] = predictions['start_pred']
dataframe['end_pred'] = predictions['end_pred'] dataframe['end_pred'] = predictions['end_pred']
dataframe['start_probs'] = predictions['start_probs']
dataframe['end_probs'] = predictions['end_probs']
print(dataframe) print(dataframe)
if save: if save:
...@@ -101,7 +103,7 @@ class InferencePipeline: ...@@ -101,7 +103,7 @@ class InferencePipeline:
criterion = torch.nn.functional.cross_entropy criterion = torch.nn.functional.cross_entropy
metric = EvalMetrics(len(self.model.config.labels)) metric = EvalMetrics(len(self.model.config.labels))
tot_loss, tot_loss_span, start_acc, end_acc, start_pred, end_pred = evaluate( tot_loss, tot_loss_span, start_acc, end_acc, start_pred, end_pred, start_probs, end_probs = evaluate(
self.model, criterion, metric, loader, verbose=verbose, device=self.device, return_span=True self.model, criterion, metric, loader, verbose=verbose, device=self.device, return_span=True
) )
...@@ -118,7 +120,9 @@ class InferencePipeline: ...@@ -118,7 +120,9 @@ class InferencePipeline:
'y_pred': metric.y_pred, 'y_pred': metric.y_pred,
'y_prob': metric.y_prob, 'y_prob': metric.y_prob,
'start_pred': start_pred, 'start_pred': start_pred,
'end_pred': end_pred 'end_pred': end_pred,
'start_probs': start_probs,
'end_probs': end_probs
} }
if __name__ == "__main__": if __name__ == "__main__":
# options for using from the command line # options for using from the command line
......
...@@ -6,3 +6,4 @@ torchsummary>=1.5.1 ...@@ -6,3 +6,4 @@ torchsummary>=1.5.1
scikit-learn>=1.5.1 scikit-learn>=1.5.1
tqdm>=4.64.0 tqdm>=4.64.0
transformers>=4.45.2 transformers>=4.45.2
joblib>=1.5.3
\ No newline at end of file
from collections import defaultdict from collections import defaultdict
from tracemalloc import start
import pandas as pd import pandas as pd
import seaborn as sn import seaborn as sn
...@@ -155,6 +156,7 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r ...@@ -155,6 +156,7 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r
iterator = zip(pbar, loader) if verbose > 1 else enumerate(loader) iterator = zip(pbar, loader) if verbose > 1 else enumerate(loader)
start_pred, end_pred = list(), list() start_pred, end_pred = list(), list()
start_probs, end_probs = list(), list()
model.to(device) model.to(device)
model.eval() model.eval()
...@@ -171,7 +173,7 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r ...@@ -171,7 +173,7 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r
end_pos = inputs['end'].to(device) end_pos = inputs['end'].to(device)
w = 0.7 w = 0.7
logits, span_loss, (start_pred_pos, end_pred_pos) = model(seq_input, start_pos, end_pos) logits, span_loss, (start_pred_pos, end_pred_pos), (start_logits, end_logits) = model(seq_input, start_pos, end_pos, return_span_logits=True)
y_probs = F.softmax(logits, dim=-1) y_probs = F.softmax(logits, dim=-1)
y_pred = torch.argmax(y_probs, dim=-1).cpu().numpy() y_pred = torch.argmax(y_probs, dim=-1).cpu().numpy()
...@@ -199,6 +201,10 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r ...@@ -199,6 +201,10 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r
start_pred.extend(start_pred_pos.cpu().numpy()) start_pred.extend(start_pred_pos.cpu().numpy())
end_pred.extend(end_pred_pos.cpu().numpy()) end_pred.extend(end_pred_pos.cpu().numpy())
if start_logits is not None and end_logits is not None:
start_probs.extend(F.softmax(start_logits, dim=-1).cpu().numpy())
end_probs.extend(F.softmax(end_logits, dim=-1).cpu().numpy())
tot_loss /= num_samples tot_loss /= num_samples
tot_loss_span /= num_samples tot_loss_span /= num_samples
...@@ -207,6 +213,6 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r ...@@ -207,6 +213,6 @@ def evaluate(model, criterion, metrics: EvalMetrics, loader, verbose , device, r
output = tot_loss, tot_loss_span, start_accuracy, end_accuracy output = tot_loss, tot_loss_span, start_accuracy, end_accuracy
if return_span: if return_span:
output += (start_pred, end_pred,) output += (start_pred, end_pred, start_probs, end_probs)
return output return output
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment