class Softmax(Activation): ''' softmax nonlinear function. ''' def __init__(self): ''' There are no parameters in softmax function. ''' super(Softmax, self).__init__() def value(self, x: np.ndarray) -> np.ndarray: ''' Parameters: x is the input to the softmax function. x is a two dimensional numpy array. Each row is the input to the softmax function Returns: output of the softmax function. The returned value is with the same shape as that of x. ''' #### write your code below #### return def derivative(self, x: np.ndarray) -> np.ndarray: ''' Parameters: x is the input to the softmax function. x is a two dimensional numpy array. Returns: a two dimensional array representing the derivative of softmax function w.r.t. x. ''' #### write your code below ####
时间: 2024-02-10 13:16:18 浏览: 83
下面是对 `Softmax` 激活函数的实现,你可以将其加入到代码中:
```python
class Softmax(Activation):
'''
softmax nonlinear function.
'''
def __init__(self):
'''
There are no parameters in softmax function.
'''
super(Softmax, self).__init__()
def value(self, x: np.ndarray) -> np.ndarray:
'''
Parameters:
x is the input to the softmax function. x is a two dimensional numpy array. Each row is the input to the softmax function
Returns:
output of the softmax function. The returned value is with the same shape as that of x.
'''
exp_scores = np.exp(x)
return exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
def derivative(self, x: np.ndarray) -> np.ndarray:
'''
Parameters:
x is the input to the softmax function. x is a two dimensional numpy array.
Returns:
a two dimensional array representing the derivative of softmax function w.r.t. x.
'''
softmax_output = self.value(x)
return softmax_output * (1 - softmax_output)
```
阅读全文