Search code examples
rkerasreticulate

Error when trying to build a neural network


When I try to fit the training sets on the model, I get the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : Evaluation error: Required version of NumPy not available: installation of Numpy
>= 1.6 not found.

I tried to import the numpy module but I still get the error:

library(reticulate)
np <- import('numpy')

This is the code:

nn_model = keras_model_sequential()
enter cnn_model %>% layer_dense(units = 20, activation = 'relu', kernel_initializer = 
                       initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 1), input_shape = ncol(xTrain)
                     ) %>%
         layer_dense(units = 5, activation = 'softmax', kernel_initializer = initializer_random_uniform(minval = -0.05,
                                                                                                        maxval = 0.05,
                                                                                                        seed = 1))
nn_model %>% compile(loss = 'categorical_crossentropy', optimizer = optimizer_sgd(lr = 0.1),
                 metrics = c('categorical_accuracy'))
nn_model %>% fit(xTrain, yTrain, epochs = 100, batch_size = 256, validation_split = 0.5)
nn_model %>% evaluate(xTest, yTest)

Solution

  • Unfortunately, it seems that the np <- import('numpy') command will not produce any error if Numpy is not present in your system; try the following to install it from R:

    library(reticulate)
    py_install("numpy")
    np <- import('numpy')