rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
831 stars 283 forks source link

How to use predict function #259

Closed MislavSag closed 6 years ago

MislavSag commented 6 years ago

I am trying to replicate Siraj's code for predicting stock prices in R (https://github.com/llSourcell/How-to-Predict-Stock-Prices-Easily-Demo).

This is my code:

url <- "https://raw.githubusercontent.com/llSourcell/How-to-Predict-Stock-Prices-Easily-Demo/master/sp500.csv"
sp500 <- read.csv(url, header = FALSE, stringsAsFactors = FALSE)
colnames(sp500) <- "closingPrice"

# choose sequence length
seq_length <- 50
sequence_length <- seq_length + 1
result <- list()
for (i in 1:(nrow(sp500) - seq_length)){
  result[[i]] <- sp500[i : (i + seq_length),1]
}

# normalised data
normalised_data <- list()
for (i in 1:length(result)){
  normalised_window <- ((result[[i]] / result[[i]][[1]]) - 1)
  normalised_data[[i]] <- normalised_window
}
result <- normalised_data

# test <- do.call(rbind, result)
# define train and test datasets
row <- round(0.9 * length(result))
train <- result[1:as.integer(row)]
# train <- sample(train)
x_train <- lapply(train, '[', -length(train[[1]]))
y_train <- lapply(train, '[', length(train[[1]]))
y_train <- unlist(y_train)
test = result[(as.integer(row)+1):length(result)]
x_test <- lapply(test, '[', -length(test[[1]]))
y_test <- lapply(test, '[', length(test[[1]]))

x_train <- array(as.numeric(unlist(x_train)), dim = c(3709, 50, 1))
x_test <- array(as.numeric(unlist(x_test)), dim = c(412, 50, 1))
# x_train <- as.matrix(x_train, dim = c(3709, 51))
# x_test <- as.matrix(x_test, dim = c(412, 51))

class(x_train)

#########################
# Step 2: Build a model #
#########################

library(keras)

model <- keras_model_sequential()
model %>% layer_lstm(units = 50L, return_sequences = TRUE, input_shape = list(NULL, 1)) %>%
  layer_dropout(0.2) %>%
  layer_lstm(units = 50L, return_sequences = FALSE) %>%
  layer_dropout(0.2) %>%
  layer_dense(1L) %>%
  layer_activation('linear')
summary(model)

model %>% compile(
  optimizer = 'rmsprop',
  loss = 'mse'
)

###########################
# Step 2: Train the model #
###########################

model %>% fit(x_train, y_train, epochs=1, batch_size=512, validation_split = 0.05)

Now I would like to get predicted values as in Siraj's python code:

def predict_sequences_multiple(model, data, window_size, prediction_len):
    #Predict sequence of 50 steps before shifting prediction run forward by 50 steps
    prediction_seqs = []
    for i in xrange(len(data)/prediction_len):
        curr_frame = data[i*prediction_len]
        predicted = []
        for j in xrange(prediction_len):
            predicted.append(model.predict(curr_frame[newaxis,:,:])[0,0])
            curr_frame = curr_frame[1:]
            curr_frame = np.insert(curr_frame, [window_size-1], predicted[-1], axis=0)
        prediction_seqs.append(predicted)
return prediction_seqs

In R, I wrote it like this:

predict_sequences_multiple <- function(model, data, window_size, prediction_len){
  #Predict sequence of 50 steps before shifting prediction run forward by 50 steps
  prediction_seqs = list()
  for (i in 1:as.integer(nrow(data)/prediction_len)){
    curr_frame = array(data[i*prediction_len,,], dim = c(prediction_len,1,1))
    predicted = list()
    for (j in 1:prediction_len){
      predicted[[j]] <- model$predict(curr_frame)[1]
      curr_frame <- curr_frame[2:nrow(curr_frame)]
      curr_frame <- array(c(curr_frame, predicted[[j]]), dim = c(prediction_len,1,1))
    }
    prediction_seqs[[i]] <- unlist(as.numeric(predicted))
  }
  return(prediction_seqs)
}
predictions <- predict_sequences_multiple(model, x_test, 50, 50)

but I get very different predicted values. I am confused about predict function (method) in R. I can find 4 predict functions: predict_on_batch, predict_proba, predict_classes and predict generator, but if I use predict only, it says this function is available only in standard stat package. For example, Siraj is using model.predict in his python code. How to transform that in R code?

jjallaire commented 6 years ago

In R you write it this way:

predict(model, curr_frame)

The fact that model is a Keras model will cause the right S3 function to be dispatched to (the predict inside the Keras package).

As for the rest of the code and why you might be getting different predictions it's hard for me to debug without wrapping my head all the way around the example and the way you've translated Python to R.

MislavSag commented 6 years ago

But if I write keras::pred and click tab to find all available functions there is no prediction function. Also if I type ? predict there is no predict function from keras package, only from stats package. I thing I actually use stat::predict() when I write a code predict(model, curr_frame). Form example if I use keras::peredict it returns an error: Error: 'predict' is not an exported object from 'namespace:keras'

jjallaire commented 6 years ago

It's definitely exported: https://github.com/rstudio/keras/blob/master/NAMESPACE#L7

I don't know why you are getting that error, perhaps an older version of keras?

Try running the example on the https://keras.rstudio.com home page and substituting the predict_classes call with:

predict(model, x_test)

Everything works as expected.

MislavSag commented 6 years ago

I have tried to install keras again (in R and python (conda)). I have also tried example you linked. But again, if I try model %>% keras::predict(x_test) it doesn't work. Returns the same error: Error: 'predict' is not an exported object from 'namespace:keras' Can I use predict_on_batch function?

jjallaire commented 6 years ago

I know for sure that the predict function is exported and works correctly (otherwise I'd probably have hundreds of bug reports here!) so I'm not sure what else is going on in your environment to confound things.

jjallaire commented 6 years ago

You can certainly try using predict_on_batch()

jjallaire commented 6 years ago

You have to write it this way:

model %>% predict(x_test)

Rather than this way:

model %>% keras::predict(x_test)
MislavSag commented 6 years ago

I have tried with predict_on_batch and it seems it is working fine.

Can be closed.

Thanks jjallaire

jjallaire commented 6 years ago

Thanks, glad that is working!

MislavSag commented 6 years ago

I know tihs is closed, but I am affraid I didn't solve this issue. If I use: y_predict <- model %>% predict(X_test) i got an error:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "keras_training_history"

It seems like predict is not exported?

Am I the only one with this error? OR: y_predict <- model %>% keras::predict(X_test) gives error: Error: 'predict' is not an exported object from 'namespace:keras'

I have tried to reinstall and update keras

jjallaire commented 6 years ago

You error indicates that you are passing a training history object rather than a model to the predict function. Perhaps you assigned the results of fit() back to the model variable? I can assure you that the predict function is exported and that this is the first time I have ever seen this error condition (again, it's likely due to not passing an actual model to predict()

MislavSag commented 6 years ago

You were right. I was assigning model to model: model <- model %>% .... It's working now