stefanonardo / pytorch-esn

An Echo State Network module for PyTorch.
MIT License
205 stars 43 forks source link

How to solve the situation of multiple data in one sample #8

Closed wky1998 closed 5 years ago

wky1998 commented 5 years ago

thank you for your sharing,but i don't know how to solve the data such as(time_step,batch,input_size),time_step is the number of data per sample,and time_step is different in each sample. and input_size=2. i don't konw the means of for i in steps in the function of Recurrent. it means 5000 samples or time_step is 5000?

stefanonardo commented 5 years ago

When you have to deal with sequences of different length, you should use Packed Sequences. You can follow this procedure (obviously you need to know the length of each sequence):

TRAINING

parameters: 
# Tensor of size (max_timestep, batch, input_size) containing the sequences sorted
# by length. You must pad shorter sequences (typically with zero).
input_sequences
# Sorted list containing the length of each sequence
lengths

# Pack input
packed_input = torch.nn.utils.rnn(pack_padded_sequence(input_sequences, lengths)

# Prepare target
target_flat = aux.prepare_target(target, lengths, washout)

model(packed_input, washout, None, target_flat)

TEST

parameters: 
# Tensor of size (max_timestep, batch, input_size) containing the sequences sorted
# by length. You must pad shorter sequences (typically with zero).
input_sequences
# Sorted list containing the length of each sequence
lengths

# Pack input
packed_input = torch.nn.utils.rnn(pack_padded_sequence(input_sequences, lengths)

output, hidden = model(packed_input, washout)

# Prepare target for comparison, removing washout and padding the sequences.
washout_target, washout_lengths = aux.washout_tensor(target, washout, lengths)

# Remember to use (you'll have to implement it usually) a loss function 
# which discards padding values.
loss = loss_fcn(output, washout_target, washout_lengths)
wky1998 commented 5 years ago

Thank you for your answer.it's great, but i still have some questions. My data have 5000 sample and each sample have different length,but input size are all 2. each sample has a target y. like this sample [[Dimension,longitude],[Dimension,longitude],[Dimension,longitude],[Dimension,longitude],].i need to use previous position to predict the last [Dimension,longitude]. Because each sample have different number of [Dimension,longitude], i need to pack them to max_length? then input shape is (max_lenght,batch,input_size), and "for i in steps " in the function of Recurrent, steps is max_lenght? suppose this how can i train these 5000 samples?

stefanonardo commented 5 years ago

Why are you interested in Recurrent? You only have to worry about ESN interface. As I said, prepare a tensor containing all your sequences, appropriately padded whenever necessary, then pack them with PyTorch utilities. Anyway this problem is not really inherent to pytorch-esn you can find many resources about the usage of variable length sequences with PyTorch. The way you can use packed sequences is the same here.