import numpy as np
y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]
t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
def mean_square_error(y, t):
return 0.5 * np.sum((y - t) ** 2)
mean_square_error(np.array(y), np.array(t))
0.097500000000000031
y2 = [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]
mean_square_error(np.array(y2), np.array(t))
0.59750000000000003
def cross_entropy_error(y, t):
delta = 1e-7
return -np.sum(t * np.log(y + delta))
cross_entropy_error(np.array(y), np.array(t))
0.51082545709933802
cross_entropy_error(np.array(y2), np.array(t))
2.3025840929945458
from dataset.mnist import load_mnist
(x_train, t_train), (x_test, t_test) = \
load_mnist(normalize=True, one_hot_label=True)
x_train.shape
(60000, 784)
t_train.shape
(60000, 10)
train_size = x_train.shape[0]
batch_size = 10
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
x_batch
array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
t_batch
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]])
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.reshape(1, y.size)
delta = 1e-7
batch_size = y.shape[0]
return -np.sum(t * np.log(y + delta)) / batch_size
cross_entropy_error(np.array(y), np.array(t))
0.51082545709933802
y
[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]
def numerical_diff(f, x):
h = 1e-4
return (f(x+h) - f(x-h)) / (2*h)
def function_1(x):
return 0.01*x**2 + 0.1*x
numerical_diff(function_1, 5)
0.1999999999990898
numerical_diff(function_1, 10)
0.2999999999986347
def function_2(x0, x1):
return x0**2 + x1**2
def function_tmp1(x0):
return function_2(x0, 4.0)
numerical_diff(function_tmp1, 3.0)
6.00000000000378
def function_tmp2(x1):
return function_2(3.0, x1)
numerical_diff(function_tmp2, 4.0)
7.999999999999119