harvardnlp / pytorch-struct

Fast, general, and tested differentiable structured prediction in PyTorch
http://harvardnlp.github.io/pytorch-struct
MIT License
1.11k stars 93 forks source link

Single-root vs multi-root dependency trees #65

Closed kmkurn closed 4 years ago

kmkurn commented 4 years ago

Hi, it's me again. Quick question, does DependencyCRF support single-root trees? That is, the root symbol has exactly one dependent. NonProjectiveDependencyCRF does support it, but DependencyCRF seems to support only the multi-root case.

srush commented 4 years ago

Good question. Please keep asking and treat the code like it is your own.

Currently that algorithm is multi-root. If you want single root, then we have to update the dynamic program slightly. In particular at the bottom of the function https://github.com/harvardnlp/pytorch-struct/blob/master/torch_struct/deptree.py#L100 you should call semiring.zero_(alpha[A][C][R].data[:, b, 0, k]) for any value k < lengths[b]. This is a bit hacky, but I think it will work. For the test you can just use the existing function here to confirm it https://github.com/harvardnlp/pytorch-struct/blob/master/torch_struct/deptree.py#L183

srush commented 4 years ago

Checked in now.