Skip to content

cast Distribution Inputs to floatX instead of tensor_variable #2233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from

Conversation

hvasbath
Copy link
Contributor

@hvasbath hvasbath commented May 28, 2017

Removes slowdown again:
#1824
Running the test from that thread:
[v8, v7, v6, v5, v4, v3, v2, v1]
('time for initialising population', 1.4203970432281494) previously 20s +

Also fixes AttributeError in multivariate.py line 250.

Was not sure about the Mixture Distribution as there are a lot of theano and numpy operators mixed in the random method generation. Someone having insight should fix that one ...

@junpenglao
Copy link
Member

Could you test if it pass test in https://github.com/pymc-devs/pymc3/blob/master/pymc3/tests/test_distributions.py#L827-L838 locally? The _repr_latex is using the Name from Theano tensor.

@hvasbath
Copy link
Contributor Author

Yep that one fails! How to deal with that? You could simply use the Distribution name and attribute names?

@junpenglao
Copy link
Member

I think we should change the function https://github.com/pymc-devs/pymc3/blob/master/pymc3/util.py#L79

@junpenglao
Copy link
Member

junpenglao commented May 28, 2017

This might work:

def get_variable_name(variable):
     """Returns the variable data type if it is a constant, otherwise
     returns the argument name.
     """
     try:
        name = variable.name
     except AttributeError:
        name = None
     if name is None:
        if hasattr(variable, 'get_parents'):
            try:
                names = [get_variable_name(item) for item in variable.get_parents()[0].inputs]
                return 'f(%s)' % ','.join([n for n in names if isinstance(n, str)]) 
            except IndexError:
                pass
        try:
            value = variable.eval()
        except AttributeError:
            value = variable
        if not value.shape:
            return asscalar(value)
        return 'array'
    return name

for some reason I cannot check out your branch. Also any idea why Travis is not building @twiecki ?

@fonnesbeck
Copy link
Member

Yes, go for it.

Copy link
Member

@ColCarroll ColCarroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't figure out why it isn't building -- can you either make some of the suggested changes and push, or just git commit --allow-empty and then push?

@@ -247,7 +247,7 @@ def _repr_latex_(self, name=None, dist=None):
mu = dist.mu
try:
cov = dist.cov
except AttributeErrir:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yowza

self.k = tt.as_tensor_variable(shape)
self.a = a = tt.as_tensor_variable(a)
self.k = floatX(shape)
self.a = a = floatX(a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total nit, and not necessary, but might be nicer to just use self.a below, and stop referring to a (same with Sigma and nu.

@hvasbath
Copy link
Contributor Author

The test failed, because you need scipy 0.18 otherwise there is no skewnorm. The requirements have to be updated!
Also I get tons of PEP8 errors too long lines... for now I ignored all of them, as you dont seem to care either?

@hvasbath
Copy link
Contributor Author

hvasbath commented May 29, 2017

Travis still not starting ...?
Locally test_distributions passes now. However in MvStundentT there is strange behavior. There n should be an int by nature, but the test fails if it is not a float. l. 651. But I have not enough insight in the test and no time to investigate that quite complex structure. So I cast it to float and edit the latex test ...
Also I introduce an intX function. If we use float32 to run on the GPU all Ints should be cast to int16, because interaction of int32 and above with float32 results in float64, what we dont want...

@junpenglao
Copy link
Member

https://travis-ci.org/pymc-devs/pymc3/requests

abuse detected: known offender (request looked fishy) 

What have you done @hvasbath 😂!

@hvasbath
Copy link
Contributor Author

hvasbath commented May 29, 2017

WTH???? I am sitting in Saudi Arabia-maybe my IP is suspicious ... :)
K thanks @junpenglao I did that lets see...

@junpenglao
Copy link
Member

You are on the naughty list 😂!
You should email [email protected] travis-ci/travis-ci#6942

@hvasbath
Copy link
Contributor Author

hvasbath commented May 29, 2017

Had to connect git with travis to resolve that- apparently this was a false positive of their malware detection ...

@hvasbath
Copy link
Contributor Author

OMG all these failed tests! Where do all these float64 casts come from? I have no time right now to look into all of these tests and whats going wrong, some of them as well assume the variables to be tensors ... not sure what are your oppinions?

@junpenglao
Copy link
Member

I dont think it's that bad, you just need to recast some of the parameters into tensor.

@hvasbath
Copy link
Contributor Author

But this is the point in the first place! We do not want to do that in order to have the random method perform fast ...

@twiecki
Copy link
Member

twiecki commented May 30, 2017

That is surprising, @hvasbath I would imagine that the a single issue underlies most of the failing tests, so perhaps just look into one and I would hope that it would resolve the other ones.

@junpenglao
Copy link
Member

junpenglao commented May 30, 2017

But this is the point in the first place! We do not want to do that in order to have the random method perform fast ...

I know, as long as they are not tensor in the random method right?
I fixed some test, still failing in

pymc3/tests/sampler_fixtures.py:130: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
pymc3/tests/sampler_fixtures.py:155: in make_step
    mu, stds, elbo = pm.advi(n=50000)
pymc3/variational/advi.py:164: in advi
    uw_i, elbo_current = f()
/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py:898: in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
/usr/local/lib/python3.5/dist-packages/theano/gof/link.py:325: in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
../../../.local/lib/python3.5/site-packages/six.py:685: in reraise
    raise value.with_traceback(tb)

@junpenglao junpenglao mentioned this pull request May 31, 2017
@hvasbath
Copy link
Contributor Author

Rebased the PR, fixed tests in variational_inference.
Something like float32 -1 will result in float64! Please keep that in mind while coding ...I had to fix a lot of these things in the tests. I wonder why the tests didnt fail before? I guess it works with tensors as theano can detect this and autocasts?
Any comments? Maybe I got something totally wrong here...

@hvasbath
Copy link
Contributor Author

hvasbath commented Jun 12, 2017

This likely only needs the new fix from @junpenglao included, then also these last tests should pass. However, I cannot rebase again without getting hundreds of conflicts. Can anybody say why?
I tried in this branch
git rebase master
When I did it yesterday it gave only few conflicts that were resolvable by hand. However, now at every line that I changed here, it reports a conflict ...

@hvasbath
Copy link
Contributor Author

Again what learned :) .. Merge did the trick and all the mess went away.

self.alpha = alpha = tt.as_tensor_variable(alpha)
self.m = m = tt.as_tensor_variable(m)
self.alpha = floatX(alpha)
self.m = floatX(m)

self.mean = tt.switch(tt.gt(alpha, 1), alpha *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt here be self.alpha instead of just alpha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep missed that!

@@ -438,17 +441,17 @@ class DiscreteUniform(Discrete):

def __init__(self, lower, upper, *args, **kwargs):
super(DiscreteUniform, self).__init__(*args, **kwargs)
self.lower = tt.floor(lower).astype('int32')
self.upper = tt.floor(upper).astype('int32')
self.lower = intX(np.floor(lower))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it work if lower being a tensor? Is intX(lower) sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having theano tensor as distribution bound inputs? Is that intended? I should have known that in advance! If yes we can forget about this whole PR ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah theano tensor could be distribution bound.

In this case can you not do something similar to Uniform(Continuous):

        self.lower = floatX(lower)
        self.upper = floatX(upper)

@hvasbath
Copy link
Contributor Author

Oh no due to the rebase and then the merge there are duplicated commits now! Argh ... How to fix that?

@twiecki
Copy link
Member

twiecki commented Jun 12, 2017

@hvasbath Looks OK to me. We will do a squash-merge in any case so it doesn't really matter.

@hvasbath hvasbath mentioned this pull request Jun 12, 2017
@hvasbath
Copy link
Contributor Author

ok then I will close the other PR again.

@hvasbath
Copy link
Contributor Author

Continued in the other cleaned up PR!

@hvasbath hvasbath closed this Jun 12, 2017
@hvasbath hvasbath deleted the random_slowdown branch July 2, 2017 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants