https://blog.csdn.net/weixin_43114885/article/details/90378914
4.2.4 mini-batch 版交叉熵误差的实现
one-hot格式的监督数据
def cross_entropy_error(y, t):
if y.ndim == 1: #一维数组要把第一维放到第二维,表示只有一条数据
t = t.reshape(1, t.size) #reshape函数代两维的参数
y = y.reshape(1, y.size)
batch_size = y.shape[0] #记下第一维的数值,表示有多少条数据
return -np.sum(t * np.log(y + 1e-7)) / batch_size
t为(batch_size,10), y 为(batch_size,10)
t*logy 结果还是(batch_size,10)
np.sum 后成为一个数值
y=np.array([[0,1,0.1,0,0,0,0,0,0,0],
[0,0,0.2,0.8,0,0,0,0,0,0]])
t=np.array([[0,1,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0]])#one-hot
batch_size =y.shape[0]
print( batch_size ) #batch_size =2
r= -np.sum(t * np.log(y + 1e-7)) / batch_size
print(r) #0.11157166315711126
非one-hot格式的监督数据
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 #
import numpy as np
y=np.array([[0,1,0.1,0,0,0,0,0,0,0],
[0,0,0.2,0.8,0,0,0,0,0,0]])
t_onehot=np.array([[0,1,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0]])#one-hot
t = t_onehot.argmax(axis=1)#非one-hot
print(t)#[1 3]
batch_size = y.shape[0]
print(batch_size)#2
k=y[np.arange(batch_size), t] # [y[0,1] y[1,3]]
print(k)#[1. 0.8]
r=-np.sum(np.log(y[np.arange(batch_size), t] + 1e-7))/ batch_size
print(r)#0.11157166315711126
t为(batch_size,1), y 为(batch_size,10)
y[np.arange(batch_size), t] 结果是一维的(有batch_size个值)
np.sum 后成为一个数值
import numpy as np
a=np.array([1,2,3,4])
print(a) # [1 2 3 4]
b=a.reshape(1,a.size)
print(b)#[[1 2 3 4]]