Код: Выделить всё
class SoftmaxWithLoss:
def __init__(self):
self.loss = None # Variable to store the loss value
self.y = None # Variable to store the output of the softmax function
self.t = None # Variable to store the target (label) data, which is expected to be a one-hot vector
def forward(self, x, t):
self.t = t # Store the target data
self.y = softmax(x) # Apply softmax to input 'x' to get the probabilities
self.loss = cross_entropy_error(self.y, self.t) # Calculate the cross-entropy loss
return self.loss # Return the loss value
def backward(self, dout=1):
batch_size = self.t.shape[0] # Get the batch size
dx = (self.y - self.t) / batch_size # Calculate the gradient of the loss with respect to input 'x'
return dx # Return the gradient
Подробнее здесь: https://stackoverflow.com/questions/791 ... oss-entrop
Мобильная версия