data.manydataset.PadSequenceManyToMany()
Padding collate for ManyToManyDataset batches.
Pads features and targets of a batch of variable-length sequences with zeros to the longest sequence in the batch and records the true lengths, as required by torch.nn.utils.rnn.pack_padded_sequence.
Examples
import pandas as pd
from torch.utils.data import DataLoader
from spotoptim.data.manydataset import ManyToManyDataset, PadSequenceManyToMany
df1 = pd.DataFrame({"x" : [1.0 , 2.0 , 3.0 ], "y" : [2.0 , 4.0 , 6.0 ]})
df2 = pd.DataFrame({"x" : [4.0 , 5.0 ], "y" : [8.0 , 10.0 ]})
ds = ManyToManyDataset([df1, df2], target= "y" )
dl = DataLoader(ds, batch_size= 2 , shuffle= False , collate_fn= PadSequenceManyToMany())
x, lengths, y = next (iter (dl))
print (x.shape, lengths.tolist(), y.shape)
torch.Size([2, 3, 1]) [3, 2] torch.Size([2, 3])