oreilly-japan / deep-learning-from-scratch-3

『ゼロから作る Deep Learning ❸』(O'Reilly Japan, 2020)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

step34.py 恒等関数y=xの高階微分が求まらない

opened this issue · comments

step34.pyの高階微分を求めるコードで y=F.sin(x)をy=z*xに変更し、実行するとエラーが出ます。
yの1階微分は求まりますが、2階微分以降(i=2,3に関して)エラー
'NoneType' object has no attribute 'data'
が出てゼロが出力されません。
定置写像の微分が定義されていないことが原因でしょうか?

x = Variable(np.linspace(-7, 7, 200))
#y = F.sin(x)
z = np.array(1.0)
y = z*x
y.backward(create_graph=True)

logs = [y.data.flatten()]

for i in range(3):
logs.append(x.grad.data.flatten())
gx = x.grad
x.cleargrad()
gx.backward(create_graph=True)

labels = ["y=sin(x)", "y'", "y''", "y'''"]
for i, v in enumerate(logs):
plt.plot(x.data, logs[i], label=labels[i])
plt.legend(loc='lower right')
plt.show()