Open hrlai opened 3 years ago
Thanks for posting! I confirm that I can get the same error:
library(greta)
#>
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#>
#> binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#>
#> %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#> eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#> tapply
# model matrix
modmat <- model.matrix(~ Sepal.Width, iris)
# index of species
jj <- as.numeric(iris$Species)
M <- ncol(modmat) # number of varying coefficients
N <- max(jj) # number of species
# prior on the standard deviation of the varying coefficient
tau <- exponential(0.5, dim = M)
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✓ Initialising python and checking dependencies ... done!
#>
# prior on the correlation between the varying coefficient
Omega <- lkj_correlation(3, M)
# optimization of the varying coefficient sampling through
# cholesky factorization and whitening
Omega_U <- chol(Omega)
Sigma_U <- sweep(Omega_U, 2, tau, "*")
z <- normal(0, 1, dim = c(N, M))
ab <- z %*% Sigma_U # equivalent to: ab ~ multi_normal(0, Sigma_U)
# the linear predictor
mu <- rowSums(ab[jj,] * modmat)
# the residual variance
sigma_e <- cauchy(0, 3, truncation = c(0, Inf))
#model
y <- iris$Sepal.Length
distribution(y) <- normal(mu, sigma_e)
m <- model(ab, sigma_e)
draws <- mcmc(m, chains = 4, initial_values = initials(sigma_e = 1))
#> only one set of initial values was provided, and was used for all chains
#> Error in py_call_impl(callable, dots$args, dots$keywords): ValueError: Cannot feed value of shape (1, 13) for Tensor 'Placeholder:0', which has shape '(?, 10)'
#>
#> Detailed traceback:
#> File "/Users/njtierney/Library/r-miniconda/envs/greta-env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
#> run_metadata_ptr)
#> File "/Users/njtierney/Library/r-miniconda/envs/greta-env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1149, in _run
#> str(subfeed_t.get_shape())))
Created on 2021-09-28 by the reprex package (v2.0.1)
I'll have a think about this, we're a bit pushed at the moment for time, just wanted to give you a heads up we might not get to this as soon as we would like to help you :)
Just browsing #314 and saw some discussion on dimensions and placeholders, just noting it down in case they are related.
I was recently trying to do prior predictive check and discovered that calculate
also doesn't work on a greta array with chol
operation...
library(greta)
#>
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#>
#> binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#>
#> %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#> eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#> tapply
M <- 3
Omega <- lkj_correlation(3, M)
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#>
Omega_U <- chol(Omega)
calculate(Omega, nsim = 1) # works
#> $Omega
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 -0.1751263 0.1297896
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] -0.1751263 1 0.1951698
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0.1297896 0.1951698 1
calculate(Omega_U, nsim = 1) # fails
#> You must feed a value for placeholder tensor 'Placeholder_1' with dtype double and shape [1,3,3]
#> [[node Placeholder_1 (defined at /ops/array_ops.py:2143) ]]
#>
#> Original stack trace for 'Placeholder_1':
#> File "/ops/array_ops.py", line 2143, in placeholder
#> return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
#> File "/ops/gen_array_ops.py", line 6262, in placeholder
#> "Placeholder", dtype=dtype, shape=shape, name=name)
#> File "/framework/op_def_library.py", line 788, in _apply_op_helper
#> op_def=op_def)
#> File "/util/deprecation.py", line 507, in new_func
#> return func(*args, **kwargs)
#> File "/framework/ops.py", line 3616, in create_op
#> op_def=op_def)
#> File "/framework/ops.py", line 2005, in __init__
#> self._traceback = tf_stack.extract_stack()
Created on 2023-09-26 with reprex v2.0.2
In case they are related, I'm linking #585 here.
Hi! Whenever the
lkj_correlation()
prior is part of a model, theinitial_values
argument seems to always causemcmc()
to fail.Reproducible code from the
greta
example models page:On my computer this throws an error:
On various datasets, the
Cannot feed value of shape (1, 13) for Tensor 'Placeholder:0', which has shape '(?, 10)'
message always have the first number13
larger than the second number10
. And the difference between them seems to beN
--- this led me to suspect the LKJ prior in the first place.I tried to remove LKJ from the model and initial values work again. (After restarting R session) This is reproducible via:
I'd really like to keep the LKJ prior as well as being able to specify initial values to help chain convergence. Looking forward to hear your idea!