(a) 推导sigmoid的导数公式
y = 1/(1+exp(-x))
Answer:
y' = y*(1-y)
sigmoid的导数形式是十分简洁的,这也是sigmoid函数使用广泛的一个原因。
(b) 当使用交叉熵作为loss function时,推导其梯度公式,输入的y是一个one-hot向量
Answer:
当我们有一个分值向量f,损失函数对这个分值向量求导的结果等于向量里每个类别对应的概率值,但除了那个正确类别的概率值,它要再减去1。例如,我们的概率向量为p = [0.2, 0.3, 0.5],第二类为正确的类别,那么的分值梯度就为df = [0.2, -0.7, 0.5]。
也是一个很重要的结论,在cs231n的作业已多次用到。
(c) 求一个单隐藏层的神经网络对于输入x的梯度
即 h=sigmoid(xW1+b1),y=softmax(hW2+b2)求y关于x的梯度
Answer:
令z2 = hw2+b2,z1=xW1+b1
则可以用链式法则求解。
(d) 求上题中的网络中有多少参数
输入为Dx维,输出为Dy维,隐藏单元有H个
Answer:
输入为Dx维,channel为1,隐藏层有H个单元,所以W1有DxH个参数,b1有H个参数
同理,W2有DDy个参数,b2有Dy个参数。
(e)sigmoid及其梯度求解
def sigmoid(x):
"""
Compute the sigmoid function for the input here.
Arguments:
x -- A scalar or numpy array.
Return:
s -- sigmoid(x)
"""
s = 1/(1+np.exp(-x))
return s
def sigmoid_grad(s):
"""
Compute the gradient for the sigmoid function here. Note that
for this implementation, the input s should be the sigmoid
function value of your original input x.
Arguments:
s -- A scalar or numpy array.
Return:
ds -- Your computed gradient.
"""
ds = s*(1-s)
return ds