cro*_*ley 4 matplotlib tensorflow google-colaboratory
我正在使用 Colab 创建一个笔记本,用于训练机器接收字符串并以手写风格输出。我使用这个Jupyter 笔记本作为如何实现此类内容的指南,因为这是我使用机器学习所做的第一件事。
我已将笔记本中的 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))
Run Code Online (Sandbox Code Playgroud)
我面临的问题是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'
Run Code Online (Sandbox Code Playgroud)
小智 9
该错误不是来自 TensorFlow,而是来自 matplotlib。bivariate_normal() 已从版本 3.1.0 中的 matplotlib.mlab 模块中删除。来源
快速而肮脏的解决方案是重新实现已弃用的功能。
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4565 次 |
最近记录: |