Open sgvignali opened 3 years ago
I had a look at the error again. It seems that the layer is created correctly even if the error is raised, but I didn't check yet if is possible to train the model. The summary includes the independent_normal layer though:
library(keras)
library(tfprobability)
input_shape <- c(28, 28, 1)
encoded_shape <- 2
n <- 2
input <- layer_input(shape = input_shape)
# The following generates the error
output <- input %>%
layer_flatten(input_shape = input_shape) %>%
layer_dense(units = n) %>%
layer_dense(units = params_size_independent_normal(encoded_shape)) %>%
layer_independent_normal(event_shape = encoded_shape)
# But the model can be created
model <- keras_model(input, output)
summary(model)
Interestingly, the error is printed only when the layer is assigned to a variable. For example the following doesn't generate the error:
input %>%
layer_flatten(input_shape = input_shape) %>%
layer_dense(units = n) %>%
layer_dense(units = params_size_independent_normal(encoded_shape)) %>%
layer_independent_normal(event_shape = encoded_shape)
For what I understood the error is generated by the python __str__
method, maybe because some information of the layer is missing, for example output$type_spec
generates a similar error.
I hope this helps to solve the problem.
I have tested this and I think this is an upstream bug. The following Python code receives a not implemented error but the model is formed correctly. I suggest we file a bug report with TensorFlow Probability.
import tensorflow as tf
import tensorflow_probability as tfp
tfkl = tf.keras.layers
tfpl = tfp.layers
input_shape = [28, 28, 1]
encoded_shape = 2
inputs = tf.keras.Input(shape=input_shape)
layer1 = tfkl.Flatten()(inputs)
layer2 = tfkl.Dense(units=10)(layer1)
layer3 = tfkl.Dense(units=tfpl.IndependentNormal.params_size(encoded_shape)) (layer2)
outputs = tfpl.IndependentNormal(event_shape = encoded_shape) (layer3)
model=tf.keras.Model(inputs, outputs)
model.summary()
To follow up, str
and repr
cannot be called on the object because it cannot be serialised. I suggest we close this issue as the limitation is upstream, is known (though poorly documented IMO and the error message is uninformative), and is unlikely to be fixed anytime soon. See: https://github.com/tensorflow/probability/issues/1500.
I get the following error when I use the
layer_independent_normal()
with the keras functional API:Here is the working example taken from the documentation:
And this is using the functional API:
With the second example I get the error, am I doing something wrong?
This is my
sessionInfo()
The same happens with
layer_independent_bernoulli()
or usinglayer_distribution_lambda()
. Any help would be very much apreciated!