import numpy as np
from study_deepLearning.dataset.mnist import load_mnist

# 평균 제곱 오차
def mean_squared_error(y, t):
    return .5 * np.sum((y-t)**2)

# 예1 : '2'일 확률이 가장 높다고 추정
t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
y = [.1, .05, .6, .0, .05, .1, .0, .1, .0, .0]

print(mean_squared_error(np.array(y), np.array(t)))

# 예2 : '7'일 확률이 가장 높다고 추정
y = [.1, .05, .1, .0, .05, .1, .0, .6, .0, .0]

print(mean_squared_error(np.array(y), np.array(t)))

# 교차 엔트로피 오차
def cross_entropy_error(y ,t):
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

# 미니배치 학습
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)

print(x_train.shape) # (60000, 784)
print(t_train.shape) # (60000, 10)

train_size = x_train.shape[0] # 60000
batch_size = 10
batch_mask = np.random.choice(train_size, batch_size)
print(batch_mask)

x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]

def cross_entropy_error_for_batch(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







