Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Softmax:
- # ...
- def backprop(self, d_L_d_out):
- '''
- Performs a backward pass of the softmax layer.
- Returns the loss gradient for this layer's inputs.
- - d_L_d_out is the loss gradient for this layer's outputs.
- '''
- # We know only 1 element of d_L_d_out will be nonzero
- for i, gradient in enumerate(d_L_d_out):
- if gradient == 0:
- continue
- # e^totals
- t_exp = np.exp(self.last_totals)
- # Sum of all e^totals
- S = np.sum(t_exp)
- # Gradients of out[i] against totals
- d_out_d_t = -t_exp[i] * t_exp / (S ** 2)
- d_out_d_t[i] = t_exp[i] * (S - t_exp[i]) / (S ** 2)
- # ... to be continued
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement