gpt4 book ai didi

tensorflow - 属性错误 : module 'matplotlib.mlab' has no attribute 'bivariate_normal'

转载 作者:行者123 更新时间:2023-12-05 00:55:20 24 4
gpt4 key购买 nike

我正在使用 Colab 创建一个笔记本,用于训练机器接收字符串并以手写方式输出。我正在使用 this Jupyter notebook 作为如何实现这样的东西的指南,因为这是我用机器学习做过的第一件事。

我已将笔记本中的 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 以及后来不支持 bivariate_normal 造成的。我遇到问题的部分是找到解决此问题的方法。我尝试通过尝试诸如“tf.compat.v1.__”之类的东西来依赖较旧的 TensorFlow 版本。我还花了几个小时研究较新的 TensorFlow 版本对应的 bivariate_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。bivariate_normal() 已从 3.1.0 版的 matplotlib.mlab 模块中删除。 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

关于tensorflow - 属性错误 : module 'matplotlib.mlab' has no attribute 'bivariate_normal' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64780530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com