예제 #1
0
#!/usr/bin/python3
# coding: utf-8

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

import numpy as np
from dezero import Variable
from 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')
import numpy as np
from dezero import Variable
from dezero.utils import plot_dot_graph
import dezero.functions as f

iterss = 0

if __name__ == '__main__':

    x = Variable(np.array(1))
    y = f.tanh(x)
    x.name = 'x'
    y.name = 'y'
    y.backward(create_graph=True)

    for iters in [0, 1, 2, 3, 4, 5, 6]:
        for i in range(iters):
            gx: Variable = x.grad
            x.clear_grad()
            gx.backward(create_graph=True)

        gx: Variable = x.grad
        gx.name = f'gx{iters + 1}'
        plot_dot_graph(gx, verbose=False, to_file=f'tanh_diff_{iters + 1}.png')
예제 #3
0
 def plot(self, *inputs, to_file='model.png'):
     ys = self.__call__(*inputs)
     return utils.plot_dot_graph(ys, verbose=True, to_file=to_file)
x = Variable(np.array(np.pi / 4))
y = sin(x)
y.backward()
print('--- original sin ---')
print(y.data)
print(x.grad)


def my_sin(x, threshold=0.0001):
    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))
y = my_sin(x)  # , threshold=1e-150)
y.backward()
print('--- approximate sin ---')
print(y.data)
print(x.grad)

x.name = 'x'
y.name = 'y'
plot_dot_graph(y, verbose=False, to_file='my_sin.png')
예제 #5
0
 def plot(self, *inputs, to_file='model.png'):
     y = self.forward(*inputs)
     return utils.plot_dot_graph(y, verbose=True, to_file=to_file)
예제 #6
0
import numpy as np
from dezero.core import Variable
import dezero.functions as F
from dezero.utils import plot_dot_graph


def network():
    x = Variable(np.array(1.0), name="x")
    y = F.exp(x)
    y.name = "y"
    y.backward(create_graph=True)
    gx = x.grad
    gx.name = "gx"
    gx.backward(create_graph=True)
    return gx


if __name__ == "__main__":
    plot_dot_graph(network())
예제 #7
0
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
import matplotlib.pyplot as plt
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 = 6

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='tanh6.png')
예제 #8
0
 def plot(self, *inputs, to_file="model.png"):
     y = self.forward(*inputs)
     return utils.plot_dot_graph(y, verbose=False, to_file=to_file)
if "__file__" in globals():
    import os, sys

    sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

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 = 0

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")
예제 #10
0
    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))
y = my_sin(x)
y.backward()

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

x.name = 'x'
y.name = 'y'
plot_dot_graph(y, verbose=False, to_file='my_sin_thres_1e-4.png')

x = Variable(np.array(np.pi / 4))
y = my_sin(x, threshold=1e-150)
y.backward()

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

x.name = 'x'
y.name = 'y'
plot_dot_graph(y, verbose=False, to_file='my_sin_thres_1e-150.png')
예제 #11
0
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 = 1

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, to_file='tanh.png')
예제 #12
0
'''
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


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


x0 = Variable(np.array(1.0))
x1 = Variable(np.array(1.0))
y = goldstein(x0, x1)
y.backward()

x0.name = 'x0'
x1.name = 'x1'
y.name = 'y'
plot_dot_graph(y)
예제 #13
0
x = Variable(np.array(np.pi / 4))
y = sin(x)
y.backward()
print('--- original sin ---')
print(y.data)
print(x.grad)


def my_sin(x, threshold=0.0001):
    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))
y = my_sin(x)  # , threshold=1e-150)
y.backward()
print('--- approximate sin ---')
print(y.data)
print(x.grad)

x.name = 'x'
y.name = 'y'
plot_dot_graph(y, to_file='my_sin.png')
예제 #14
0
if '__file__' in globals():
    import os
    import sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    import numpy as np
    import matplotlib.pyplot as plt
    from dezero import Variable
    from dezero import setup_variable
    from dezero.utils import plot_dot_graph
    import dezero.functions as F
setup_variable()

if __name__ == '__main__':
    x = Variable(np.array(1.0))
    y = F.tanh(x)
    x.name = 'x'
    y.name = 'y'
    y.backward(create_graph=True)
    iters = 8

    for i in range(iters):
        gx = x.grad
        x.cleargrad()
        gx.backward(create_graph=True)
        # draw graph
        gx.name = 'gx' + str(i + 1)
        plot_dot_graph(gx, verbose=False, to_file='tanh{}.png'.format(str(i)))
예제 #15
0
'''
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


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


x0 = Variable(np.array(1.0))
x1 = Variable(np.array(1.0))
y = goldstein(x0, x1)
y.backward()

x0.name = 'x0'
x1.name = 'x1'
y.name = 'y'
plot_dot_graph(y, verbose=False)
예제 #16
0
    def __init__(self, hidden_size, out_size):
        super().__init__()
        self.rnn = L.RNN(hidden_size)
        self.fc = L.Linear(out_size)

    def reset_state(self):
        self.rnn.reset_state()

    def forward(self, x):
        h = self.rnn(x)
        y = self.fc(h)
        return y


seq_data = [np.random.randn(1, 1) for _ in range(10)]  # ダミーの時系列データ
xs = seq_data[0:-1]
ts = seq_data[1:]  # xsより1ステップ先のデータ
model = SimpleRNN(10, 1)
loss, cnt = 0, 0

for x, t in zip(xs, ts):
    y = model(x)
    loss += F.mean_squared_error(y, t)
    cnt += 1
    if cnt == 1:
        model.cleargrads()
        loss.backward()
        break

plot_dot_graph(loss, verbose=False, to_file='SimpleRNN.png')
예제 #17
0
if '__file__' in globals():
    import os
    import sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from dezero.utils import plot_dot_graph
from dezero import Variable
import dezero.functions as F

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

iters = 5

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='../save/tanh.png')
import numpy as np
from dezero import Variable
from dezero.math_functions import sin
from dezero.utils import plot_dot_graph

if __name__ == '__main__':
    x = Variable(np.array(np.pi / 4))
    y = sin(x)
    y.backward()

    x.name = 'x'
    y.name = 'y'

    plot_dot_graph(y, verbose=False, to_file='sin.png')

    y = sin(x, threshold=1e-150)
    y.backward()

    plot_dot_graph(y, verbose=False, to_file='sin_1e-150.png')