(batch)
| 250 | } |
| 251 | |
| 252 | def comments_collate_fn(batch): |
| 253 | num_comments = 23 |
| 254 | |
| 255 | comments_like = [item['comments_like'] for item in batch] |
| 256 | comments_inputid = [item['comments_inputid'] for item in batch] |
| 257 | comments_mask = [item['comments_mask'] for item in batch] |
| 258 | |
| 259 | comments_inputid_resorted = [] |
| 260 | comments_mask_resorted = [] |
| 261 | comments_like_resorted = [] |
| 262 | |
| 263 | for idx in range(len(comments_like)): |
| 264 | comments_like_one = comments_like[idx] |
| 265 | comments_inputid_one = comments_inputid[idx] |
| 266 | comments_mask_one = comments_mask[idx] |
| 267 | if comments_like_one.shape != torch.Size([0]): |
| 268 | comments_inputid_one, comments_mask_one, comments_like_one = (list(t) for t in zip(*sorted(zip(comments_inputid_one, comments_mask_one, comments_like_one), key=lambda s: s[2], reverse=True))) |
| 269 | comments_inputid_resorted.append(comments_inputid_one) |
| 270 | comments_mask_resorted.append(comments_mask_one) |
| 271 | comments_like_resorted.append(comments_like_one) |
| 272 | |
| 273 | comments_inputid = pad_sequence(num_comments,comments_inputid_resorted,250) |
| 274 | comments_mask = pad_sequence(num_comments,comments_mask_resorted,250) |
| 275 | comments_like=[] |
| 276 | for idx in range(len(comments_like_resorted)): |
| 277 | comments_like_resorted_one = comments_like_resorted[idx] |
| 278 | if len(comments_like_resorted_one)>=num_comments: |
| 279 | comments_like.append(torch.tensor(comments_like_resorted_one[:num_comments])) |
| 280 | else: |
| 281 | if isinstance(comments_like_resorted_one, list): |
| 282 | comments_like.append(torch.tensor(comments_like_resorted_one+[0]*(num_comments-len(comments_like_resorted_one)))) |
| 283 | else: |
| 284 | comments_like.append(torch.tensor(comments_like_resorted_one.tolist()+[0]*(num_comments-len(comments_like_resorted_one)))) |
| 285 | |
| 286 | label = [item['label'] for item in batch] |
| 287 | |
| 288 | return { |
| 289 | 'label': torch.stack(label), |
| 290 | 'comments_inputid': comments_inputid, |
| 291 | 'comments_mask': comments_mask, |
| 292 | 'comments_like': torch.stack(comments_like), |
| 293 | } |
| 294 | |
| 295 | def title_w2v_collate_fn(batch): |
| 296 | length_title = 128 |
nothing calls this directly
no test coverage detected