keras atomic vector error on Boston Housing Example

Open
#422 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
keras, r, tensorflow

Research direction

Reproduce the Boston Housing example using the reported R 4.0.0, keras 2.3.0.0, TensorFlow 2.2.0, Windows, and CUDA details. Start by checking whether the failure occurs during model evaluation or TensorFlow GPU initialization, then verify behavior without GPU support. Done means the cause is identified and the supported CPU fallback or compatibility requirement is clear.

Written by the indexing model from the issue text.

Description

Summary
Context: Running Boston housing Code from Deep Learning with R Book. Issue: Code fails because something internal is not recursive but the R code expects it to be recursive producing:
2020-05-31 12:39:37.602972: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
Error: $ operator is invalid for atomic vectors

Code Detail

> dataset <- dataset_boston_housing()
2020-05-31 12:38:08.129130: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
> c(c(train_data, train_targets), c(test_data, test_targets)) %<-% dataset
> str(train_data)
 num [1:404, 1:13] 1.2325 0.0218 4.8982 0.0396 3.6931 ...
> str(test_data)
 num [1:102, 1:13] 18.0846 0.1233 0.055 1.2735 0.0715 ...
> str(train_targets)
 num [1:404(1d)] 15.2 42.3 50 21.1 17.7 18.5 11.3 15.6 15.6 14.4 ...
> mean <- apply(train_data, 2, mean)
> std <- apply(train_data, 2, sd)
> train_data <- scale(train_data, center = mean, scale = std)
> test_data <- scale(test_data, center = mean, scale = std)
> # Because we will need to instantiate the same model multiple times,
> # we use a function to construct it.
> build_model <- function() {
+   model <- keras_model_sequential() %>% 
+     layer_dense(units = 64, activation = "relu", 
+                 input_shape = dim(train_data)[[2]]) %>% 
+     layer_dense(units = 64, activation = "relu") %>% 
+     layer_dense(units = 1) 
+     
+   model %>% compile(
+     optimizer = "rmsprop", 
+     loss = "mse", 
+     metrics = c("mae")
+   )
+ }
> k <- 4
> indices <- sample(1:nrow(train_data))
> folds <- cut(indices, breaks = k, labels = FALSE)
> 
> num_epochs <- 100
> all_scores <- c()
> for (i in 1:k) {
+   cat("processing fold #", i, "\n")
+   # Prepare the validation data: data from partition # k
+   val_indices <- which(folds == i, arr.ind = TRUE) 
+   val_data <- train_data[val_indices,]
+   val_targets <- train_targets[val_indices]
+   
+   # Prepare the training data: data from all other partitions
+   partial_train_data <- train_data[-val_indices,]
+   partial_train_targets <- train_targets[-val_indices]
+   
+   # Build the Keras model (already compiled)
+   model <- build_model()
+   
+   # Train the model (in silent mode, verbose=0)
+   model %>% fit(partial_train_data, partial_train_targets,
+                 epochs = num_epochs, batch_size = 1, verbose = 0)
+                 
+   # Evaluate the model on the validation data
+   results <- model %>% evaluate(val_data, val_targets, verbose = 0)
+   all_scores <- c(all_scores, results$mean_absolute_error)
+ }  
processing fold # 1 
2020-05-31 12:39:36.574624: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-05-31 12:39:37.522676: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 780M computeCapability: 3.0
coreClock: 0.797GHz coreCount: 8 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 149.01GiB/s
2020-05-31 12:39:37.523031: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-05-31 12:39:37.528538: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-05-31 12:39:37.534908: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-05-31 12:39:37.536716: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-05-31 12:39:37.543800: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-05-31 12:39:37.546785: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-05-31 12:39:37.560846: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-05-31 12:39:37.561866: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1657] Ignoring visible gpu device (device: 0, name: GeForce GTX 780M, pci bus id: 0000:01:00.0, compute capability: 3.0) with Cuda compute capability 3.0. The minimum required Cuda capability is 3.5.
2020-05-31 12:39:37.587933: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-05-31 12:39:37.602014: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1cedd161a40 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-31 12:39:37.602433: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-05-31 12:39:37.602771: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-31 12:39:37.602972: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      
Error: $ operator is invalid for atomic vectors

Perhaps this is related to the CUDA capability of my graphics card, but the process is not terminated when it notes this and is therefore not identifiable or solvable.

Note: It's not a latest and greatest graphics card, but its not totally incapable. It is indeed a shame if it can't help my CPU solve even these intro problems. :-( On the other hand, if the graphics card is not up to the task, it would be nice if it dropped back to a CPU equivalent step.

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19635)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] keras_2.3.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6     lattice_0.20-41  zeallot_0.1.0    rappdirs_0.3.1   grid_4.0.0      
 [6] R6_2.4.1         jsonlite_1.6.1   magrittr_1.5     tfruns_1.4       whisker_0.4     
[11] Matrix_1.2-18    reticulate_1.16  generics_0.0.2   tools_4.0.0      xfun_0.14       
[16] yaml_2.2.1       compiler_4.0.0   base64enc_0.1-3  tensorflow_2.2.0 knitr_1.28  
Dominant language
R
Stars
1.3k
Forks
316
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/tensorflow

All issues in rstudio/tensorflow

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.