Closed yuriy-babenko closed 1 month ago
The above code executed without any errors for me. Can you tell me more about your system (OS, package versions, python version, etc.)
That said, I have seen similar errors before. At some point tensorflow became more strict about the shape of training data. In older versions, in fit()
it used to implicitly promote 1d arrays with shape(n)
to 2d arrays of shape(n, 1)
. But then stopped doing that in favor of requiring users to be more explicit.
You can likely fix the error by adding a 1 dim to the input y
: dim(y) <- c(dim(y), 1)
> reticulate::py_config() version: 3.11.9 (main, Aug 7 2024, 13:09:04) [Clang 15.0.0 (clang-1500.3.9.4)]
> sessionInfo() R version 4.4.1 (2024-06-14) Platform: aarch64-apple-darwin20 Running under: macOS Sonoma 14.5
> tensorflow::tf_config() TensorFlow v2.17.0 (~/.virtualenvs/r-keras/lib/python3.11/site-packages/keras) Python v3.11 (~/.virtualenvs/r-keras/bin/python)
y: dim(y) <- c(dim(y), 1)
with that you mean partial_y_train or which y?
Yes, I mean partial_y_train
passed to fit()
must be a 2d array with shape (n, 1)
.
But, still, I can't reproduce the error locally with the same OS, Python version, and latest packages.
1) tried changing the dimensions of _partial_ytrain but hitting Error in py_call_impl(callable, call_args$unnamed, call_args$named) :ValueError: Cannot take the length of shape with unknown rank.
dim(partial_y_train) <- c(dim(partial_y_train), 1)
2) I re-ran the notebook from scratch today and do not observe the error any longer either. I did not have to change anything in the code. This is a bit weird but maybe some local dependency on my machine? I am not sure ...
You were likely working interactively and had accumulated state. My best guess is that at some point you converted partial_y_train
from an R array to a tensor, and then dim<-
doesn't work on that.
Also, it's probably a bad habit to use dim<-
, array_reshape()
, is better/safer when working with an R array that will be passed to NumPy/TensorFlow/Jax/PyTorch, or op_reshape()
when you already have a tensor.
(If you have a GPU, be careful not to convert large datasets to TF tensors prematurely, because they will be allocated on the GPU and then there won't be enough memory for the model. It's best to pass an R array, NumPy array, or tfdataset to fit()
.
Thank you @t-kalinowski. Appreciate your detailed advise and kind help. Many great insights (in addition to the brilliant book).
Looking at newswire example https://github.com/t-kalinowski/deep-learning-with-R-2nd-edition-code/blame/5d666f93d52446511a8a8e4eb739eba1c0ffd199/ch04.R#L221
Running code:
Specifically, at training the model step, hitting:
Which sounds like an issue here https://github.com/rstudio/keras3/issues/1063, yet in their code their used:
val_indices <- 1:10000
instead of 1000.