glanceable-io / ordinal-log-loss Goto Github PK
View Code? Open in Web Editor NEWRepository of the COLING 2022 paper : Ordinal Log-Loss - A simple log-based loss function for ordinal text classification.
Home Page: https://www.glanceable.io
Repository of the COLING 2022 paper : Ordinal Log-Loss - A simple log-based loss function for ordinal text classification.
Home Page: https://www.glanceable.io
Hello!
I've read the articles on ordinal log loss and very impressed with the idea of distances.
I tried to use OLL in my study, and I found some code which is not familiar to me.
def compute_loss(self, model, inputs, return_outputs=False):
num_classes = model.module.num_labels
dist_matrix = model.module.dist_matrix
labels = inputs["labels"]
outputs = model(**inputs)
logits = outputs.logits
probas = F.softmax(logits,dim=1)
true_labels = [num_classes*[labels[k].item()] for k in range(len(labels))]
label_ids = len(labels)*[[k for k in range(num_classes)]]
distances = [[float(dist_matrix[true_labels[j][i]][label_ids[j][i]]) for i in range(num_classes)] for j in range(len(labels))]
distances_tensor = torch.tensor(distances,device='cuda:0', requires_grad=True)
err = -torch.log(1-probas)*abs(distances_tensor)**2
loss = torch.sum(err,axis=1).mean()
return (loss, outputs) if return_outputs else loss
In this code, the following line:
distances_tensor = torch.tensor(distances,device='cuda:0', requires_grad=True)
According to my best knowledge, distances_tensor itself did not change but just "given" by label information. So I think distances_tensor does not need to have "requires_grad=True". Even after removing those code, loss.backward() seems to work properly.
Is there any reason distances_tensor have "requires_grad=True"?
Thanks in advance.
Hi,
Thanks for the code you shared.
I would like to copy the code to my project, but I encountered some mistakes.
Could you give an example of the parameters below? (such as logits, probas, true labels, dist_mat, distances)
Thanks in advance.
class OLL1Trainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False):
num_classes = model.module.num_labels
dist_matrix = model.module.dist_matrix
labels = inputs["labels"]
outputs = model(**inputs)
logits = outputs.logits
probas = F.softmax(logits,dim=1)
true_labels = [num_classes*[labels[k].item()] for k in range(len(labels))]
label_ids = len(labels)*[[k for k in range(num_classes)]]
distances = [[float(dist_matrix[true_labels[j][i]][label_ids[j][i]]) for i in range(num_classes)] for j in range(len(labels))]
distances_tensor = torch.tensor(distances,device='cuda:0', requires_grad=True)
err = -torch.log(1-probas)*distances_tensor
loss = torch.sum(err,axis=1).mean()
return (loss, outputs) if return_outputs else loss
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.