Exemplo n.º 1
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')
Exemplo n.º 2
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)
Exemplo n.º 3
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')