educoder常用损失函数的实现
第1关:实现常见损失函数的前向传播import numpy as npdef softmax(x):x = x - np.max(x, axis=1, keepdims=True)return np.exp(x) / np.sum(np.exp(x), axis=1, keepdims=True)class SoftmaxWithLoss:def __init__(self):self.loss
·
第1关:实现常见损失函数的前向传播
import numpy as np
def softmax(x):
x = x - np.max(x, axis=1, keepdims=True)
return np.exp(x) / np.sum(np.exp(x), axis=1, keepdims=True)
class SoftmaxWithLoss:
def __init__(self):
self.loss = None
def forward(self, x, t):
r'''
SoftMax + Cross Entropy的前向传播
Parameter:
- x: numpy.array, (B, C)
- t: numpy.array, (B)
Return:
- loss: float
'''
########## Begin ##########
y = softmax(x)
batch_size = y.shape[0]
# 为了避免log计算的时候y太小,所以加一个1e-7来避免算数错误
loss = -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
self.loss = loss
return loss
########## End ##########
class MeanSquaredError:
def __init__(self):
self.loss = None
def forward(self, y, t):
r'''
Mean Squared Error的前向传播
Parameter:
- y: numpy.array, (B, N)
- t: numpy.array, (B, N)
Return:
- loss: float
'''
########## Begin ##########
loss = 0.5 * np.sum((y - t) ** 2)
self.loss = loss
return loss
########## End ##########
第2关:实现常见损失函数的反向传播
import numpy as np
def softmax(x):
x = x - np.max(x, axis=1, keepdims=True)
return np.exp(x) / np.sum(np.exp(x), axis=1, keepdims=True)
class SoftmaxWithLoss:
def __init__(self):
self.loss = None
self.y = None
self.t = None
def forward(self, x, t):
r'''
SoftMax + Cross Entropy的前向传播
Parameter:
- x: numpy.array, (B, C)
- t: numpy.array, (B)
Return:
- loss: float
'''
y = softmax(x)
batch_size = y.shape[0]
loss = -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
self.loss = loss
self.y = y
self.t = t
return loss
def backward(self):
r'''
SoftMax + Cross Entropy的反向传播
Return:
- dx: numpy.array, (B, C)
'''
########## Begin ##########
batch_size = self.t.shape[0]
dx = self.y.copy()
dx[np.arange(batch_size), self.t] -= 1
dx = dx / batch_size
return dx
########## End ##########
class MeanSquaredError:
def __init__(self):
self.loss = None
self.y = None
self.t = None
def forward(self, y, t):
r'''
Mean Squared Error的前向传播
Parameter:
- y: numpy.array, (B, N)
- t: numpy.array, (B, N)
Return:
- loss: float
'''
loss = 0.5 * np.sum((y - t) ** 2)
self.loss = loss
self.y = y
self.t = t
return loss
def backward(self):
r'''
Mean Squared Error的反向传播
Return:
- dy: numpy.array, (B, N)
'''
########## Begin ##########
y_grad = self.y - self.t
return y_grad
########## End ##########
更多推荐


所有评论(0)