Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using Colab to create a notebook that trains a machine to take in a string and output it in a handwritten style. I'm using this Jupyter notebook as a guide for how to implement something like this because this is the first thing I have ever done with machine learning.

I have downgraded TensorFlow in the notebook to 1.15.2 to avoid the issue of different versions supporting different attributes - mainly because the above notebook I am referencing was created using TensorFlow version 1.X. The notebook I am creating uses Python 3.

In the code below I am trying to do a gaussian plot of stroke probability.

def gauss_plot(strokes, title, figsize=(20,2)):
  plt.figure(figsize=figsize)
  buff = 1
  epsilon = 1e-4
  minx= np.min(strokes[:,0])-buff
  maxx = np.max(strokes[:,0])+buff
  miny = np.min(strokes[:,1])-buff
  maxy = np.max(strokes[:,1])+buff
  delta = abs(maxx-minx)/400. ;
  x = np.arange(minx, maxx, delta)
  y = np.arange(miny, maxy, delta)
  X, Y = np.meshgrid(x, y)
  Z = np.zeros_like(X)    
  for i in range(strokes.shape[0]):
    gauss = mlab.bivariate_normal(X, Y, sigmax=strokes[i,2], sigmay=strokes[i,3], mux=strokes[i,0], muy=strokes[i,1], sigmaxy=0.0)
    # gauss = mlab.np.compat.v1.biv_normal(X, Y, sigmax=strokes[i,2], sigmay=strokes[i,3], mux=strokes[i,0], muy=strokes[i,1], sigmaxy = 0.0 )
  Z += gauss * np.power(strokes[i,3] + strokes[i,2], .4)
  plt.title(title, fontsize=20)
  plt.imshow(Z)
gauss_plot(strokes, "Stroke Probability", figsize=(2*model.ascii_steps,4))

The issue I am facing is AttributeError: module 'matplotlib.mlab' has no attribute 'bivariate_normal'. I know that this is due to TensorFlow 2.2.X and later not supporting bivariate_normal. The part that I am having trouble with is finding a way around this issue. I tried relying upon the older TensorFlow version by trying things such as "tf.compat.v1.__". I also spent a few hours researching what the equivalent of bivariate_normal is for the newer TensorFlow versions. So far I have had no luck.

I am hoping that by posting this someone who is more familiar with machine learning than myself might be able to let me know a way to fix this issue I am having.

The full error message is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-174bc4f9cf0b> in <module>()
     22   plt.imshow(Z)
---> 24 gauss_plot(strokes, "Stroke Probability", figsize=(2*model.ascii_steps,4))
<ipython-input-33-174bc4f9cf0b> in gauss_plot(strokes, title, figsize)
     17   for i in range(strokes.shape[0]):
---> 18     gauss = mlab.bivariate_normal(X, Y, sigmax=strokes[i,2], sigmay=strokes[i,3], mux=strokes[i,0], muy=strokes[i,1], sigmaxy=0.0)
     19     # gauss = mlab.np.compat.v1.biv_normal(X, Y, sigmax=strokes[i,2], sigmay=strokes[i,3], mux=strokes[i,0], muy=strokes[i,1], sigmaxy = 0.0 )
     20   Z += gauss * np.power(strokes[i,3] + strokes[i,2], .4)
AttributeError: module 'matplotlib.mlab' has no attribute 'bivariate_normal'

The error is not from TensorFlow but from matplotlib. bivariate_normal() has been removed from matplotlib.mlab module in version 3.1.0. source

Quick-and-dirty solution is to reimplement the deprecated function.

def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0,
                 mux=0.0, muy=0.0, sigmaxy=0.0):
    Bivariate Gaussian distribution for equal shape *X*, *Y*.
    See `bivariate normal
    <http://mathworld.wolfram.com/BivariateNormalDistribution.html>`_
    at mathworld.
    Xmu = X-mux
    Ymu = Y-muy
    rho = sigmaxy/(sigmax*sigmay)
    z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)
    denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)
    return np.exp(-z/(2*(1-rho**2))) / denom

credit

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.