-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Logistic regression with time-varying coefficients: cannot construct sampler #90
Comments
Thank for the bug report! We'll take a closer look at it shortly. |
I have a fix in #93 for the error you were seeing, but we still need to look into the samplers AeMCMC produces (or lack thereof) for your model. |
Thank you, that was quick! I've tried with the patch, and Here is the new code: import numpy as np
import aesara
import aesara.tensor as at
from aemcmc.basic import construct_sampler
from aesara.tensor.random.utils import RandomStream
def logistic_fit(X_val, y_val):
N, M, T = X_val.shape
srng = RandomStream(0)
X = at.tensor3("X")
sigma_rv = srng.exponential(1, size=X.shape[1])
beta_t_rv = at.cumsum(srng.normal(0, 1/sigma_rv, size=(X.shape[1],X.shape[2])), axis=1)
eta = at.tensordot(X, beta_t_rv, 2)
p = at.sigmoid(-eta)
Y_rv = srng.bernoulli(p, name="Y")
y_vv = Y_rv.clone()
y_vv.name = "y"
sample_vars = [sigma_rv, beta_t_rv]
sampler, initial_values = construct_sampler({Y_rv: y_vv}, srng)
inputs = [X, y_vv] + [initial_values[rv] for rv in sample_vars]
outputs = [sampler.sample_steps[rv] for rv in sample_vars]
sample_step = aesara.function(
inputs,
outputs,
updates=sampler.updates,
on_unused_input="ignore",
)
sigma_val = np.ones(M)
beta_pst_vals = []
sigma_pst_val, beta_pst_val = (
sigma_val,
np.zeros(M,T)
)
for i in range(100):
sigma_pst_val, beta_pst_val = sample_step(
X_val,
y_val,
sigma_pst_val,
beta_pst_val
)
beta_pst_vals += [beta_pst_val]
beta_pst_mean = np.mean(beta_pst_vals, axis=0)
return beta_pst_mean
# X_val = np.load("X_val.npy")
# y_val = np.load("y_val.npy")
X_val = np.zeros((1000, 50, 10))
y_val = np.zeros(1000)
beta = logistic_fit(X_val, y_val) Here is the error (also notice the warning) /Users/acristia/anaconda3/lib/python3.8/site-packages/aehmc/utils.py:43: UserWarning: The following parameters need to be computed in order to determine the shapes in this parameter map: [<TensorType(float64, (None, None))>]
warnings.warn(
Traceback (most recent call last):
File "examples/gibbs_sample.py", line 61, in <module>
beta = logistic_fit(X_val, y_val)
File "examples/gibbs_sample.py", line 28, in logistic_fit
inputs = [X, y_vv] + [initial_values[rv] for rv in sample_vars]
File "examples/gibbs_sample.py", line 28, in <listcomp>
inputs = [X, y_vv] + [initial_values[rv] for rv in sample_vars]
KeyError: CumOp{1, add}.0 Let me know if I can provide more useful information! |
That |
Looked like an error to me? |
Yes, it is. I'll reopen this and take a look soon. |
Description of your problem or feature request
I want to perform a classification task which consists in predicting whether a document$i$ published at a time $t_i$ belongs to a certain class ( $y_i=1$ ) or not ( $y_i=0$ ). The contents of $i$ is given by a bag of words $b_{ij}$ . The predictive features are encoded into a sparse tensor $x_{ijt} = b_{ij} \delta_{t,t_i}$ . For this task I am considering a logistic regression with time-varying coefficients, such that the coefficients evolve through a random walk:
For the sake of simplicity below I assume$\sigma_{j,0}=\sigma_j$ , although I do not see any compelling reason to do so.
However:
at.tensordot(X, beta_t_rv, 2)
involves too many needless operations. I would not have done that with Stan but in this case I found it easier to write the model this way.Please provide a minimal, self-contained, and reproducible example.
As a clueless user, I tried to build a sampler based on my generative model by looking at other examples; which means I could be doing something completely idiotic!
Here is what I came up with:
Although thus far the script does not actually use the data, if at some point you want to try with actual data, download and unzip these files (X_val.npy and y_val.npy) to the folder the script is executed from: data.zip
However, this crashes:
Please provide the full traceback of any errors.
Versions and main components
The text was updated successfully, but these errors were encountered: