Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I get the predicted results? #6

Closed
Ying-Kang opened this issue May 29, 2020 · 20 comments
Closed

How could I get the predicted results? #6

Ying-Kang opened this issue May 29, 2020 · 20 comments

Comments

@Ying-Kang
Copy link

This repo hightlights me a lot

However, I focus more on the predicted results In my work.

It's hard for me to understand your implementation datails to deduct the predict results as I just step into the area for few months.

Could you please tell me how to get the predicted results based on your code?

I'd appreciated it if you can help .

Thanks any way

@Ying-Kang Ying-Kang changed the title How could I get the predict results? How could I get the predicted results? May 29, 2020
@shchur
Copy link
Owner

shchur commented Jun 1, 2020

Hi, please have a look at #3 and #5.

@shchur
Copy link
Owner

shchur commented Jun 8, 2020

Please have a look at my answer #5 (comment)

@KristenMoore
Copy link

@Kouin - did you figure it out? I'm trying to do the same.

@shchur
Copy link
Owner

shchur commented Jun 29, 2020

I provided the code for generating predictions in the thread for another issue #5 (comment) and it seems to work for the original poster there

@KristenMoore
Copy link

KristenMoore commented Jun 29, 2020

Thanks, I was thrown off by the comment in #3 (comment) about the step() function in RNNLayer being needed when generating new sequences. Has it been used in #5 (comment)?

@shchur
Copy link
Owner

shchur commented Jun 29, 2020

I'm not sure if I understand what exactly you want to do. Can you describe it in more detail?

If you want to sample new trajectories from the TPP, you will need to use RNNLayer.step. At each step you will sample the next inter-event time \tau_{i} from p(\tau_i | History_i) and feed it into the RNN to obtain the parameters for p(\tau_{i+1} | History_{i+1}).

If you want to get the predictions one step into the future (i.e. you want to compute the expected time until the next event \mathbb{E}[\tau_i | \History_i]) you should use the code from #5 that I referenced. The code that I wrote there computes the expected time until the next event for all events in the batch. You can use it to, for example, compute the mean squared error or mean absolute error in the event time prediction task.

@KristenMoore
Copy link

Thanks a lot for the explanation - I want to do the former. I'll follow these directions.

@shchur
Copy link
Owner

shchur commented Jun 29, 2020

One more important detail: make sure that you apply the relevant transformations to the samples before feeding them into the RNN. Under default settings, we transform the RNN input in_times by applying logarithm (code), and also additionally normalize the values to have zero mean and unit standard deviation (code) using the statistics of the training set.

@KristenMoore
Copy link

Thanks a lot!

@cjchristopher
Copy link

Hi @shchur - apologies if this is a silly question! Just on the above instructions for sampling new trajectories with RNNLayer.step - is the suggestion that this would be done as model.rnn.step() in a sampling loop? I'm running into problems trying to do this correctly after training the model as per your example in the interactive notebook. We're interested in sampling new trajectories from i=0, or extending one of the input samples. Any assistance getting us on the right track is appreciated!

dl_train = torch.utils.data.DataLoader(d_train, batch_size=1, shuffle=False, collate_fn=collate)
for x in dl_train:
    break
y, h = model.rnn.step(x, model.rnn(x))

@shchur
Copy link
Owner

shchur commented Aug 24, 2020

Hi @cjchristopher, here is my implementation of sampling for entire trajectories

next_in_time = torch.zeros(1, 1, 1)
h = torch.zeros(1, 1, history_size)
inter_times = []
t_max = 1000
with torch.no_grad():
    while sum(inter_times) < t_max:
        _, h = model.rnn.step(next_in_time, h)
        tau = model.decoder.sample(1, h)
        inter_times.append(tau.item())
        next_in_time = ((tau + 1e-8).log() - mean_in_train) / std_in_train

@avs123
Copy link

avs123 commented Oct 9, 2020

Hi @shchur, thanks for sharing the code. I need your clarification on how you denormalize the sample generated. The transformation you applied [next_in_time = ((tau + 1e-8).log() - mean_in_train) / std_in_train] doesn't seem to work as my input has discrete integer times and outputs generated are fractional. Please assist by providing possible de-normalization code that works on your model. Thanks :)

@shchur
Copy link
Owner

shchur commented Oct 11, 2020

Hi @avs123, do I understand it correctly that you want the model to generate discrete inter-event times? This is currently not supported, as the model is learning a continuous probability distribution for the inter-event times, so the sampled inter-event times tau will all be continuous. A potential hacky solution is to discretize the inter-event times after they are sampled with torch.ceil(tau).

@avs123
Copy link

avs123 commented Oct 11, 2020

Thanks for responding on time @shchur. Can you please confirm that is the generated tau the actual inter-event time or do we need to apply some transformation to get it in the time space as is the input sequence? Anything to nullify the effect of log and normalisation transformation that is being applied to the input. Please elaborate.

@shchur
Copy link
Owner

shchur commented Oct 11, 2020

The transformations applied to the RNN input are not related to the transformations applied to the output, so tau should already be the correct inter-event time.

@KristenMoore
Copy link

Thanks for the refactored code, @shchur .
To sample in the new framework, do I just need to implement sampling from inter_time_dist?

features = self.get_features(batch)  
context = self.get_context(features)   
inter_time_dist = self.get_inter_time_dist(context)

Any tips would be appreciated. Thanks.

@shchur
Copy link
Owner

shchur commented Jan 18, 2021

Hey @KristenMoore, I have just implemented sampling for the new code. I checked it on a few datasets (see interactive.ipynb) and it seems that the generated sequences look fine. I haven't tested it on marked sequences, though. The code seems simple enough, so I hope that there are no mistakes, but let me know if anything seems odd.

I also realized that there was a very serious bug that I introduced while refactoring. The slicing for context embedding was off-by-one, which means that the model was peeking into the future. This is fixed by the last commit.

@KristenMoore
Copy link

Great - thanks @shchur!
I will let you know if I notice anything that seems odd.

@KristenMoore
Copy link

Hi @shchur - just one question about the sampling.
Why is the torch.no_grad() only applied to this one line: https://github.com/shchur/ifl-tpp/blob/master/code/dpp/models/recurrent_tpp.py#L178

Thanks.

@shchur
Copy link
Owner

shchur commented Feb 18, 2021

There is no reason, really, I guess you could just do .detach() and get the same result. You could also wrap the entire call to sample in a no_grad() context if you don't need to differentiate the samples afterwards.

@shchur shchur closed this as completed Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants