def tanh(x: Variable) -> Variable: data = np.tanh(x.data) w = 1 - data**2 return x.unary_op(data, w)
def relu(x: Variable) -> Variable: data = x.data.copy() np.maximum(data, 0, data) w = data.copy() w[w > 0] = 1 return x.unary_op(data, w)
def sigmoid(x: Variable) -> Variable: data = (np.exp(-x.data) + 1)**(-1) w = -data + 1 w[:] *= data[:] return x.unary_op(data, w)