File size: 9,423 Bytes
b8b2212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
language:
- fr
- en
---

---
language:
- en
- fr
---

# Title-Paragraph Segmentation Model - ver 1.0

<!-- Provide a quick summary of what the model is/does. -->

Formal Content Segmentation Model that belongs to *first order segmentation* (FOS) model family. It performs
`title-paragraph separation` task.

Architecture:
- E5-base Cross Encoder

Dataset:
- Custom Constrative Title-Paragraph Dataset based off `wikitext`

Performance:
- 89% acc on test set


Broader context:

  1) The aim of FOS is to separate content types featured in raw instructured strings such as:
  * text
  * code
  * tables
  * list
  * math formulas
  * images
  
  
  2) This will enable further processings such as *second order segmentation* (SOS) that
  aims at generating semantic frontiers i.e segmenting:
  * plain text into knowledge units
  * code into functional blocks
  * math formulas blocks into equations
  * objects/concepts within an image
  * videos into timestamped chapters


## Model Details

### Direct Use

###### Setup and Utilities

```python
from transformers import XLMRobertaPreTrainedModel, XLMRobertaModel, AutoTokenizer
from nltk.tokenize import line_tokenize
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from datasets import Dataset


#Utility Functions


def get_default_device():
    if torch.cuda.is_available():
        return torch.device('cuda')
    elif torch.backends.mps.is_available():
        return torch.device('mps')
    else:
        return torch.device('cpu')
    
def to_device(data, device):
    if isinstance(data, (list,tuple)):
        return [to_device(x, device) for x in data]
    elif isinstance(data, dict):
        return {'input_ids':to_device(data['input_ids'],device),'attention_mask':to_device(data['attention_mask'],device)}
    return data.to(device)

class DeviceDataLoader():
    def __init__(self, dl, device):
        self.dl = dl
        self.device = device
        
    def __iter__(self):
        for b in self.dl: 
            yield to_device(b, self.device)

    def __len__(self):
        return len(self.dl)

class IsoBN(nn.Module):
    def __init__(self, hidden_size):
        """Init method"""
        super().__init__()
        self.register_parameter(name='cov', param=torch.nn.Parameter(torch.zeros(hidden_size, hidden_size)))
        self.register_parameter(name='std', param=torch.nn.Parameter(torch.zeros(hidden_size)))

        self.cov.requires_grad = False
        self.std.requires_grad = False

    def forward(self, input, momentum: float = 0.05, eps: float = 1e-3, beta: float = 0.5):
        """Forward method"""
        if self.training:
            x = input.detach()
            n = x.size(0)
            mean = x.mean(dim=0)
            y = x - mean.unsqueeze(0)
            std = (y ** 2).mean(0) ** 0.5
            cov = (y.t() @ y) / n
            self.cov.data += momentum * (cov.data - self.cov.data)
            self.std.data += momentum * (std.data - self.std.data)
        corr = torch.clamp(self.cov / torch.ger(self.std, self.std), -1, 1)
        gamma = (corr ** 2).mean(1)
        denorm = (gamma * self.std)
        scale = 1 / (denorm + eps) ** beta
        E = torch.diag(self.cov).sum()
        new_E = (torch.diag(self.cov) * (scale ** 2)).sum()
        m = (E / (new_E + eps)) ** 0.5
        scale *= m
        return input * scale.unsqueeze(0).detach()

class e5_base_CTSEG(XLMRobertaPreTrainedModel):
    
    def __init__(self, config):
        
        super().__init__(config)
        
        self.e5 = XLMRobertaModel(config).from_pretrained('intfloat/multilingual-e5-base')
        self.dropout = nn.Dropout(0.5)
        self.linear_1 = nn.Linear(768,256)
        self.linear_2 = nn.Linear(256,128)
        self.linear_3 = nn.Linear(128,2)
        self.relu = nn.ReLU()
        self.isobn = IsoBN(768)
        
    def forward(self, sent):
        
        sent['input_ids'] = sent['input_ids'].reshape(sent['input_ids'].shape[0],-1)
        sent['attention_mask'] = sent['attention_mask'].reshape(sent['attention_mask'].shape[0],-1)
        
        hs= self.e5(input_ids=sent['input_ids'], attention_mask=sent['attention_mask'])
        cls_hs = hs.last_hidden_state[:, 0]
        cls_hs = self.isobn(cls_hs)
        
    
        out = self.linear_1(cls_hs)
        out = self.relu(out)
        out = self.dropout(out)
        out = self.linear_2(out)
        out = self.relu(out)
        out = self.dropout(out)
        out = self.linear_3(out)
        
        
        return out
    
    
    def training_step(self, sent, labels):
        out = self.forward(sent)
        loss = F.cross_entropy(out, labels)
        return loss
    
    def validation_step(self, sent, labels):
        out = self.forward(sent)
        loss = F.cross_entropy(out, labels)
        acc = accuracy(out, labels)
        return {'val_acc':acc,'val_loss':loss.detach()}
    
    def validation_epoch_end(self, metrics):
        batch_losses = [x['val_loss'] for x in metrics]
        batch_accs =  [x['val_acc'] for x in metrics]
        
        epoch_loss = torch.stack(batch_losses).mean().item()
        epoch_acc = torch.stack(batch_accs).mean().item()
        
        return {'val_loss':epoch_loss, 'val_acc':epoch_acc}
    
    def epoch_end(self, epoch, result):
        
        print("Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}".format(
            epoch, result['train_loss'], result['val_loss'], result['val_acc']))
        
    def evaluate(self, val_loader):
        self.eval()
        metrics = [self.validation_step(sent,labels.type(torch.LongTensor).to(device, non_blocking=True)) for sent,labels in val_loader]
        return self.validation_epoch_end(metrics)
def accuracy(out, labels):    
    return (out.argmax(dim=1) == labels).sum()/labels.numel()

```

```python
tokenizer = AutoTokenizer.from_pretrained('intfloat/multilingual-e5-base')
model = e5_base_CTSEG.from_pretrained('ProfessorBob/title-par-segmentation')
device = get_default_device()
to_device(model,device)
```

```python
def infer_block(
    chunks, 
    batch_size: int = 8, 
    return_probability: bool = False,
    tokenizer = tokenizer
):
    """ Bulk Infer function"""

    tok_text_bulk = tokenizer(
        ['query: ' + sent[0] +'[SEP]'+ sent[1] for sent in chunks],
        padding='max_length',
        truncation=True,
        return_tensors='pt'
    )
    sentences = Dataset.from_dict({
        'input_ids': tok_text_bulk['input_ids'],
        'attention_mask':  tok_text_bulk['attention_mask']
    })
    sentences.set_format(
        'torch', 
        columns=['input_ids','attention_mask']
    )
    sentences = DataLoader(
        sentences, 
        batch_size=batch_size, 
        pin_memory=True
    )
    sentences = DeviceDataLoader(sentences, device)
    preds = list()
    model.eval()
    with torch.no_grad():
        for i, batch in enumerate(sentences):
            out = model(batch)
            if return_probability:
                preds.extend((out.softmax(dim=1).cpu()[:, 1]).tolist())
            else:
                preds.extend(out.argmax(dim=1).cpu().tolist())
    
    if device == torch.device('cuda'):
        torch.cuda.empty_cache()
    assert len(preds) == len(chunks)
    
    return preds, out

def segmentation_pipeline(text):

    block = line_tokenize(text)
    chunks =  [
                    (u, v) for u, v in zip(block[:-1], block[1:])
                ]
    preds, out = infer_block(chunks,return_probability=False)
    cut_idx = [i+1 for i, value in enumerate(preds) if value == 1]
    cut_idx = [0]+cut_idx+[len(block)]
    seg = [block[cut_idx[i]:cut_idx[i+1]] for i in range(len(cut_idx)-1)]

    return seg
```

###### Usage example


```python

mixed_string = """

Ancient Foundations (3000 BCE - 600 CE)

In the dawn of human civilization, mathematics emerged as an essential tool for commerce, construction, and astronomy. Explore the mathematical innovations of ancient cultures such as the Babylonians, Egyptians, and Greeks, laying the groundwork for numerical systems, geometry, and the Pythagorean theorem.

The Golden Age of Islamic Mathematics (700 CE - 1300 CE)

Delve into the intellectual flourishing during the Islamic Golden Age, where scholars like Al-Khwarizmi and Omar Khayyam made groundbreaking contributions to algebra, trigonometry, and the development of algorithms. Discover how these advancements paved the way for the Renaissance in Europe.

"""

```

Generated Title-Paragraph Segmentation

```console
Block 1
-----
Ancient Foundations (3000 BCE - 600 CE)

Block 2
-----
In the dawn of human civilization, mathematics emerged as an essential tool for commerce, construction, and astronomy. Explore the mathematical innovations of ancient cultures such as the Babylonians, Egyptians, and Greeks, laying the groundwork for numerical systems, geometry, and the Pythagorean theorem.

Block 3
-----
The Golden Age of Islamic Mathematics (700 CE - 1300 CE)

Block 4
-----
Delve into the intellectual flourishing during the Islamic Golden Age, where scholars like Al-Khwarizmi and Omar Khayyam made groundbreaking contributions to algebra, trigonometry, and the development of algorithms. Discover how these advancements paved the way for the Renaissance in Europe.

```



<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->