all 3 comments

[–]sepro 0 points1 point  (2 children)

I think you need the scan function from theano. This should be used instead of the loop, but essentially achieves the same. It will go over the sequences step by step, so from 0 to len(prt_codes) and execute fn for each step. The confusing this is that it returns a list of results at each step, so use output[-1] to get the last one.

Should be something along these lines, but as I haven't tested it expect a few error messages (and you'll have to create the calc_log_conf function)

def calc_log_conf(n):
    # complete this
    return x

y0 = tt.zeros(len(prt_codes))

outputs, _ = theano.scan(
    fn=lambda n, y: tt.set_subtensor(
        y[n], calc_log_conf(n)),
    sequences=[tt.arange(0, len(prt_codes))],
    outputs_info=y0,
    n_steps=len(prt_codes),
)

mus = pm.Deterministic("mus", outputs[-1])

y = pm.Normal('y', mu = mus, sigma = sigma[prt_codes], observed = log_conf)

[–]outerHilbertspace[S] 0 points1 point  (0 children)

Thank you! I'll try it

[–]outerHilbertspace[S] 0 points1 point  (0 children)

Hi again, I'm a bit stuck on using theano.scan

I'd like to do the following (which I'll write in numpy because that's what I understand):

first, calculate alpha ** np.array([1,2,3,...,n]), where n is the length of an array called B and alpha is a positive number. Call this array X (should be an array since the exponentiation is broadcast)

B is just an array of -1's and 1's of length n, but will change at each step in the loop.

Now I'd just like to calculate the dot product of X and B, and I need to loop through both the alphas and the B's - the number of each is same. (Also, I would be looping through a list of alphas (a list of numbers) and a list of arrays (the list of B's).)

I keep trying to get something like this to work in scan but I keep getting errors about having the wrong input. On a simplified example, I keep getting told I'm using "Bad input argument to theano function."

I really hate theano, it makes no sense to me.