rstudio / tfprobability

R interface to TensorFlow Probability
https://rstudio.github.io/tfprobability/
Other
54 stars 16 forks source link

TF 2.0 Compatibility #60

Closed atroiano closed 5 years ago

atroiano commented 5 years ago

Hey. I am trying to implement a basic example of TF 2.0 functionality using TF probability and it appears it does not work. I am basically testing the first example from this post https://blogs.rstudio.com/tensorflow/posts/2019-06-05-uncertainty-estimates-tfprobability/

tensorflow::install_tensorflow(version = "2.0.0-beta1",method = 'conda')
# generate the data
x_min <- -40
x_max <- 60
n <- 150
w0 <- 0.125
b0 <- 5

normalize <- function(x) (x - x_min) / (x_max - x_min)

# training data; predictor 
x <- x_min + (x_max - x_min) * runif(n) %>% as.matrix()

# training data; target
eps <- rnorm(n) * (3 * (0.25 + (normalize(x)) ^ 2))
y <- (w0 * x * (1 + sin(x)) + b0) + eps

model <- keras_model_sequential() %>%
  layer_dense(units = 8, activation = "relu") %>%
  layer_dense(units = 2, activation = "linear") %>%
  layer_distribution_lambda(function(x)
    tfd_normal(loc = x[, 1, drop = FALSE],
               # ignore on first read, we'll come back to this
               # scale = 1e-3 + 0.05 * tf$math$softplus(x[, 2, drop = FALSE])
               scale = 1e-3 + tf$math$softplus(x[, 2, drop = FALSE])
    )
  )
negloglik <- function(y, model) - (model %>% tfd_log_prob(y))
learning_rate <- 0.01
model %>% compile(optimizer = optimizer_adam(lr = learning_rate), loss = negloglik)

model %>% fit(x, y, epochs = 1000) 

I am getting the following error:


  AttributeError: in converted code:
    relative to /anaconda3/envs/r-tensorflow/lib/python3.6/site-packages:

    tensorflow_probability/python/layers/distribution_layer.py:211 call  *
        distribution, value = super(DistributionLambda, self).call(
    tensorflow/python/keras/layers/core.py:785 call
        return self.function(inputs, **arguments)
    tensorflow_probability/python/layers/distribution_layer.py:189 _fn
        distribution.shape = value.shape
    tensorflow/python/training/tracking/tracking.py:80 __setattr__
        super(AutoTrackable, self).__setattr__(name, value)

    AttributeError: can't set attribute ```

This same code works fine in the current nightly build of TF, using tensorflow::install_tensorflow(version = "nightly")
skeydan commented 5 years ago

https://github.com/tensorflow/probability/issues/503

atroiano commented 5 years ago

See ticket referenced above for the resolution.