-
Notifications
You must be signed in to change notification settings - Fork 7
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
Question: Can Torchsurv Handle Time Series Data for Survival Analysis? #42
Comments
Hey @Lamgayin , thank you for your question. For now, time-dependent covariates are not supported by TorchSurv. We will add this as a potential new feature. Best regards, Melodie |
Hi @Lamgayin,
I attached a simple code example to illustrate how to use time series with RNN with Good luck! import torch
from torchsurv.loss import cox
from torchsurv.metrics.cindex import ConcordanceIndex
# Parameters
input_size = 10
output_size = 1
num_layers = 2
seq_length = 5
batch_size = 8
# make random boolean events
events = torch.rand(batch_size) > 0.5
print(events) # tensor([ True, False, True, True, False, False, True, False])
# make random positive time to event
time = torch.rand(batch_size) * 100
print(time) # tensor([32.8563, 38.3207, 24.6015, 72.2986, 19.9004, 65.2180, 73.2083, 21.2663])
# Create simple RNN model
rnn = torch.nn.RNN(input_size, output_size, num_layers)
inputs = torch.randn(seq_length, batch_size, input_size)
h0 = torch.randn(num_layers, batch_size, output_size)
# Forward pass time series input
outputs, _ = rnn(inputs, h0)
estimates = outputs[-1] # Keep only last predictions, many to one approach
print(estimates.size()) # torch.Size([8, 1])
print(f"Estimate shape for {batch_size} samples = {estimates.size()}") # Estimate shape for 8 samples = torch.Size([8, 1])
loss = cox.neg_partial_log_likelihood(estimates, events, time)
print(f"loss = {loss}, has gradient = {loss.requires_grad}") # loss = 1.0389232635498047, has gradient = True
cindex = ConcordanceIndex()
print(f"c-index = {cindex(estimates, events, time)}") # c-index = 0.20000000298023224
|
@tcoroller Thank you for taking the time to reply to my question!!! It helped me a lot and my problem has been solved based on your suggestions 🙏🙏 |
Dear authors, hello, I am a newcomer in the field of survival analysis. I have a research topic regarding the application of time series models to survival analysis, and I am not sure if torchsurv supports such functionality.thx a lot
The text was updated successfully, but these errors were encountered: