Skip to content

plt.yscale("log") gives FloatingPointError: underflow encountered in multiply #4285

Closed

Description

@theopavlakou

I'm running an optimisation algorithm and I'm using numpy. When I plot the graph using a linear graph, everything works fine, however, in a certain scenario when I use plt.yscale("log") I get the following errors:

http://hastebin.com/soranibaki.avrasm

but I have no idea why since there aren't even any zero values in the arrays that I'm plotting.

Activity

  1. tacaswell commented on Mar 27, 2015

    @tacaswell
    Member

    In the future, please paste exceptions directly here. That link will rot.

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
        return self.func(*args)
      File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 380, in motion_notify_event
        FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=event)
      File "/usr/lib64/python2.6/site-packages/matplotlib/backend_bases.py", line 1833, in motion_notify_event
        guiEvent=guiEvent)
      File "/usr/lib64/python2.6/site-packages/matplotlib/backend_bases.py", line 1440, in __init__
        LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent)
      File "/usr/lib64/python2.6/site-packages/matplotlib/backend_bases.py", line 1358, in __init__
        xdata, ydata = trans.transform_point((x, y))
      File "/usr/lib64/python2.6/site-packages/matplotlib/transforms.py", line 1332, in transform_point
        return self.transform(np.asarray([point]))[0]
      File "/usr/lib64/python2.6/site-packages/matplotlib/transforms.py", line 1273, in transform
        return self.transform_affine(self.transform_non_affine(values))
      File "/usr/lib64/python2.6/site-packages/matplotlib/transforms.py", line 2220, in transform_non_affine
        self._a.transform(points))
      File "/usr/lib64/python2.6/site-packages/matplotlib/transforms.py", line 2010, in transform_non_affine
        y_points = y.transform_non_affine(points[:, 1])
      File "/usr/lib64/python2.6/site-packages/matplotlib/scale.py", line 136, in transform_non_affine
        return ma.power(10.0, a) / 10.0
      File "/usr/lib64/python2.6/site-packages/numpy/ma/core.py", line 3715, in __div__
        return divide(self, other)
      File "/usr/lib64/python2.6/site-packages/numpy/ma/core.py", line 1084, in __call__
        m |= filled(domain(da, db), True)
      File "/usr/lib64/python2.6/site-packages/numpy/ma/core.py", line 778, in __call__
        return umath.absolute(a) * self.tolerance >= umath.absolute(b)
    FloatingPointError: underflow encountered in multiply

    Oddly, it is coming out of the motion notify event

  2. tacaswell commented on Mar 27, 2015

    @tacaswell
    Member

    Can you provide a minimal amount code code that will reproduce this?

  3. added this to the milestone on Mar 27, 2015
  4. theopavlakou commented on Mar 27, 2015

    @theopavlakou
    Author

    Errr, it's a little bit difficult because the optimisation algorithm is not just one method, it's a whole class and then you need the code for making the data. Would you like me to post these?

  5. tacaswell commented on Mar 27, 2015

    @tacaswell
    Member

    No, just the mpl related code. You should be able to sort out what artists
    and data trigger the issue.

    On Fri, Mar 27, 2015, 12:10 theopavlakou notifications@github.com wrote:

    Errr, it's a little bit difficult because the optimisation algorithm is
    not just one method, it's a whole class and then you need the code for
    making the data. Would you like me to post these?

    —
    Reply to this email directly or view it on GitHub
    #4285 (comment)
    .

  6. theopavlakou commented on Mar 27, 2015

    @theopavlakou
    Author
    =================================
    Graph class
    =================================
    
    class Graph(object):
        def __init__(self, title, xlabel, ylabel, logx=False, logy=False, legend_loc="upper right", figsize=(15.0, 12.0)):
            self.fig = plt.figure(figsize=figsize)
            self.title = title
            self.xlabel = xlabel
            self.ylabel = ylabel
            self.logx = logx
            self.logy = logy
            self.loc = legend_loc
    
        def plot_points(self, x_seq, y_seq, label, color="blue", plot_or_scatter="plot"):
            if plot_or_scatter == "plot":
                plt.plot(x_seq, y_seq, c=color, label=label)
            elif plot_or_scatter == "scatter":
                plt.scatter(x_seq, y_seq, c=color, label=label)
    
            plt.legend(loc=self.loc)
            if self.logy:
                plt.yscale("log")
            if self.logx:
                plt.xscale("log")
            plt.xlabel(self.xlabel)
            plt.ylabel(self.ylabel)
            plt.title(self.title)
    
        def savefig(self, name, path="./img/", extension=".png"):
            plt.savefig(path+name+extension)
    
        def show_plot(self):
            plt.show()
    
    =========================
    Plotting
    =========================
    
    g = graphs.Graph("How the error decreases as a function of gradient evaluations", xlabel="Number of gradient evaluations $k$",
                     ylabel="$f(x^k) - f(x^*)$", logx=False, logy=True, legend_loc="upper right")
    g.plot_points(N*np.arange(len(eps_gd)), eps_gd, label="Gradient Descent", color="r")
    g.plot_points(np.arange(len(eps_sgd)), eps_sgd, label="Stochastic GD", color="blue")
    g.plot_points(np.arange(len(eps_sdca)), eps_sdca, label="SDCA", color="y")
    g.savefig("gd_vs_sdca_vs_sgd_error_log_reg_max_it_"+str(max_it))
    g.show_plot()
    
    I have also done this without this class and the same thing happens. 
    
    (and in case you'd like to try with the same data)
    [0.36698146485064509, 0.20704331501487561, 0.14206043509635857, 0.09968463379203385, 0.07022171484128592, 0.049604656820509518, 0.035136757642150296, 0.024956116953530172, 0.01777241282436548, 0.012689449460893787, 0.0090831731297289497, 0.0065178204606318735, 0.0046882365901543821, 0.0033801200532111997, 0.0024425527177182049, 0.001768963867761375, 0.0012838995820977361, 0.00093379967777484918, 0.00068054965494646069, 0.00049696002808896633, 0.0003635883149545327, 0.00026649903730413538, 0.00019568085032295457, 0.00014392525189288463, 0.00010603034743850159, 7.8234096865692493e-05, 5.7809963766031203e-05, 4.2777767908730446e-05, 3.169644988842979e-05, 2.3515210919033969e-05, 1.7466348328665227e-05, 1.2987939843944751e-05, 9.6679432600721427e-06, 7.2036945220066428e-06, 5.3725018918093781e-06, 4.0102530640684364e-06, 2.9958209949443315e-06, 2.2396748061703775e-06, 1.6755463188022901e-06, 1.2543213911975215e-06, 9.3955429569803073e-07, 7.0416839004217024e-07, 5.2802542949148545e-07, 3.9613208857591786e-07, 2.9731468886851076e-07, 2.2323855064509956e-07, 1.6768137711675735e-07, 1.2599419996206507e-07, 9.4700992159246766e-08, 7.1200950246819161e-08, 5.3546877154531103e-08, 4.0280069169007504e-08, 3.0307159004649975e-08, 2.2808209099167698e-08, 1.7168036325898584e-08, 1.2924885384180129e-08, 9.7320214109686276e-09, 7.328986084687017e-09, 5.5200598536586654e-09, 4.1581271759127958e-09, 3.1325738669707448e-09, 2.3602070298167632e-09, 1.7784449468649655e-09, 1.3401957321690361e-09, 1.0100205116714278e-09, 7.6124340164795967e-10, 5.7377957940474289e-10, 4.3250569792263605e-10, 3.2603220123661458e-10, 2.457807291023073e-10, 1.8529000556100073e-10, 1.3969203571662092e-10, 1.0531742145047929e-10, 7.9402484587376421e-11, 5.9865001844627841e-11, 4.5133563553179101e-11, 3.4026781392526573e-11, 2.5652036050871629e-11, 1.9337642598316052e-11, 1.457567400109383e-11, 1.0984990694851149e-11, 8.2768236708830045e-12, 6.2350125062948791e-12, 4.6950221488373245e-12, 3.5338398873818733e-12, 2.6582069878600123e-12, 1.9976242882080442e-12, 1.4993561947562739e-12, 1.1233236563157334e-12, 8.4021678503631847e-13, 6.2638783049351332e-13, 4.6540549192286562e-13, 3.439470930288735e-13, 2.5213164889237305e-13, 1.8318679906315083e-13, 1.3078427230084344e-13, 9.1260332624187868e-14, 6.1950444774083735e-14, 3.9301895071730542e-14, 2.2648549702353193e-14]

    [TAC edited for markup]

  7. tacaswell commented on Mar 27, 2015

    @tacaswell
    Member

    If you put triple backticks around your code it will preserve the formatting.

  8. theopavlakou commented on Mar 27, 2015

    @theopavlakou
    Author

    Thanks, do you think you'll need anything else?

  9. theopavlakou commented on Mar 31, 2015

    @theopavlakou
    Author

    Has there been any progress here?

    Thanks

  10. tacaswell commented on Mar 31, 2015

    @tacaswell
    Member

    There is still a whole lot going on in your example. Can you reproduce this with just plot and set_yscale? Is it dependent on the data you are plotting? Does this happen with other backends?

  11. theopavlakou commented on Mar 31, 2015

    @theopavlakou
    Author

    Yes, I can reproduce this with just plot and set_yscale. It is dependent on the data it seems (that's why I included the data, so that you could reproduce it) but nothing seems to be wrong with the data, if you analyse it (I believe). By other backends are you talking about using something like gtk instead of tkinter? If so, yes it does happen in that case also.

  12. tacaswell commented on May 24, 2015

    @tacaswell
    Member

    I can not reproduce this

    import matplotlib.pyplot as plt
    
    data = [0.36698146485064509, 0.20704331501487561, 0.14206043509635857, 0.09968463379203385, 0.07022171484128592, 0.049604656820509518, 0.035136757642150296, 0.024956116953530172, 0.01777241282436548, 0.012689449460893787, 0.0090831731297289497, 0.0065178204606318735, 0.0046882365901543821, 0.0033801200532111997, 0.0024425527177182049, 0.001768963867761375, 0.0012838995820977361, 0.00093379967777484918, 0.00068054965494646069, 0.00049696002808896633, 0.0003635883149545327, 0.00026649903730413538, 0.00019568085032295457, 0.00014392525189288463, 0.00010603034743850159, 7.8234096865692493e-05, 5.7809963766031203e-05, 4.2777767908730446e-05, 3.169644988842979e-05, 2.3515210919033969e-05, 1.7466348328665227e-05, 1.2987939843944751e-05, 9.6679432600721427e-06, 7.2036945220066428e-06, 5.3725018918093781e-06, 4.0102530640684364e-06, 2.9958209949443315e-06, 2.2396748061703775e-06, 1.6755463188022901e-06, 1.2543213911975215e-06, 9.3955429569803073e-07, 7.0416839004217024e-07, 5.2802542949148545e-07, 3.9613208857591786e-07, 2.9731468886851076e-07, 2.2323855064509956e-07, 1.6768137711675735e-07, 1.2599419996206507e-07, 9.4700992159246766e-08, 7.1200950246819161e-08, 5.3546877154531103e-08, 4.0280069169007504e-08, 3.0307159004649975e-08, 2.2808209099167698e-08, 1.7168036325898584e-08, 1.2924885384180129e-08, 9.7320214109686276e-09, 7.328986084687017e-09, 5.5200598536586654e-09, 4.1581271759127958e-09, 3.1325738669707448e-09, 2.3602070298167632e-09, 1.7784449468649655e-09, 1.3401957321690361e-09, 1.0100205116714278e-09, 7.6124340164795967e-10, 5.7377957940474289e-10, 4.3250569792263605e-10, 3.2603220123661458e-10, 2.457807291023073e-10, 1.8529000556100073e-10, 1.3969203571662092e-10, 1.0531742145047929e-10, 7.9402484587376421e-11, 5.9865001844627841e-11, 4.5133563553179101e-11, 3.4026781392526573e-11, 2.5652036050871629e-11, 1.9337642598316052e-11, 1.457567400109383e-11, 1.0984990694851149e-11, 8.2768236708830045e-12, 6.2350125062948791e-12, 4.6950221488373245e-12, 3.5338398873818733e-12, 2.6582069878600123e-12, 1.9976242882080442e-12, 1.4993561947562739e-12, 1.1233236563157334e-12, 8.4021678503631847e-13, 6.2638783049351332e-13, 4.6540549192286562e-13, 3.439470930288735e-13, 2.5213164889237305e-13, 1.8318679906315083e-13, 1.3078427230084344e-13, 9.1260332624187868e-14, 6.1950444774083735e-14, 3.9301895071730542e-14, 2.2648549702353193e-14]
    
    fig, ax = plt.subplots()
    ax.plot(data)
    ax.set_yscale('log')

    works as expected.

  13. petehuang commented on Jan 10, 2017

    @petehuang
    Contributor

    I also cannot reproduce. @theopavlakou did you end up getting this figured out or no?

  14. theopavlakou commented on Jan 11, 2017

    @theopavlakou
    Author

    I didn't figure it out, but I did some workaround from what I remember.

  15. tacaswell commented on Jan 11, 2017

    @tacaswell
    Member

    closing because we can not reproduce 😞

    Please ping to have this re-opened if you can still reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions