Coder Social home page Coder Social logo

oreilly-japan / deep-learning-from-scratch-3 Goto Github PK

View Code? Open in Web Editor NEW
677.0 40.0 274.0 14.52 MB

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

License: MIT License

Python 100.00%
deep-learning nueral-networks gpu autograd automatic-differentiation python

deep-learning-from-scratch-3's Introduction

本書概要

本書では「DeZero」というディープラーニングのフレームワークを作ります。DeZeroは本書オリジナルのフレームワークです。最小限のコードで、フレームワークのモダンな機能を実現します。本書では、この小さな——それでいて十分にパワフルな——フレームワークを、全部で60のステップで完成させます。それによって、PyTorch、TensorFlow、Chainerなどの現代のフレームワークに通じる深い知識を養います。

pypi MIT License Build Status

ニュース

【試し読み】本書の一部をオンラインで公開しています。 https://koki0702.github.io/dezero-book/

ファイル構成

フォルダ名 説明
dezero DeZeroのソースコード
examples DeZeroを使った実装例
steps 各stepファイル(step01.py ~ step60.py)
tests DeZeroのユニットテスト

必要な外部ライブラリ

本書で使用するPytnonのバージョンと外部ライブラリは下記の通りです。

またオプションとして、NVIDIAのGPUで実行できる機能も提供します。その場合は下記のライブラリが必要です。

  • CuPy (オプション)

実行方法

本書で説明するPythonファイルは、主にstepsファルダにあります。 実行するためには、下記のとおりPythonコマンドを実行します(どのディレクトリからでも実行できます)。

$ python steps/step01.py
$ python steps/step02.py

$ cd steps
$ python step31.py

デモ

DeZeroの他の実装例はexamplesにあります。

正誤表

本書の正誤情報は、🔎 正誤表ページに掲載しています。

正誤表ページに掲載されていない誤植や間違いなどを見つけた方は、📧 [email protected]までお知らせください。

deep-learning-from-scratch-3's People

Contributors

aha-oretama avatar c-bata avatar ftnext avatar koki0702 avatar miyagawa-orj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deep-learning-from-scratch-3's Issues

There are some typos in step 59, 60 (class RNN(Layer) and class BetterRNN(Model) )

class RNN(Layer):
,,,
def init(self, hidden_size, in_size=None):
...
self.x2h = Linear(hidden_size, in_size=in_size)
self.h2h = Linear(hidden_size, in_size=in_size, nobias=True) #here is problematic

if we code my_rnn = RNN(10, 1), then hidden_size =10, in_size =1
and this will not work.
since h_t = h_t-1 * h2h will raise error (failed to shape match for matrix multiplication of 1 x 10 * 1 x 10)

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

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

I can't implement STEP 23 with Google Colab.

I can't implement STEP 23 with Google Colab.
Even if I input the same as the source code, the module cannot be imported.
Any good guides or tutorials on this?

I still have a long way to go, but I can't go any further. Please help me

内包表記の変数名が一貫していない

self.outputs = [weakref.ref(y) for y in outputs]

self.outputs = [weakref.ref(output) for output in outputs]

書籍のほうでは、後者になっています。おそらく、前者が直し忘れだと思われます。

それに関連してですが、内包表記の変数名が一貫していないのは感心しません。例えば、

xs = [x.data for x in inputs]

こういうところで、変数名を何故xにするのでしょうか。(inputにしないのは何故なのか。outputsのときだけ変数名をoutputとするのは何故なのか) 一貫性がなくて見ていて気持ち悪いです。

ついでに書籍のほう、p.249

   y = self.outputs[0]()

ここ、weakrefだから"()"をつけていることを書籍のほうのソースコード上にも"#weakref"のようにコメントがついていたほうが親切だと思います。

それから、

   x, = self.inputs

tailing commaは、このコードを読まされる側としては見落としやすく、バグの原因になりやすいため、括弧をつけたほうがよろしいです。
cf.https://www.python.org/dev/peps/pep-0008/#when-to-use-trailing-commas

例)

   (x,) = self.inputs

Implementation for mean absolute error?

Thank you for providing such a great library for studying deep learning from scratch.

I was wondering about how to implement a MeanAbsoluteError with dezero. I found that there is a MeanSquaredError, but I have not found anything for MeanAbsoluteError. Any tips on creating one? Thank you so much!

Variableクラスのmatmulメソッドの設定でmatmaulとtypoしている

現状

dezero/core.py の 345行目

Variable.matmaul = dezero.functions.matmul

def setup_variable():
    # 省略

    Variable.matmaul = dezero.functions.matmul
    # 省略

影響

ステップ41の例をVariableインスタンスのメソッドで実行しようとすると、
typoが原因で、matmulメソッドの呼び出しでエラーが発生します。
※masterブランチをcloneし、setup.pyを実行して環境構築しています

>>> import numpy as np
>>> from dezero import Variable
>>> x = Variable(np.random.randn(2, 3))
>>> W = Variable(np.random.randn(3, 4))
>>> # y = dezero.functions.matmul(x, W) 相当のコード
>>> y = x.matmul(W)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Variable' object has no attribute 'matmul'
>>> y = x.matmaul(W)
>>> y.shape
(2, 4)

修正

def setup_variable():
    # 省略

    Variable.matmul = dezero.functions.matmul
    # 省略

How to draw dot graph for higher order differentiation by tanh function

I want to visualize below graph using second order differentiation by tanh function.
스크린샷 2022-07-09 오후 4 46 55

To realize this graph, I tried to the source code that you gave us. But this code only gives me the result that just first order differentiation graph by tanh. I wonder how to draw first, second, third, ..., and higher order differentiation. plz give me some guide. Thanks.

'''
Need the dot binary from the graphviz package (www.graphviz.org).
'''
import numpy as np
from dezero import Variable
from dezero.utils import plot_dot_graph
import dezero.functions as F

x = Variable(np.array(1.0))
y = F.tanh(x)
x.name = 'x'
y.name = 'y'
y.backward(create_graph=True)

iters = 3

for i in range(iters):
    gx = x.grad
    x.cleargrad()
    gx.backward(create_graph=True)

gx = x.grad
gx.name = 'gx' + str(iters + 1)
plot_dot_graph(gx, verbose=False, to_file='tanh.png')

Implementation ideas of backpropagation of concat operator

This book is the best deep learning book I have ever seen. With your help, I also wrote a framework with personal characteristics.

At the end of the study of the whole book, I have one more technical detail that I would like to ask you about, which is the frequently used "concat" operator. Its backpropagation involves the reverse splitting of gradient, which I really can't figure out. , can you provide some ideas or add this method to dezero, thank you.

p483のコードの記述ミス

p483の下段のコードですが、
ts = [example[0] for example in train_set]
は、
ts = [example[1] for example in train_set]
の間違いではないでしょうか?

オライリー・ジャパンへの連絡先のメールアドレスの@が全角のため、連絡する際に混乱しました

現状

README

正誤表ページに掲載されていない誤植や間違いなどを見つけた方は、✉️ japan@oreilly.co.jpまでお知らせください。

Errata

本ページに掲載されていない誤植など間違いを見つけた方は、japan@oreilly.co.jpまでお知らせください。

問題

上記のメールアドレスは2つとも@が全角になっています。
メールアドレスをコピペしてEメールを送ろうとしたところ、
「メールアドレスが認識できない」とエラーが表示され、送れませんでした

The address "japan@oreilly.co.jp" in the "To" field was not recognized. (Gmailのエラーメッセージ)

Workaround

オライリー・ジャパンが公開しているメールアドレスと比較したところ、@に全角・半角の違いがあることがわかりました
https://www.oreilly.co.jp/company/

オライリー・ジャパンが公開しているメールアドレス(@が半角のもの)を使用したところ、Eメールを送信できました

要望

@を半角にしていただけますと、誤植を報告する人が私のように混乱することはなくなると考えます。

意図を持って@を全角にしているのでしたら
「※@は半角に変換してからメールを送信してください」
のような注釈を記載すると、誤植を報告する人にとって親切と考えます

step34.pyのflatten()について

step34.pyのコードで、y.dataとx.grad.dataにflatten()が実行されていますが、当該処理は必要でしょうか?
y及びx.gradが保持するdataはいずれも最初から1次元配列だと思うのですが。

pip install -e .でインストールできない

本の中でpip install -e .しているかは未確認です(そこまで読めていません)。
別の方法を使っている場合は無視してください

環境

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.14.6
BuildVersion:	18G103
$ python3.7 -V
Python 3.7.3

手順

$ git clone [email protected]:ftnext/deep-learning-from-scratch-3.git
$ cd deep-learning-from-scratch-3/
$ python3.7 -m venv env
$ . env/bin/activate
(env) $ pip install -e .

期待結果

dezeroがインストールできる

実際

以下のエラーによりdezeroがインストールできませんでした

Obtaining file:///Users/.../deep-learning-from-scratch-3
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/.../deep-learning-from-scratch-3/setup.py", line 2, in <module>
        from dezero import __version__
      File "/Users/.../deep-learning-from-scratch-3/dezero/__init__.py", line 16, in <module>
        from dezero.core import Variable
      File "/Users/.../deep-learning-from-scratch-3/dezero/core.py", line 2, in <module>
        import numpy as np
    ModuleNotFoundError: No module named 'numpy'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /Users/.../deep-learning-from-scratch-3/

from dezero import __version__の指定によってdezeroのコードが一度実行されるので、
numpyがあらかじめ入った環境でないと pip install -e . できないように思われます。

暫定的な解決策

from dezero import __version__を消し、setup(version='0.0.12',と指定しました。
ModuleNotFoundError: No module named 'numpy'は発生せず、pip install -e .できました

https://github.com/oreilly-japan/deep-learning-from-scratch-3/blob/master/dezero/models.pyのResNetの記述について

https://github.com/oreilly-japan/deep-learning-from-scratch-3/blob/master/dezero/models.pyのResNetについてですが、
steps/step58.pyの内容を下記のように書き換えて流すと、
ここから、

if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
from PIL import Image
import dezero
from dezero.models import VGG16, ResNet

url = 'https://github.com/oreilly-japan/deep-learning-from-scratch-3/raw/images/zebra.jpg'
img_path = dezero.utils.get_file(url)
img = Image.open(img_path)

x =VGG16.preprocess(img)
print(x.shape)
x = x[np.newaxis]

model = ResNet(pretrained=True)
with dezero.test_mode():
y = model(x)
predict_id = np.argmax(y.data)
model.plot(x, to_file='ResNet152.pdf')
labels = dezero.datasets.ImageNet.labels()
print(labels[predict_id])

ここまで、

下記のようなエラーが出ます。
ここから、

Traceback (most recent call last):
File "step58_2.py", line 17, in 
model = ResNet(pretrained=True)
File "../dezero/models.py", line 138, in init
self.res2 = BuildingBlock(block[0], 64, 64, 256, 1)
File "../dezero/models.py", line 196, in init
downsample_fb)
File "../dezero/models.py", line 236, in init
self.conv1 = L.Conv2d(in_channels, mid_channels, 1, stride_1x1, 0, nobias=True)
TypeError: init() got multiple values for argument ‘nobias'

ここまで、

エラーの内容を調べてみるとResNetの記述でのL.Conv2dの記述で、in_channelsを記述してしまっているためだとわかりました。
一応、下記の箇所を以下のように変更するとうまく動作してくれるようなのですが、この変更で問題ないでしょうか?
ここから、

p134 self.conv1 = L.Conv2d(3, 64, 7, 2, 3) -> self.conv1 = L.Conv2d(64, 7, 2, 3)
p223 self.conv1 = L.Conv2d(in_channels, mid_channels, 1, stride_1x1, 0, nobias=True) ->
self.conv1 = L.Conv2d(mid_channels, 1, stride_1x1, 0, nobias=True)
p226 self.conv2 = L.Conv2d(mid_channels, mid_channels, 3, stride_3x3, 1, nobias=True) ->
self.conv2 = L.Conv2d(mid_channels, 3, stride_3x3, 1, nobias=True)
p229 self.conv3 = L.Conv2d(mid_channels, out_channels, 1, 1, 0, nobias=True) ->
self.conv3 = L.Conv2d(out_channels, 1, 1, 0, nobias=True)
p231 self.conv4 = L.Conv2d(in_channels, out_channels, 1, stride, 0, nobias=True) ->
self.conv4 = L.Conv2d(out_channels, 1, stride, 0, nobias=True)
p253 self.conv1 = L.Conv2d(in_channels, mid_channels, 1, 1, 0, nobias=True) ->
self.conv1 = L.Conv2d(mid_channels, 1, 1, 0, nobias=True)
p255 self.conv2 = L.Conv2d(mid_channels, mid_channels, 3, 1, 1, nobias=True) ->
self.conv2 = L.Conv2d(mid_channels, 3, 1, 1, nobias=True)
p257 self.conv3 = L.Conv2d(mid_channels, in_channels, 1, 1, 0, nobias=True) ->
self.conv3 = L.Conv2d(in_channels, 1, 1, 0, nobias=True)

ここまで、

ご確認ください。

Typo in Wiki DeZeroをiPhoneで動かす

iPhoone というtypoがありました

iPhoneのアプリに、Pythonistaがあります。このPythonistaというアプリを使うと、iPhoone上でPythonを動かすことができます。

修正後(変更箇所
→ iPhoneのアプリに、Pythonistaがあります。このPythonistaというアプリを使うと、iPhone上でPythonを動かすことができます。

wikiへのプルリクエストの送り方がよくわからなかったので、Issueでお知らせします

linear_simple関数について(ステップ43)

この関数の最初に、as_variable関数が実行されていますが、これは必要でしょうか?その直後のmatmul関数は、MatMulクラスが実行されると思いますが、このクラスはFunctionクラスを継承しているため、このクラスの__call__メソッドが呼ばれ、ここで自動的に引数に対してas_variable関数が実行されるように思うのですが。

Momentum SGDについて(step46)

p.359の Momentum SGDクラスのupdate_one関数についてです。
vという変数にself.vsというインスタンス変数の特定の要素(self.vs[v_key])が代入され、その後変数vのほうが更新されていますが、この方法の場合、インスタンス変数である辞書(self.vs)の持つ値は変わらない、つまり最初に呼ばれた時に設定した0のままになってしまうのではないでしょうか?
その結果、forループでupdate_one関数が呼ばれる度に変数vが0に更新されてしまい、学習が上手く進まないように思います。
理解が不十分で的外れな指摘となっていたら申し訳ありません。

Examples code(mnist.py) doesn't work

This Link is result of executing the examples_code(mnist.py).
GoogleColabratory - deep-learning-from-scratch-3/try_mnist.py

Enviroment info

import dezero
dezero.__version__
# '0.0.11'

import sys
sys.version
# '3.6.8 (default, Oct  7 2019, 12:59:55) \n[GCC 8.3.0]'

Execute Command

python examples/mnist.py

Output

Downloading MNIST(train-images-idx3-ubyte.gz) ... Done
Downloading MNIST(train-labels-idx1-ubyte.gz) ... Done
Downloading MNIST(t10k-images-idx3-ubyte.gz) ... Done
Downloading MNIST(t10k-labels-idx1-ubyte.gz) ... Done
Traceback (most recent call last):
  File "examples/mnist.py", line 40, in <module>
    acc = F.accuracy(y, t)
  File "/usr/local/lib/python3.6/dist-packages/dezero-0.0.11-py3.6.egg/dezero/functions.py", line 367, in accuracy
    return Variable(acc)
  File "/usr/local/lib/python3.6/dist-packages/dezero-0.0.11-py3.6.egg/dezero/core.py", line 49, in __init__
    raise TypeError('{} is not supported'.format(type(data)))
TypeError: <class 'numpy.float64'> is not supported

MeanSquaredErrorクラスについて(step42)

p.323のMeanSquaredErrorクラスでは、backwardでgyに対してbroadcast_to関数が実行されていますが、これは必要でしょうか?
その下でgx0が計算される際、gy*diffの部分はmul関数のforwardが呼ばれることになると思いますが、これはndarrayに対して計算が行われるため、gyは自動的に拡張されると思うのですが、どうでしょうか?

SinCurveクラスについて(step59)

P.483のSinCurveクラスについて質問です。
①図を描画する際に定義した「ts」は、exampleの1次元目の要素を取り出す(つまり、example[1]とする)べきではないでしょうか?
②P.484の実行結果について、1番目のデータの教師データ(0.04656735)と2番目のデータの入力データ(-0.02867358)が一致していないのは何故でしょうか?

VGG16をResNetにしたらエラー(ステップ58)

とても良い書籍であり、たいへん勉強になっており感謝しています。

さて、本文では触れられていませんが、ステップ58においてVGG16の代りにResNetを
試したところ、以下のようなエラーが出て動作しませんでした。
dezero.modelsのResNetクラスに何か問題があるかもしれません。

以下、内容:

まず、書籍のp.472~473のコードは正常に動作しました。
runfile('C:/Users//DeZero_steps_after58/DeZero_step58.py', wdir='C:/Users//DeZero_steps_after58')
zebra

dezero.modelsの中のResNetクラスを見て、VGG16と同様な入力を与えればそのまま
動作するのではないかと考えて、上記のコードにおいて、モデルをResNetに変更
してみました。画像の前処理は共通と考えて、VGG16.preprocess(img)のままとしました。

import numpy as np
from PIL import Image
import dezero
from dezero.models import VGG16, ResNet50

url ='https://github.com/oreilly-japan/deep-learning-from-scratch-3/' 'raw/images/zebra.jpg'
img_path = dezero.utils.get_file(url)
img = Image.open(img_path)

x = VGG16.preprocess(img)
x = x[np.newaxis]

#model = VGG16(pretrained=True)
model = ResNet(pretrained=True)
with dezero.test_mode():
y = model(x)
predict_id = np.argmax(y.data)

model.plot(x, to_file='vgg.pdf')
labels = dezero.datasets.ImageNet.labels()
print(labels[predict_id])

つぎのエラーが出ました。
File "C:\Users*****\anaconda3\lib\site-packages\dezero\models.py", line 235, in init
nobias=True)
TypeError: init() got multiple values for argument 'nobias'

BottleneckAとBottleneckB中のConv2dの引数に問題があるようなので、入力サイズに相当する
引数には in_channels というキーワードを使うように変更してみました。次はその例です。
  self.conv1 = L.Conv2d(mid_channels, 1, stride_1x1, 0,
  nobias=True, in_channels=in_channels)

このことによって、エラーは出ずに動作するようになりました。しかし、zebraという正しい答え
にはならず、つぎのように、black grouse になりました。
runfile('C:/Users//DeZero_steps_after58/DeZero_step58.py', wdir='C:/Users//DeZero_steps_after58')
black grouse

ちなみに、ResNet50にしたところ、答えは vultureになりました。
ResNetクラスのコードか、学習済みのパラメータに問題があるかもしれません。
ご検討をお願いします。
以上

https://github.com/oreilly-japan/deep-learning-from-scratch-3/tree/master/dezero/models.pyのResNetの記述について

model = ResNet(pretrained=True)
with dezero.test_mode():
y = model(x)
predict_id = np.argmax(y.data)
model.plot(x, to_file='ResNet152.pdf')
labels = dezero.datasets.ImageNet.labels()
print(labels[predict_id])
ここまで、

下記のようなエラーが出ます。
ここから、
Traceback (most recent call last):
File "step58_2.py", line 17, in
model = ResNet(pretrained=True)
File "../dezero/models.py", line 138, in init
self.res2 = BuildingBlock(block[0], 64, 64, 256, 1)
File "../dezero/models.py", line 196, in init
downsample_fb)
File "../dezero/models.py", line 236, in init
self.conv1 = L.Conv2d(in_channels, mid_channels, 1, stride_1x1, 0, nobias=True)
TypeError: init() got multiple values for argument ‘nobias'
ここまで、

エラーの内容を調べてみるとResNetの記述でのL.Conv2dの記述で、in_channelsを記述してしまっているためだとわかりました。
一応、下記の箇所を以下のように変更するとうまく動作してくれるようなのですが、この変更で問題ないでしょうか?
ここから、
p134 self.conv1 = L.Conv2d(3, 64, 7, 2, 3) -> self.conv1 = L.Conv2d(64, 7, 2, 3)
p223 self.conv1 = L.Conv2d(in_channels, mid_channels, 1, stride_1x1, 0, nobias=True) ->
self.conv1 = L.Conv2d(mid_channels, 1, stride_1x1, 0, nobias=True)
p226 self.conv2 = L.Conv2d(mid_channels, mid_channels, 3, stride_3x3, 1, nobias=True) ->
self.conv2 = L.Conv2d(mid_channels, 3, stride_3x3, 1, nobias=True)
p229 self.conv3 = L.Conv2d(mid_channels, out_channels, 1, 1, 0, nobias=True) ->
self.conv3 = L.Conv2d(out_channels, 1, 1, 0, nobias=True)
p231 self.conv4 = L.Conv2d(in_channels, out_channels, 1, stride, 0, nobias=True) ->
self.conv4 = L.Conv2d(out_channels, 1, stride, 0, nobias=True)
p253 self.conv1 = L.Conv2d(in_channels, mid_channels, 1, 1, 0, nobias=True) ->
self.conv1 = L.Conv2d(mid_channels, 1, 1, 0, nobias=True)
p255 self.conv2 = L.Conv2d(mid_channels, mid_channels, 3, 1, 1, nobias=True) ->
self.conv2 = L.Conv2d(mid_channels, 3, 1, 1, nobias=True)
p257 self.conv3 = L.Conv2d(mid_channels, in_channels, 1, 1, 0, nobias=True) ->
self.conv3 = L.Conv2d(in_channels, 1, 1, 0, nobias=True)
ここまで、

ご確認ください。

step21: __array_priority__ を省略してもVariableの __add__ が実行される。

step 21で次のようにnumpy ndarrayを左項においた場合には、numpy ndarrayの __add__ が呼ばれるため問題が残っているという説明があるのですが __array_priority__ を指定する前から問題なく動いていました。pdbでみると Variable の __add__() が呼ばれていることが確認できました。

Dropboxの方にもコメントしたのですが、自分のコードが間違えてる可能性もあるかなと思ったので、再現コードをこちらに載せています。

x = Variable(np.array(1.0))
y = np.array(2.0) + x
print(y)
再現用ソースコードと実行結果
import weakref
import numpy as np
import contextlib


class Config:
    enable_backprop = True


@contextlib.contextmanager
def using_config(name, value):
    old_value = getattr(Config, name)
    setattr(Config, name, value)
    try:
        yield
    finally:
        setattr(Config, name, old_value)


def no_grad():
    return using_config('enable_backprop', False)


class Variable:
    def __init__(self, data):
        if data is not None:
            if not isinstance(data, np.ndarray):
                raise TypeError('{} is not supported'.format(type(data)))

        self.data = data
        self.grad = None
        self.creator = None
        self.priority = 0

    @property
    def shape(self):
        return self.data.shape

    @property
    def ndim(self):
        return self.data.ndim

    @property
    def size(self):
        return self.data.size

    @property
    def dtype(self):
        return self.data.dtype

    @property
    def array(self):
        return self.data

    def __len__(self):
        return len(self.data)

    def __repr__(self):
        if self.data is None:
            return 'variable(None)'
        p = str(self.data).replace('\n', '\n' + ' ' * 9)
        return 'variable(' + p + ')'

    def set_creator(self, func):
        self.creator = func
        self.priority = func.priority + 1

    def cleargrad(self):
        self.grad = None

    def backward(self, retain_grad=False):
        if self.grad is None:
            self.grad = np.ones_like(self.data)

        funcs = []
        seen_set = set()

        def add_func(f):
            if f not in seen_set:
                funcs.append(f)
                seen_set.add(f)
                funcs.sort(key=lambda x: x.priority)

        add_func(self.creator)

        while funcs:
            f = funcs.pop()
            gys = [output().grad for output in f.outputs]  # output is weakref
            gxs = f.backward(*gys)
            if not isinstance(gxs, tuple):
                gxs = (gxs,)

            for x, gx in zip(f.inputs, gxs):
                if x.grad is None:
                    x.grad = gx
                else:
                    x.grad = x.grad + gx

                if x.creator is not None:
                    add_func(x.creator)

            if not retain_grad:
                for y in f.outputs:
                    y().grad = None  # y is weakref


def as_variable(obj):
    if isinstance(obj, Variable):
        return obj
    return Variable(obj)


def as_array(x):
    if np.isscalar(x):
        return np.array(x)
    return x


class Function:
    def __call__(self, *inputs):
        inputs = [as_variable(x) for x in inputs]

        xs = [x.data for x in inputs]
        ys = self.forward(*xs)
        if not isinstance(ys, tuple):
            ys = (ys,)
        outputs = [Variable(as_array(y)) for y in ys]

        if Config.enable_backprop:
            self.priority = max([x.priority for x in inputs])
            for output in outputs:
                output.set_creator(self)
            self.inputs = inputs
            self.outputs = [weakref.ref(output) for output in outputs]

        return outputs if len(outputs) > 1 else outputs[0]

    def forward(self, xs):
        raise NotImplementedError()

    def backward(self, gys):
        raise NotImplementedError()


class Add(Function):
    def forward(self, x0, x1):
        y = x0 + x1
        return y

    def backward(self, gy):
        return gy, gy


def add(x0, x1):
    x1 = as_array(x1)
    return Add()(x0, x1)


class Mul(Function):
    def forward(self, x0, x1):
        y = x0 * x1
        return y

    def backward(self, gy):
        x0, x1 = self.inputs[0].data, self.inputs[1].data
        return gy * x1, gy * x0


def mul(x0, x1):
    x1 = as_array(x1)
    return Mul()(x0, x1)


Variable.__add__ = add
Variable.__radd__ = add
Variable.__mul__ = mul
Variable.__rmul__ = mul

x = Variable(np.array(1.0))
y = np.array(2.0) + x
print(y)
$ python step21.py 
variable(3.0)

When can I get the chinese version or english version ?

Hi, I am a reader of your books, I have read DL from scrach 1st and 2nd edition, but I can not read japan laguage. I hope read this book as soon as possible. Your book help me a lot in deep learning, thank you very much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.