rstudio / tensorflow.rstudio.com

https://tensorflow.rstudio.com
9 stars 12 forks source link

Beginner tutorial does not run on Ventura 13.1 M1 #47

Open pedro-alonsod opened 1 year ago

pedro-alonsod commented 1 year ago

Hi,

I am trying my hand at the beginner tutorial after successfully installing TensorFlow on Rstudio but trying the code I get the following error: Tuto - Beginner.

Error:

`Traceback (most recent call last): File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 52, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.NotFoundError: Graph execution error:

Detected at node 'StatefulPartitionedCall_2' defined at (most recent call last): File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/engine/training.py", line 1650, in fit tmp_logs = self.train_function(iterator) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/engine/training.py", line 1249, in train_function return step_function(self, iterator) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/engine/training.py", line 1233, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/engine/training.py", line 1222, in run_step outputs = model.train_step(data) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/engine/training.py", line 1027, in train_step self.optimizer.minimize(loss, self.trainable_variables, tape=tape) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 527, in minimize self.apply_gradients(grads_and_vars) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1140, in apply_gradients return super().apply_gradients(grads_and_vars, name=name) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 634, in apply_gradients iteration = self._internal_apply_gradients(grads_and_vars) File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1166, in _internal_apply_gradients return tf.internal.distribute.interim.maybe_merge_call( File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1216, in _distributed_apply_gradients_fn distribution.extended.update( File "/Users/pedroalonso/opt/anaconda3/envs/thesis/lib/python3.10/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_2' could not find registered platform with id: 0x17f5d0f40 [[{{node StatefulPartitionedCall_2}}]] [Op:__inference_train_function_2604]`

Any help? Thanks.

t-kalinowski commented 1 year ago

Welcome!

I can't reproduce the error on Monterey (macOS 12.6), The beginner quickstart guide renders without error.

I'll update macOS and see if I can reproduce the issue.

I'm not sure if it's related, but there are a handful of TensorFlow operations that fail on M1 Macs with tensors that are placed on the GPU. The fix (until the appropriate M1 GPU kernel is available), is to pin the operation to the CPU.

e.g., from the "Apple Silicon" section in ?install_tensorflow():

Tensorflow on Apple Silicon is not officially supported by the Tensorflow maintainers. However Apple has published a custom version of Tensorflow compatible with Arm Macs. install_tensorflow() will install the special packages tensorflow-macos and tensorflow-metal on Arm Macs. See https://developer.apple.com/metal/tensorflow-plugin/ for instructions on how to do the equivalent manually. Please note that this is an experimental build of both Python and Tensorflow, with known issues. In particular, certain operations will cause errors, but can often be remedied by pinning them to the CPU. For example:

  x <- array(runif(64*64), c(1, 64, 64))
  keras::layer_random_rotation(x, .5)  # Error:
  # No registered 'RngReadAndSkip' OpKernel for 'GPU' devices
  # Pin the operation to the CPU to avoid the error
  with(tf$device("CPU"), keras::layer_random_rotation(x, .5) ) # No Error
pedro-alonsod commented 1 year ago

Hi,

I had the same problem, I think, with python only; the fix I found was to do this:

model.compile(optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=1e-3), loss=loss_fn, metrics=["accuracy"])

It may be the same in Rstudio. But I'm not sure how to use the optimizer adam from the legacy in RStudio.

You could try that and tell me please?

Thanks!

EDIT: I forgot to say that I use anaconda not venv.

And my env:

Name Version Build Channel absl-py 1.4.0 pypi_0 pypi anyio 3.6.2 pyhd8ed1ab_0 conda-forge appdirs 1.4.4 pyh9f0ad1d_0 conda-forge appnope 0.1.3 pyhd8ed1ab_0 conda-forge argon2-cffi 21.3.0 pyhd8ed1ab_0 conda-forge argon2-cffi-bindings 21.2.0 py310h8e9501a_3 conda-forge asttokens 2.2.1 pyhd8ed1ab_0 conda-forge astunparse 1.6.3 pypi_0 pypi atk-1.0 2.38.0 hcb7b3dd_1 conda-forge attrs 22.2.0 pyh71513ae_0 conda-forge backcall 0.2.0 pyh9f0ad1d_0 conda-forge backports 1.0 pyhd8ed1ab_3 conda-forge backports.functools_lru_cache 1.6.4 pyhd8ed1ab_0 conda-forge beautifulsoup4 4.11.2 pyha770c72_0 conda-forge bleach 6.0.0 pyhd8ed1ab_0 conda-forge brotlipy 0.7.0 py310h8e9501a_1005 conda-forge bzip2 1.0.8 h3422bc3_4 conda-forge c-ares 1.18.1 h3422bc3_0 conda-forge ca-certificates 2022.12.7 h4653dfc_0 conda-forge cached-property 1.5.2 hd8ed1ab_1 conda-forge cached_property 1.5.2 pyha770c72_1 conda-forge cachetools 5.3.0 pypi_0 pypi cairo 1.16.0 h73a0509_1014 conda-forge certifi 2022.12.7 pypi_0 pypi cffi 1.15.1 py310h2399d43_3 conda-forge charset-normalizer 3.0.1 pypi_0 pypi click 8.1.3 pypi_0 pypi colorama 0.4.6 pyhd8ed1ab_0 conda-forge comm 0.1.2 pyhd8ed1ab_0 conda-forge contourpy 1.0.7 pypi_0 pypi cryptography 39.0.0 py310hfc83b78_0 conda-forge cycler 0.11.0 pypi_0 pypi debugpy 1.6.6 py310h0f1eb42_0 conda-forge decorator 5.1.1 pyhd8ed1ab_0 conda-forge defusedxml 0.7.1 pyhd8ed1ab_0 conda-forge dill 0.3.6 pypi_0 pypi dm-tree 0.1.8 pypi_0 pypi entrypoints 0.4 pyhd8ed1ab_0 conda-forge etils 1.0.0 pypi_0 pypi executing 1.2.0 pyhd8ed1ab_0 conda-forge expat 2.5.0 hb7217d7_0 conda-forge flatbuffers 23.1.21 pypi_0 pypi flit-core 3.8.0 pyhd8ed1ab_0 conda-forge font-ttf-dejavu-sans-mono 2.37 hab24e00_0 conda-forge font-ttf-inconsolata 3.000 h77eed37_0 conda-forge font-ttf-source-code-pro 2.038 h77eed37_0 conda-forge font-ttf-ubuntu 0.83 hab24e00_0 conda-forge fontconfig 2.14.2 h82840c6_0 conda-forge fonts-conda-ecosystem 1 0 conda-forge fonts-conda-forge 1 0 conda-forge fonttools 4.38.0 pypi_0 pypi freetype 2.12.1 hd633e50_1 conda-forge fribidi 1.0.10 h27ca646_0 conda-forge gast 0.4.0 pypi_0 pypi gdk-pixbuf 2.42.10 h9bcf4fe_0 conda-forge gettext 0.21.1 h0186832_0 conda-forge giflib 5.2.1 h27ca646_2 conda-forge google-auth 2.16.0 pypi_0 pypi google-auth-oauthlib 0.4.6 pypi_0 pypi google-pasta 0.2.0 pypi_0 pypi googleapis-common-protos 1.58.0 pypi_0 pypi graphite2 1.3.13 h9f76cd9_1001 conda-forge graphviz 7.1.0 h4f8fbd6_0 conda-forge grpcio 1.46.3 py310ha26ec5d_0 conda-forge gtk2 2.24.33 h57013de_2 conda-forge gts 0.7.6 h4b6d4d6_2 conda-forge h5py 3.6.0 nompi_py310hb8bbf05_100 conda-forge harfbuzz 6.0.0 hddbc195_0 conda-forge hdf5 1.12.1 nompi_hd9dbc9e_104 conda-forge icu 70.1 h6b3803e_0 conda-forge idna 3.4 pyhd8ed1ab_0 conda-forge importlib-metadata 6.0.0 pyha770c72_0 conda-forge importlib_metadata 6.0.0 hd8ed1ab_0 conda-forge importlib_resources 5.10.2 pyhd8ed1ab_0 conda-forge ipykernel 6.21.1 pyh736e0ef_0 conda-forge ipython 8.9.0 pyhd1c38e8_0 conda-forge ipython_genutils 0.2.0 py_1 conda-forge jedi 0.18.2 pyhd8ed1ab_0 conda-forge jinja2 3.1.2 pyhd8ed1ab_1 conda-forge joblib 1.2.0 pypi_0 pypi jpeg 9e h1a8c8d9_3 conda-forge jsonschema 4.17.3 pyhd8ed1ab_0 conda-forge jupyter_client 8.0.2 pyhd8ed1ab_0 conda-forge jupyter_core 5.2.0 py310hbe9552e_0 conda-forge jupyter_events 0.6.3 pyhd8ed1ab_0 conda-forge jupyter_server 2.2.1 pyhd8ed1ab_0 conda-forge jupyter_server_terminals 0.4.4 pyhd8ed1ab_1 conda-forge jupyterlab_pygments 0.2.2 pyhd8ed1ab_0 conda-forge keras 2.11.0 pypi_0 pypi keras-preprocessing 1.1.2 pyhd8ed1ab_0 conda-forge kiwisolver 1.4.4 pypi_0 pypi krb5 1.20.1 h69eda48_0 conda-forge lcms2 2.14 h481adae_1 conda-forge lerc 4.0.0 h9a09cb3_0 conda-forge libblas 3.9.0 16_osxarm64_openblas conda-forge libcblas 3.9.0 16_osxarm64_openblas conda-forge libclang 15.0.6.1 pypi_0 pypi libcurl 7.87.0 h9049daf_0 conda-forge libcxx 14.0.6 h2692d47_0 conda-forge libdeflate 1.17 h1a8c8d9_0 conda-forge libedit 3.1.20191231 hc8eb9b7_2 conda-forge libev 4.33 h642e427_1 conda-forge libffi 3.4.2 h3422bc3_5 conda-forge libgd 2.3.3 h90fb8ed_4 conda-forge libgfortran 5.0.0 11_3_0_hd922786_27 conda-forge libgfortran5 11.3.0 hdaf2cc0_27 conda-forge libglib 2.74.1 h4646484_1 conda-forge libiconv 1.17 he4db4b2_0 conda-forge liblapack 3.9.0 16_osxarm64_openblas conda-forge libnghttp2 1.51.0 hae82a92_0 conda-forge libopenblas 0.3.21 openmp_hc731615_3 conda-forge libpng 1.6.39 h76d750c_0 conda-forge libprotobuf 3.19.4 hccf11d3_0 conda-forge librsvg 2.54.4 ha2634a2_0 conda-forge libsodium 1.0.18 h27ca646_1 conda-forge libsqlite 3.40.0 h76d750c_0 conda-forge libssh2 1.10.0 h7a5bd25_3 conda-forge libtiff 4.5.0 h5dffbdd_2 conda-forge libtool 2.4.7 hb7217d7_0 conda-forge libwebp 1.2.4 h999c80f_1 conda-forge libwebp-base 1.2.4 h57fd34a_0 conda-forge libxcb 1.13 h9b22ae9_1004 conda-forge libxml2 2.10.3 h87b0503_0 conda-forge libzlib 1.2.13 h03a7124_4 conda-forge llvm-openmp 15.0.7 h7cfbb63_0 conda-forge markdown 3.4.1 pypi_0 pypi markupsafe 2.1.2 py310h8e9501a_0 conda-forge matplotlib 3.6.3 pypi_0 pypi matplotlib-inline 0.1.6 pyhd8ed1ab_0 conda-forge mistune 2.0.4 pyhd8ed1ab_0 conda-forge nbclassic 0.5.1 pyhd8ed1ab_0 conda-forge nbclient 0.7.2 pyhd8ed1ab_0 conda-forge nbconvert 7.2.9 pyhd8ed1ab_0 conda-forge nbconvert-core 7.2.9 pyhd8ed1ab_0 conda-forge nbconvert-pandoc 7.2.9 pyhd8ed1ab_0 conda-forge nbformat 5.7.3 pyhd8ed1ab_0 conda-forge ncurses 6.3 h07bb92c_1 conda-forge nest-asyncio 1.5.6 pyhd8ed1ab_0 conda-forge notebook 6.5.2 pyha770c72_1 conda-forge notebook-shim 0.2.2 pyhd8ed1ab_0 conda-forge numpy 1.24.1 pypi_0 pypi oauthlib 3.2.2 pypi_0 pypi openjpeg 2.5.0 hbc2ba62_2 conda-forge openssl 3.0.8 h03a7124_0 conda-forge opt-einsum 3.3.0 pypi_0 pypi packaging 23.0 pyhd8ed1ab_0 conda-forge pandas 1.5.3 pypi_0 pypi pandoc 2.19.2 hce30654_1 conda-forge pandocfilters 1.5.0 pyhd8ed1ab_0 conda-forge pango 1.50.12 h6c112b8_1 conda-forge parso 0.8.3 pyhd8ed1ab_0 conda-forge pcre2 10.40 hb34f9b4_0 conda-forge pexpect 4.8.0 pyh1a96a4e_2 conda-forge pickleshare 0.7.5 py_1003 conda-forge pillow 9.4.0 py310h5a7539a_1 conda-forge pip 23.0 pyhd8ed1ab_0 conda-forge pixman 0.40.0 h27ca646_0 conda-forge pkgutil-resolve-name 1.3.10 pyhd8ed1ab_0 conda-forge platformdirs 2.6.2 pyhd8ed1ab_0 conda-forge plotly 5.13.0 pypi_0 pypi pooch 1.6.0 pyhd8ed1ab_0 conda-forge prometheus_client 0.16.0 pyhd8ed1ab_0 conda-forge promise 2.3 pypi_0 pypi prompt-toolkit 3.0.36 pyha770c72_0 conda-forge protobuf 3.20.3 pypi_0 pypi psutil 5.9.4 py310h8e9501a_0 conda-forge pthread-stubs 0.4 h27ca646_1001 conda-forge ptyprocess 0.7.0 pyhd3deb0d_0 conda-forge pure_eval 0.2.2 pyhd8ed1ab_0 conda-forge pyasn1 0.4.8 pypi_0 pypi pyasn1-modules 0.2.8 pypi_0 pypi pycparser 2.21 pyhd8ed1ab_0 conda-forge pydot 1.4.2 py310hbe9552e_3 conda-forge pygments 2.14.0 pyhd8ed1ab_0 conda-forge pyopenssl 23.0.0 pyhd8ed1ab_0 conda-forge pyparsing 3.0.9 pyhd8ed1ab_0 conda-forge pyrsistent 0.19.3 py310h8e9501a_0 conda-forge pysocks 1.7.1 pyha2e5f31_6 conda-forge python 3.10.9 h3ba56d0_0_cpython conda-forge python-dateutil 2.8.2 pyhd8ed1ab_0 conda-forge python-fastjsonschema 2.16.2 pyhd8ed1ab_0 conda-forge python-json-logger 2.0.4 pyhd8ed1ab_0 conda-forge python_abi 3.10 3_cp310 conda-forge pytz 2022.7.1 pyhd8ed1ab_0 conda-forge pyyaml 6.0 py310h8e9501a_5 conda-forge pyzmq 25.0.0 py310hc407298_0 conda-forge readline 8.1.2 h46ed386_0 conda-forge requests 2.28.2 pyhd8ed1ab_0 conda-forge requests-oauthlib 1.3.1 pypi_0 pypi rfc3339-validator 0.1.4 pyhd8ed1ab_0 conda-forge rfc3986-validator 0.1.1 pyh9f0ad1d_0 conda-forge rsa 4.9 pypi_0 pypi scikit-learn 1.2.1 pypi_0 pypi scipy 1.10.0 pypi_0 pypi send2trash 1.8.0 pyhd8ed1ab_0 conda-forge setuptools 67.1.0 pyhd8ed1ab_0 conda-forge six 1.15.0 pypi_0 pypi sniffio 1.3.0 pyhd8ed1ab_0 conda-forge soupsieve 2.3.2.post1 pyhd8ed1ab_0 conda-forge stack_data 0.6.2 pyhd8ed1ab_0 conda-forge tenacity 8.1.0 pypi_0 pypi tensorboard 2.11.2 pypi_0 pypi tensorboard-data-server 0.6.1 pypi_0 pypi tensorboard-plugin-wit 1.8.1 pypi_0 pypi tensorflow-datasets 4.8.2 pypi_0 pypi tensorflow-deps 2.10.0 0 apple tensorflow-estimator 2.11.0 pypi_0 pypi tensorflow-hub 0.12.0 pypi_0 pypi tensorflow-macos 2.11.0 pypi_0 pypi tensorflow-metadata 1.12.0 pypi_0 pypi tensorflow-metal 0.7.1 pypi_0 pypi termcolor 2.2.0 pypi_0 pypi terminado 0.17.1 pyhd1c38e8_0 conda-forge threadpoolctl 3.1.0 pypi_0 pypi tinycss2 1.2.1 pyhd8ed1ab_0 conda-forge tk 8.6.12 he1e0b03_0 conda-forge toml 0.10.2 pypi_0 pypi tornado 6.2 py310h8e9501a_1 conda-forge tqdm 4.64.1 pyhd8ed1ab_0 conda-forge traitlets 5.9.0 pyhd8ed1ab_0 conda-forge typing-extensions 4.4.0 hd8ed1ab_0 conda-forge typing_extensions 4.4.0 pyha770c72_0 conda-forge tzdata 2022g h191b570_0 conda-forge urllib3 1.26.14 pyhd8ed1ab_0 conda-forge wcwidth 0.2.6 pyhd8ed1ab_0 conda-forge webencodings 0.5.1 py_1 conda-forge websocket-client 1.5.0 pyhd8ed1ab_0 conda-forge werkzeug 2.2.2 pypi_0 pypi wheel 0.38.4 pyhd8ed1ab_0 conda-forge wrapt 1.14.1 pypi_0 pypi xorg-libxau 1.0.9 h27ca646_0 conda-forge xorg-libxdmcp 1.1.3 h27ca646_0 conda-forge xz 5.2.6 h57fd34a_0 conda-forge yaml 0.2.5 h3422bc3_2 conda-forge zeromq 4.3.4 hbdafb3b_1 conda-forge zipp 3.12.0 pyhd8ed1ab_0 conda-forge zlib 1.2.13 h03a7124_4 conda-forge zstd 1.5.2 hf913c23_6 conda-forge

t-kalinowski commented 1 year ago

If in python you encounter something like this:

tf.keras.optimizers.legacy.Adam(learning_rate=1e-3)

and you want to translate it to R, you can for the most part just replace . with $

library(tensorflow)
tf$keras$optimizers$legacy$Adam(learning_rate=1e-3)

(Note in R, the tf symbol comes from the tensorflow package.)

pedro-alonsod commented 1 year ago

Yes! That did it also for R! Thank you so much! I guess the issues with tensorflow in M1 should be similar to those with the newest packages.

To any one who experience this problem with the newest tensorflow-macos package, here is the link where I got the python fix: https://developer.apple.com/forums/thread/721619

The beginner tutorial looks like this for me:

library(tensorflow)

library(keras)

c(c(x_train, y_train), c(x_test, y_test)) %<-% keras::dataset_mnist()

x_train <- x_train / 255

x_test <- x_test / 255

model <- keras_model_sequential(input_shape = c(28, 28)) %>% layer_flatten() %>% layer_dense(128, activation = "relu") %>% layer_dropout(0.2) %>% layer_dense(10)

predictions <- predict(model, x_train[1:2, , ])

predictions

tf$nn$softmax(predictions)

loss_fn <- loss_sparse_categorical_crossentropy(from_logits = TRUE)

loss_fn(y_train[1:2], predictions)

model %>% compile( optimizer = tf$keras$optimizers$legacy$Adam(learning_rate=1e-3), loss = loss_fn, metrics = "accuracy" )

model %>% fit(x_train, y_train, epochs = 5)

Thanks!