0

Short problem forumlation: I'm classifying longitudinal data using neural networks in my master thesis. I have samples from two one-dimensional curves at 12 evenly spaced time points, 100 samples per curve, and I have used these samples to train a neural network to classify a new sample to correct curve (see code at bottom). My goal is to observe these curves at less time points (1-11) and then predict which curve they belong to. The idea is that these curves are representing "true" models of, for example, blood levels of healthy/sick patients and we want to determine if a patient needs healthcare before it's too late.

The two curves together with one sample each, observed at 12 time points enter image description here

An unknown sample, observed at only 8 time points enter image description here

Question: The feed-forward neural network is designed with 12 input variables, that is, the 12 observations per sample. How can I classify a sample with less observations, such as the green one in the second picture, using my trained neural network? Is this possible with my basic feed-forward neural network, or do I need to use a recurrent neural network and treat the problem as a future-prediction problem?

My R Keras code. I define the curves, generate corresponding data and train neural networks

Define functions and generate data

library(keras)

# Define functions 
x <- seq(0.6,1.13,0.01)

GCM1 <- function(x){
  2*(1.3*x-1)^4 - 0.08*x^6 + 4.04
}

GCM2 <- function(x){
  2.4*(1.3*x-1)^4 - 0.14*x^6 + 4.06
}

# Generate and label data
seqx = seq(0.6, 1.13, length.out = 12)
sd = 0.03

sampleGCM <- data.frame(matrix(nrow=200,ncol = 12))
for(i in 1:100){
  newRow = GCM1(seqx) + rnorm(1,0,sd) + rnorm(12,0,sd)
  sampleGCM[i,] = newRow
}
for(i in 101:200){
  newRow = GCM2(seqx) + rnorm(1,0,sd) + rnorm(12,0,sd)
  sampleGCM[i,] = newRow
}

# Class 0 = Blue function (GCM1)
# Class 1 = Red function (GCM2)
sampleGCM <- cbind(data.frame(Class = rep(c(0,1), each = 100)), sampleGCM)

X = model.matrix(Class ~ . -1, data = sampleGCM)
y = sampleGCM$Class

# Prepare data for training and testing
trainsize <- floor(0.70*nrow(X))
trainid <- sample(1:nrow(X), size = trainsize)
trainX <- X[trainid,]
trainy <- y[trainid]
testX <- X[-trainid,]
testy <- y[-trainid]

# Scale data
trainX <- scale(trainX, center = apply(trainX, 2, mean), scale = apply(trainX, 2, sd))
testX  <- scale(testX, center = apply(testX, 2, mean), scale = apply(testX, 2, sd))

Create and train neural network

GCMNN <- keras_model_sequential() %>%
  layer_dense(units = 24, activation = "relu", input_shape = ncol(X)) %>%
  layer_dense(units = 1, activation = "sigmoid")

GCMNN %>% compile(loss = "binary_crossentropy",
                  optimizer = optimizer_rmsprop(),
                  metrics = c("accuracy"))

system.time(
  history <- GCMNN %>%
    fit(trainX, trainy, epochs = 100, batch_size = 50, validation_split = 0.2)
)


accuracy <- function(pred, truth){
  mean(pred == truth)
}

GCMNN %>% predict_classes(testX) %>% accuracy(testy)

plot(history)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.