rstudio / tensorflow

TensorFlow for R
https://tensorflow.rstudio.com
Apache License 2.0
1.33k stars 318 forks source link

Installation Issue #562

Closed weixiaokuan11 closed 1 year ago

weixiaokuan11 commented 1 year ago

I am using MacOS M2 build. By using the approaches provided in this thread. I can install tensorflow and it seems to be working but it(keras) cannot be used to fit the model. The codes and errors are as the below. I have been researching this for a while and cannot find a solution. Can someone share some insights into this issue? Thank you. -Xiaokuan

library(keras)

mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y

x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))

x_train <- x_train / 255
x_test <- x_test / 255

y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)

model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% 
  layer_dropout(rate = 0.4) %>% 
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 10, activation = 'softmax')

summary(model)

model %>% compile(
  loss = 'categorical_crossentropy',
  optimizer = optimizer_rmsprop(),
  metrics = c('accuracy')
)

history <- model %>% fit(
  x_train, y_train, 
  epochs = 30, batch_size = 128, 
  validation_split = 0.2
)

tensorflow::as_tensor("Hello World")

2023-03-28 18:37:13.766399: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz Epoch 1/30 2023-03-28 18:37:13.947807: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. 2023-03-28 18:37:14.073945: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 2023-03-28 18:37:14.073973: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 2023-03-28 18:37:14.088151: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 2023-03-28 18:37:14.089599: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 2023-03-28 18:37:14.098454: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 2023-03-28 18:37:14.098472: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x280147c70 Error in py_call_impl(callable, dots$args, dots$keywords) : tensorflow.python.framework.errors_impl.NotFoundError: Graph execution error:

<... omitted ...>on3.8/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1166, in _internal_apply_gradients return tf.internal.distribute.interim.maybe_merge_call( File "/Users/roger/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1216, in _distributed_apply_gradients_fn distribution.extended.update( File "/Users/roger/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1211, in apply_grad_to_update_var return self._update_step_xla(grad, var, id(self._var_key(var))) Node: 'StatefulPartitionedCall_4' could not find registered platform with id: 0x280147c70 [[{{node StatefulPartitionedCall_4}}]] [Op:__inference_train_function_854] See reticulate::py_last_error() for details

tensorflow::as_tensor("Hello World") tf.Tensor(b'Hello World', shape=(), dtype=string)

t-kalinowski commented 1 year ago

This has been fixed with TF 2.13 (due to be on CRAN in about a week).

This should be all that's needed to get everything working on an M2 Mac now:

remotes::install_github("rstudio/tensorflow")
reticulate::install_python()
tensorflow::install_tensorflow()