Comments (11)
yes. It works. the log likelihood and norm computation are correct and same for both implementations.
My mistake was that I thought the num_tags in pytorch crf included pad,start and stop tags.
Thank you very much. You have been very helpful with issues.
from pytorch_neural_crf.
You mean test the correctness with pytorch-crf?
from pytorch_neural_crf.
yes, the correctness of implementation.
I tested it with small sequence length of N=3, basically hard coded the values of transition and scores in the respective implementations and compare the forward labeled scores.
I can share a dummy sample notebook if you would like to look?
from pytorch_neural_crf.
Sure. no problem. That will be appreciated
from pytorch_neural_crf.
Not sure how you try, but I tried the example here (https://pytorch-crf.readthedocs.io/en/stable/).
everything looks fine.
import torch
from torchcrf import CRF
num_tags = 5 # number of tags is 5
model = CRF(num_tags)
seq_length = 3
batch_size = 2
emissions = torch.randn(seq_length, batch_size, num_tags)
tags = torch.tensor([[0, 1], [2, 4], [3, 1]], dtype=torch.long)
print(model(emissions, tags))
from src.model.module import LinearCRF
from src.data.data_utils import START_TAG, STOP_TAG, PAD
labels = ['A', 'B', 'C', 'D', 'E', START_TAG, STOP_TAG, PAD]
label2idx = {label: idx for idx, label in enumerate(labels)}
mycrf = LinearCRF(label_size=8, label2idx= label2idx, add_iobes_constraint=False, idx2labels=labels)
converted_emissions = torch.cat([emissions.transpose(0, 1), torch.full((batch_size, 3, 3), -10000)], dim=-1)
converted_tags= tags.transpose(0,1)
mask=torch.tensor([[1,1,1],[1,1,1]]).bool()
mycrf.transition.data[:5, :5] = model.transitions.data ## transition between labels
mycrf.transition.data[5, :5] = model.start_transitions.data ## transition from start tag
mycrf.transition.data[:5, 6] = model.end_transitions.data ## transition to end tag
unlabeled, labeled = mycrf(converted_emissions, torch.LongTensor([3,3]), converted_tags, mask)
loglikelihood = labeled- unlabeled
print(loglikelihood)
from pytorch_neural_crf.
thanks for the example, I'll try it with the example and update you on it.
from pytorch_neural_crf.
I am having a slight confusion about computing probability of most likely sequence and would appreciate your help.
viterbi score to probability
As per my understanding it is
np.exp(viterbi_score - z) , both viterbi_score and z are in log space.
if I change my my dataloader batch_size then the value of z changes as it is dependent on the batch (input). The problem is then I cant use batch_size > 1.
which is not efficient.
On intuitive side, the probability of sequence should be same given the same model parameters, irrespective of the batch_size.
Is my reasoning correct?
from pytorch_neural_crf.
Not really.
Your viterbi score
is always with size batch_size
, same as z
.
So even if you do exponential you still have the final probability with size "batch_size".
So the size of your batch, will never affect the value in each instance
from pytorch_neural_crf.
Ok. I'll try to write a test case and validate.
As per size of z
, on top of mind I think it is a scalar not a vector of batch_size
which requires setting batch_size=1.
from pytorch_neural_crf.
my Mistake , I assumed the z returned were of batchsize
but the forward unlabel already sums them and then return it.
here last_alphas are the z values.
Maybe it would be a good idea, send the individual z rather than sum(z) , which would make forward_unlabeled api more useful,eg computing probability.
Again, thanks for help.
from pytorch_neural_crf.
yeah, seems it might make the code more readable. I will see how I can do that
from pytorch_neural_crf.
Related Issues (20)
- AttributeError: 'str' object has no attribute 'size' HOT 2
- Macro F1 and Precision HOT 2
- get error when running in torch 1.81 HOT 4
- ValueError: The label B-MISC does not exist in label2idx dict. The label might not appear in the training set. HOT 1
- How to tweak the learning rate of CRF layer? HOT 1
- Errors when running the default model HOT 7
- Is there a way to use local model checkpoints for Hugginface models ? HOT 3
- About orig_to_token_index padding problem HOT 2
- About CRF layer HOT 3
- Evaluation question HOT 1
- About GLOVE result F1 91.36 HOT 9
- Can not run ner_predictor.py HOT 3
- Please help me? HOT 7
- Summary of training/finetuning/prediction commands HOT 2
- Learning on Heterogeneous Tag Sets using Tag Hierarchy HOT 1
- Use custom pre-trained BERT models HOT 2
- purpose of token_type_ids or segments HOT 2
- load data error HOT 5
- ImportError: cannot import name 'context_models' from 'src.config' when running transformers_predictor.py HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytorch_neural_crf.