损失函数
import numpy as np
# 均方误差
def mean_squared_error(y, t):
return 0.5 * np.sum((y - t)**2)
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]
print(mean_squared_error(np.array(y),np.array(t))) # 0.09750000000000003
y2 = [0.1, 0.05, 0.1,0.0,0.05,0.1,0.0,0.6,0.0,0.0]
print(mean_squared_error(np.array(y2),np.array(t))) # 0.5975
# 交叉熵误差函数
def cross_entropy_error(y, t):
delta = 1e-7
return -np.sum(t * np.log(y + delta))
print(cross_entropy_error(np.array(y),np.array(t))) # 0.510825457099338
print(cross_entropy_error(np.array(y2),np.array(t))) # 2.302584092994546
三维图像
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
# Make data.
X = np.arange(-3, 3, 0.25)
Y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(X, Y)
# R = np.sqrt(X**2 + Y**2)
# Z = np.sin(R)
Z = X**2 + Y**2
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-0.01, 25.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
梯度
## mini-batch 训练
import sys, os
sys.path.append(os.getcwd())
from mnist import load_mnist
import numpy as np
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
train_size = x_train.shape[0] # (60000, 784) 的第一个元素 60000
batch_size = 100
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.reshape(1, y.size)
batch_size2 = y.shape[0]
return -np.sum(t * np.log(y + 1e-7)) / batch_size2
def numeric_diff(f, x):
h = 1e-4 # 0.0001
return (f(x + h) - f(x - h)) / 2 * h
# 求梯度
def numeric_gradient(f, x):
h = 1e-4 # 0.0001
grad = np.zeros_like(x) # 生成 和x形状相同的数组
for idx in range(x.size):
tmp_val = x[idx]
# 计算f(x+h)
x[idx] = tmp_val + h
fxh1 = f(x)
# 计算f(x-h)
x[idx] = tmp_val - h
fxh2 = f(x)
grad[idx] = (fxh1 - fxh2) / (2 * h)
x[idx] = tmp_val # 还原值
return grad
def function_2(x):
return x[0] ** 2 + x[1] ** 2
print(numeric_gradient(function_2, np.array([3.0, 4.0]))) # [6. 8.]
print(numeric_gradient(function_2, np.array([3.0, 0.0]))) # [6. 0.]
print(numeric_gradient(function_2, np.array([0.0, 2.0]))) # [0. 4.]