rtensorflowautoencoder

R Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'placeholder'


I am trying to Implement Auto Encoder Dimension Reduction from Tensorflow in R, in this example:

library(dimRed)
library(tensorflow)
fraud_data <- read.csv("fraud_data")
data_label <- fraud_data["class"]
my_formula <- as.formula("class ~ .")
dat <- as.dimRedData(my_formula, fraud_data)
dimen <- NULL
dimension_params <- NULL
dimen <- dimRed::AutoEncoder()
dimension_params <- dimen@stdpars
dimension_params$ndim <- 2
emb <- dimen@fun(fraud_data, dimension_params) 
dimensional_data <- data.frame(emb@data@data)
x11()
plot(x=dimensional_data[,1], y=dimensional_data[,2], col=data_label, main="Laplacian Eigenmaps Projection")
legend(x=legend_pos, legend = unique(data_label), col=unique(data_label), pch=1)

I keep getting AttributeError module 'tensorflow' has no attribute 'placeholder'" as stated in this traceback:

14. stop(structure(list(message = "AttributeError: module 'tensorflow' has no attribute 'placeholder'", 
    call = py_get_attr_impl(x, name, silent), cppstack = NULL), class = c("Rcpp::exception", 
"C++Error", "error", "condition"))) 
13. py_get_attr_impl(x, name, silent) 
12. py_get_attr(x, name) 
11. py_get_attr_or_item(x, name, TRUE) 
10. `$.python.builtin.object`(x, name) 
9. `$.python.builtin.module`(tf, "placeholder") 
8. tf$placeholder 
7. graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
    weight_decay = weight_decay, learning_rate = learning_rate, 
    n_steps = n_steps, ndim = ndim) 
6. eval(substitute(expr), data, enclos = parent.frame()) 
5. eval(substitute(expr), data, enclos = parent.frame()) 
4. with.default(pars, {
    graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
        weight_decay = weight_decay, learning_rate = learning_rate, 
        n_steps = n_steps, ndim = ndim) ... 
3. with(pars, {
    graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
        weight_decay = weight_decay, learning_rate = learning_rate, 
        n_steps = n_steps, ndim = ndim) ... 
2. dimen@fun(dat, dimension_params) 

Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: module 'tensorflow' has no attribute 'placeholder' 

As by the common solution is to Disable Tensorflow 2 Behaviour as stated in Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session', I tried to use reticulate and suppress the errors by this example:

library(reticulate)
x <- import("tensorflow.compat.v1", as="tf") 
x$disable_v2_behavior()

but this doesn't change anything.. and I still get AttributeError, I am wondering, How should I make a proper change in Tensorflow from R in this case?

Here is Sample Data used for the example: https://drive.google.com/file/d/1Yt4V1Ir00fm1vQ9futziWbwjUE9VvYK7/view?usp=sharing


Solution

  • I found out deeper that tf acts as R tensorflow module, since ?tf is a valid command after using library(tensorflow), and then since Tensorflow updated to version 2+, instead using tf$placeholder, use tf$compat$v1$placeholder, so I had an idea to add the features available in tf$compat$v1 to tf

    tf_synchronize <- function(){
      library(tensorflow)
      rm(list=c("tf")) #Delete first if there any tf variable in Global Environment
      tf_compat_names <- names(tf$compat$v1)  
      for(x in 2:length(tf_compat_names)){
        tf[[tf_compat_names[x]]] <- tf$compat$v1[[tf_compat_names[x]]]
      }
    }
    

    With this executed, the AttributeError are no more, and Auto Encoder from Dimension Reduction is successfully executed