예제 #1
0
파일: step40.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable

x0 = Variable(np.array([1, 2, 3]))
x1 = Variable(np.array([10]))
y = x0 + x1

# broadcastできているか確認
print(y)

# 逆伝播してx1の勾配がもとのx1と同じshapeになっているか確認
y.backward()
print(x1.grad)
예제 #2
0
파일: step36.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable

x = Variable(np.array(2.0))
y = x**2
y.backward(create_graph=True)
gx = x.grad
x.cleargrad()

z = gx**3 + y
z.backward()
print(x.grad)
예제 #3
0
파일: step39.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
	import sys, os
	sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F

x = Variable(np.array([[1,2,3], [4,5,6]]))
y = F.sum(x)
#y.backward()
print(y)
#print(x.grad)

x = np.array(([[1,2,3],[4,5,6]]))
y = np.sum(x, axis=0)
print(y)
print(x.shape, '->', y.shape)

x = np.array([[1,2,3], [4,5,6]])
y = np.sum(x, keepdims=True)
print(y)
print(y.shape)  # 2次元のまま

# Dezeroのsum関数でも2つの引数を指定できるように修正
x = Variable(np.array([[1,2,3], [4,5,6]]))
y = x.sum(axis=0)
print(y)

x = Variable(np.random.randn(2,3,4,5))
y = x.sum(keepdims=True)
예제 #4
0
파일: step43.py 프로젝트: EriHata/my_dezero
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F
import matplotlib.pyplot as plt

# toy dataset
np.random.seed(0)
x = np.random.rand(100, 1)
y = np.sin(2 * np.pi * x) + np.random.rand(100, 1)

# 重みの初期化
I, H, O = 1, 10, 1  # input, Hidden, output
W1 = Variable(0.01 * np.random.randn(I, H))
b1 = Variable(np.zeros(H))
W2 = Variable(0.01 * np.random.randn(H, O))
b2 = Variable(np.zeros(0))


# NNの推論関数
def predict(x):
    y = F.linear(x, W1, b1)
    y = F.sigmoid(y)
    y = F.linear(y, W2, b2)
    return y


lr = 0.2
iters = 10000
예제 #5
0
if '__file__' in globals():
	import sys, os
	sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F

x = Variable(np.array([[1,2,3], [4,5,6]]))
print(id(x))
y = F.get_item(x, 1)
print(y)

y.backward()
print(x.grad)
print(y.grad)  # Noneなのでy.shapeの形で自動補完される


# 同じ要素を複数回抜き出す
x = Variable(np.array([[1,2,3], [4,5,6]]))
print(id(x))
indices = np.array([0,0,1])
y = F.get_item(x, indices)
print(y)

Variable.__getitem__ = F.get_item

print('-------------')
y = x[1]
print(y)
print(y.shape)
예제 #6
0
파일: step37.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
	import sys, os
	sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
import matplotlib.pyplot as plt
from my_dezero import Variable
import my_dezero.functions as F

x = Variable(np.array([[1,2,3], [4,5,6]]))
y = F.sin(x)
print(y)

c = Variable(np.array([[10,20,30], [40,50,60]]))
y = x + c
print(y)

x = Variable(np.array([[1,2,3], [4,5,6]]))
c = Variable(np.array([[10,20,30], [40,50,60]]))
t = x + c
y = F.sum(t)

y.backward(retain_grad=True)
print(y.grad)
print(t.grad)
print(x.grad)
print(c.grad)
예제 #7
0
파일: step41.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F

x = Variable(np.random.randn(2, 3))
W = Variable(np.random.randn(3, 4))
# 実装した行列積の関数
y = F.matmul(x, W)
# 逆伝播が正常に動くか確認
y.backward()
print(x)
print(x.grad)
print(x.grad.shape)
print('--------------')
print(y)
print(W.grad)
print(W.grad.shape)
예제 #8
0
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as numpy
from my_dezero import Variable

from my_dezero.utils import get_dot_grap

x0 = Variable(np.array(1.0))
x1 = Variable(np.array(1.0))
y = x0 + x1

# 変数に名前をつける
x0.name = 'x0'
x1.name = 'x1'
y.name = 'y'

txt = get_dot_grap(y, verbose=False)
print(txt)

# dotファイルとして保存
with open('sample.dot', 'w') as o:
    o.write(txt)
예제 #9
0
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
import matplotlib.pyplot as plt
from my_dezero import Variable
import my_dezero.functions as F

x = Variable(np.linspace(-7, 7, 200))
y = F.sin(x)
y.backward(create_graph=True)

logs = [y.data.flatten()]
print(type(y.data.flatten()))  # np.ndarray

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, v, label=labels[i])
plt.legend(loc='lower right')
plt.show()
예제 #10
0
파일: step23.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable

x = Variable(np.array(1.0))
y = (x + 3)**2
y.backward()

print(y)
print(x.grad)
예제 #11
0
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
import matplotlib.pyplot as plt
from my_dezero import Variable
from my_dezero.utils import plot_dot_graph
import my_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 = 2

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

#Drawing a calcuration graph
gx = x.grad
gx.name = 'gx' + str(iters + 1)
plot_dot_graph(gx, verbose=False, to_file='tanh.png')
예제 #12
0
파일: step42.py 프로젝트: EriHata/my_dezero
if '__file__' in globals():
    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F
import matplotlib.pyplot as plt

# toy dataset
np.random.seed(0)
x = np.random.rand(100, 1)
y = 5 + 2 * x + np.random.rand(100, 1)
x, y = Variable(x), Variable(y)

# parameter
W = Variable(np.zeros((1, 1)))
b = Variable(np.zeros(1))


def predict(x):
    y = F.matmul(x, W) + b
    return y


# x0, x1はベクトルを想定
def mean_square_error(x0, x1):
    diff = x0 - x1
    return F.sum(diff**2) / len(diff)

예제 #13
0
파일: step38.py 프로젝트: EriHata/my_dezero
	sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
import matplotlib.pyplot as plt
from my_dezero import Variable
import my_dezero.functions as F

# reshapeについて
x = np.array([[1,2,3], [4,5,6]])
print(x.shape)
y = np.reshape(x, (6,))
# y = x.reshape((6,)) 
# どちらでも結果は一緒
print(y.shape)

x = Variable(np.array([[1,2,3], [4,5,6]]))
y = F.reshape(x, (6,))
y.backward(retain_grad=True)
print(x.grad)

# Variableクラスにreshapeメソッドを追加し
# shapeを可変長引数で取得することでこの書き方が可能になった
x = Variable(np.array([[1,2,3], [4,5,6]]))
y = x.reshape((2,3))
print(y.shape)
z = x.reshape(2,3)
print(z.shape)


# transposeについて
print('----------------------------')
예제 #14
0
# ヘッセ行列とベクトルの結果だけがほしいとき
# ヘッセ行列を直接求めると計算時間がかかるので
# double propagationを使って計算する
# ∇^2f(x)v = ∇(v^T∇f(x))
# ∇(x)はgradf(x)

if '__file__' in globals():
	import sys, os
	sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
import my_dezero.functions as F

# 変数が2つ
x = Variable(np.array([1.0, 2.0]))
v = Variable(np.array([4.0, 5.0]))

# 多変量関数を作成
def f(x):
	t = x ** 2
	y = F.sum(t)
	return t

# ヘッセ行列とベクトルの結果を求めるためにdouble propagationを使う
y = f(x)
y.backward(create_graph=True)
gx = x.grad
x.cleargrad()

z = F.matmul(v, gx)
예제 #15
0
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from my_dezero import Variable
from my_dezero.utils import plot_dot_graph


def goldstein(x, y):
    z = (1 + (x+y+1)**2 * (19 - 14*x + 3*x**2 - 14*y + 6*x*y + 3*y**2)) *\
    (30 + (2*x - 3*y)**2 * (18 - 32*x + 12*x**2 + 48*y - 36*x*y + 27*y**2))
    return z


x = Variable(np.array(1.0))
y = Variable(np.array(1.0))
z = goldstein(x, y)
z.backward()

x.name = 'x'
y.name = 'y'
z.name = 'z'
plot_dot_graph(z, verbose=False, to_file='goldstein.png')
예제 #16
0
파일: step27.py 프로젝트: EriHata/my_dezero
#y.backward()

#print(y.data)
#print(x.grad)

import math


def my_sin(x, threshold=1e-150):
    y = 0
    for i in range(100000):
        c = (-1)**i / math.factorial(2 * i + 1)
        t = c * x**(2 * i + 1)
        y = y + t
        if abs(t.data) < threshold:
            break
    return y


x = Variable(np.array(np.pi / 4))  # 45度
y = my_sin(x)
y.backward()

print(y.data)
print(x.grad)

from my_dezero.utils import plot_dot_graph

x.name = 'x'
y.name = 'y'
plot_dot_graph(y, verbose=False, to_file='my_sin.png')