动画.FuncAnimation值错误:关闭fi上的I/O操作

2024-06-06 01:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在复制下面的代码时遇到了困难,虽然我了解梯度下降的工作原理,但我不知道有什么问题动画.FuncAnimation功能。我花了4个小时来解决这个问题,但不幸的是我没能找到解决方案。下面是我试图复制的代码(我使用jupyter笔记本)

%matplotlib inline
import math,os,sys,numpy as np
from numpy.random import random
from matplotlib import pyplot as plt, rcParams, animation, rc
from __future__ import print_function, division
from ipywidgets import interact, interactive, fixed 
from ipywidgets.widgets import *
rc('animation', html='html5')
rcParams['figure.figsize'] = 3,3
%precision 4
np.set_printoptions(precision=4, linewidth=100)

def lin(a,x,b): return a * x + b

a = 3.
b = 8.
n = 30

x = random(n)
y = lin(a,x,b)

plt.scatter(x,y)

def sse(y, y_pred): return ((y-y_pred)**2).sum()
def loss(y, a, x, b): return sse(y, lin(a, x, b))
def avg_loss(y, a, x, b): return np.sqrt(loss(y, a, x, b)/n)

a_guess = -1
b_guess = 1
avg_loss(y, a_guess, x, b_guess)

lr = 0.01
#d[(y-(a*x+b))**2, b] = 2 (y_pred - y)
#d[(y -(a*x+b)) **2, a] = x * dy/db

def upd():
    global a_guess, b_guess
    y_pred = lin(a_guess, x, b_guess)
    dydb = 2 * (y_pred - y)
    dyda = x * dydb
    a_guess -= lr*dyda.mean()
    b_guess -= lr*dydb.mean()


fig = plt.figure(dpi=100, figsize=(5,5))
plt.scatter(x,y)
line, = plt.plot(x, lin(a_guess, x, b_guess))
plt.close()

def animate(i):
    line.set_ydata(lin(a_guess, x, b_guess))
    for i in range(10): upd()
    return line,


ani = animation.FuncAnimation(fig, animate, np.arange(0, 40), interval=100)
ani

但当我运行它时,我得到以下错误,

^{pr2}$

寻求帮助!!!

提前感谢:)


Tags: fromimportreturndefnplinepltrandom
3条回答

我也有同样的问题。在

我的解决方案是安装最新的ffmpeg

sudo apt-get update install ffmpeg

我在ubuntu18.04.2上也遇到了同样的问题。我安装了最新的ffmpeg版本后,删除了conda一,它工作得很好

我也有同样的问题。我使用的是ffmpeg,它是我用conda安装的。我不得不用以下方法去掉它:

conda remove ffmpeg

然后我用apt安装了它:

^{pr2}$

似乎康达的版本有问题。在

相关问题 更多 >