Search code examples
rtensorflowkerasneural-networkpredict

Predict() applied to a neural network model does not work


currently Im performing a statistical learning analysis on a given dataset (redwine quality); the goal is to predict the quality value. I already tried the logistic regression and the random forest but the accuracy is bad. Now with the neural network the accuracy is 99%, and I have to apply the model used to the test dataset with the function predict(). I tried this, but not works. This is the neural network model:

>  modnn=keras_model_sequential() 
    >     modnn %>%
    >      layer_dense(units=7,activation="relu",input_shape=c(11))%>%
    >       layer_dropout(0.3) %>%  
    >       layer_batch_normalization()%>%   
    >       layer_dense(units=7,activation="relu")%>%
    >       layer_dropout(0.3)%>%
    >       layer_batch_normalization()%>% 
    >       layer_dense(units=7,activation="relu")%>%
    >       layer_dropout(0.3)%>%
    >       layer_batch_normalization()%>%
    >       layer_dense(units=1)

This is the model compilation:

modnn %>% compile(loss="binary_crossentropy",optimizer="adam",metrics=c("accuracy"))

This is what I tried:

predict(modnn,newdata=dataset_test)

Error in the predict:

Error in is_tensorflow_dataset(x) :  argument "x" is missing, with no default

Edit1, I tried to remove the attribute from the predict() function in dataset_test:

predict(modnn,dataset_test)

This is the new error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: in user code:
<... omitted ...>late/lib/python3.8/site-packages/keras/engine/training.py", line 1791, in predict_step
return self(x, training=False)
File "/Users/turex/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/turex/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/keras/engine/input_spec.py", line 183, in assert_input_compatibility
raise ValueError(f'Missing data for input "{name}". '

ValueError: Missing data for input "dense_25_input". You passed a data dictionary with keys ['fixed.acidity', 'volatile.acidity', 'citric.acid', 'residual.sugar', 'chlorides', 'free.sulfur.dioxide', 'total.sulfur.dioxide', 'density', 'pH', 'sulphates', 'alcohol', 'id_number']. Expected the following keys: ['dense_25_input']

See `reticulate::py_last_error()` for details

Solution

  • I think that I solved the error.What I did is this: I just define a variable that is the scale of dataset_test

    xtest<-as.matrix(scale(dataset_test[1:11]))
    

    After this, I computed the function predict in this way:

    predict(modnn,xtest)
    

    Everything is work, the last thing that I notice is that the predictions are always different when I tried to compute the code of the neural network structure and then of the prediction. So I put a set_random_seed(), and with this works and the prediction are the same every time. Thx