<reponame>taekwang94/code-exercise<filename>DeepLearning_from_Scratch/Chapter4_Training_NN.py
<<<<<<< HEAD
import numpy as np

def cross_entropy_error(y,t):
    if y.ndim==1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

def sum_squares_error(y,t):
    return 0.5*np.sum((y-t)**2)

def numerical_diff(f,x):
    h = 1e-4
    return (f(x+h)-f(x-h))/(2*h)

def func_1(x):
    return 0.01*x**2 + 0.1*x

def numerical_gradient(f,x):
    h = 1e-4
    grad = np.zeros_like(x)

    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val+h
        fxh1 = f(x)

        x[idx] = tmp_val -h
        fxh2 = f(x)

        grad[idx]= (fxh1-fxh2) / (2*h)
        x[idx] = tmp_val
    return grad

def gradient_descent(f,init_x, lr = 0.01, step_num = 100):
    x = init_x

    for i in range(step_num):
        grad = numerical_gradient(f,x)
        x -=lr*grad
    return x
=======
import numpy as np

def cross_entropy_error(y,t):
    if y.ndim==1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

def sum_squares_error(y,t):
    return 0.5*np.sum((y-t)**2)

def numerical_diff(f,x):
    h = 1e-4
    return (f(x+h)-f(x-h))/(2*h)

def func_1(x):
    return 0.01*x**2 + 0.1*x

def numerical_gradient(f,x):
    h = 1e-4
    grad = np.zeros_like(x)

    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val+h
        fxh1 = f(x)

        x[idx] = tmp_val -h
        fxh2 = f(x)

        grad[idx]= (fxh1-fxh2) / (2*h)
        x[idx] = tmp_val
    return grad

def gradient_descent(f,init_x, lr = 0.01, step_num = 100):
    x = init_x

    for i in range(step_num):
        grad = numerical_gradient(f,x)
        x -=lr*grad
    return x
>>>>>>> e8dc90c85b9519d31ca84678bd32c29f74538635

<<<<<<< HEAD
import numpy as np

def cross_entropy_error(y,t):
    if y.ndim==1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

def sum_squares_error(y,t):
    return 0.5*np.sum((y-t)**2)

def numerical_diff(f,x):
    h = 1e-4
    return (f(x+h)-f(x-h))/(2*h)

def func_1(x):
    return 0.01*x**2 + 0.1*x

def numerical_gradient(f,x):
    h = 1e-4
    grad = np.zeros_like(x)

    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val+h
        fxh1 = f(x)

        x[idx] = tmp_val -h
        fxh2 = f(x)

        grad[idx]= (fxh1-fxh2) / (2*h)
        x[idx] = tmp_val
    return grad

def gradient_descent(f,init_x, lr = 0.01, step_num = 100):
    x = init_x

    for i in range(step_num):
        grad = numerical_gradient(f,x)
        x -=lr*grad
    return x
=======
import numpy as np

def cross_entropy_error(y,t):
    if y.ndim==1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

def sum_squares_error(y,t):
    return 0.5*np.sum((y-t)**2)

def numerical_diff(f,x):
    h = 1e-4
    return (f(x+h)-f(x-h))/(2*h)

def func_1(x):
    return 0.01*x**2 + 0.1*x

def numerical_gradient(f,x):
    h = 1e-4
    grad = np.zeros_like(x)

    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val+h
        fxh1 = f(x)

        x[idx] = tmp_val -h
        fxh2 = f(x)

        grad[idx]= (fxh1-fxh2) / (2*h)
        x[idx] = tmp_val
    return grad

def gradient_descent(f,init_x, lr = 0.01, step_num = 100):
    x = init_x

    for i in range(step_num):
        grad = numerical_gradient(f,x)
        x -=lr*grad
    return x
>>>>>>> e8dc90c85b9519d31ca84678bd32c29f74538635