mikedewar / EDHMM

An implementation of the Explicit Duration HMM using Python
12 stars 11 forks source link

experiments all fail due to sample_mean_prec() mean calculation #1

Open danstowell opened 8 years ago

danstowell commented 8 years ago

Hi. I read the paper, I wanted to try the code!

I'm using python 2.7. If I run any of the experiments (i.e. cd experiments; python experiment_1.py) I get something like this:

../transition.py:25: RuntimeWarning: divide by zero encountered in log
  return np.log(self.A[i,j])
../log_space.py:7: RuntimeWarning: underflow encountered in exp
  return lny + log(1 + exp(lnx - lny))
Traceback (most recent call last):
  File "experiment_1.py", line 70, in 
    online=True, sample_U = True
  File "../edhmm.py", line 537, in beam
    self.O.update(Z_samples, Y)
  File "../emission.py", line 140, in update
    mu, tau = self.sample_mean_prec(Z, Y)
  File "../emission.py", line 59, in sample_mean_prec
    ybar = np.mean(n[i],1)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2885, in mean
    out=out, keepdims=keepdims)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 56, in _mean
    rcount = _count_reduce_items(arr, axis)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 50, in _count_reduce_items
    items *= arr.shape[ax]
IndexError: tuple index out of range
zsh: exit 1     python experiment_1.py

I notice that the line that chokes is protected by an except ValueError. Maybe this should, or should also, guard against IndexError, but I don't know the code so I don't know the intention.

(In case it matters, on my system I also had to mildly rearrange the order of imports)

mikedewar commented 8 years ago

It's been many years since we wrote this, so I'm not really sure there remains any specific intention! I guess I'd worry first about why ybar = np.mean(n[i],1) is puking an index error. If you have a moment, can you see how big n is and what i is causing the problem? No worries if not, I'll try to get around to having a play at some point!

danstowell commented 8 years ago

Yeah I realise the code is a little old, but I'm rather hoping it can still be useful!

It turns out that n is a dict with keys 0, 1, 2 and each one holds a numpy array whose shapes are:

 0: (99,)
 1: (105,)
 2: (296,)

Each of the Y passed in via Ys is a list containing scalar-valued arrays, such as [array(-2.8626048450849297), array(-2.043008544196465), array(-3.752285935165192), .... When converted using np.array(.).T it makes a flat 1D array - the programmer was clearly expecting to end up with 2D arrays. I wondered about changing the conversion to np.array(., ndmin=2).T - but then there's an np.squeeze() immediately after, deliberately dropping size-1 axes. It gives me the feeling that something is happening such as the code expects vector-valued observations and is being given scalar-valued ones?