AttributeError:模块'matplotlib.mlab'没有属性'BITVARIAL_NORMAL' [英] AttributeError: module 'matplotlib.mlab' has no attribute 'bivariate_normal'

查看:43
本文介绍了AttributeError:模块'matplotlib.mlab'没有属性'BITVARIAL_NORMAL'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Colab创建一个笔记本,它训练机器接收字符串并以手写样式输出。我使用thisJupyter笔记本作为如何实现此类操作的指南,因为这是我第一次使用机器学习。

我已经将笔记本中的TensorFlow降级到1.15.2,以避免不同版本支持不同属性的问题-主要是因为我引用的上述笔记本是使用TensorFlow 1.X版本创建的,我创建的笔记本使用的是Python 3。

在下面的代码中,我尝试绘制笔划概率的高斯图。

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))

我面临的问题是AttributeError: module 'matplotlib.mlab' has no attribute 'bivariate_normal'。我知道这是由于TensorFlow 2.2.X和更高版本不支持BIBVARIAL_NORMAL。我遇到麻烦的部分是找到一种绕过这个问题的方法。我尝试使用较旧的TensorFlow版本,比如&tf.compat.v1.__";。我还花了几个小时研究较新的TensorFlow版本的BINVARIAL_NORMAL的等价物是什么。到目前为止我还没有运气。

我希望通过发布这篇文章,比我更熟悉机器学习的人可以让我知道解决我遇到的这个问题的方法。

完整的错误消息为:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-174bc4f9cf0b> in <module>()
     22   plt.imshow(Z)
     23 
---> 24 gauss_plot(strokes, "Stroke Probability", figsize=(2*model.ascii_steps,4))

<ipython-input-33-174bc4f9cf0b> in gauss_plot(strokes, title, figsize)
     16 
     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'

推荐答案

错误不是来自TensorFlow,而是来自matplotlib。 在3.1.0版中,已从matplotlib.mlab模块中删除了biariable_Normal()。source

快速解决方案是重新实现已弃用的函数。

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

这篇关于AttributeError:模块&#39;matplotlib.mlab&#39;没有属性&#39;BITVARIAL_NORMAL&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆