国产大模型演进全景:稀疏激活、推理跃升与低成本部署
2026/10/22 21:27:48
在感知机模型中,我们使用阶跃函数作为激活函数:
defstep_function(x):returnnp.array(x>0,dtype=np.int)阶跃函数的特点:
神经网络采用Sigmoid函数作为激活函数:
defsigmoid(x):return1/(1+np.exp(-x))Sigmoid函数的优势:
# 两种函数输出对比x_values=[-2,-1,0,1,2]sigmoid_output=[0.12,0.27,0.50,0.73,0.88]step_output=[0,0,0,1,1]近年来,ReLU函数成为主流选择:
defrelu(x):returnnp.maximum(0,x)ReLU的优势:
importmatplotlib.pyplotaspltimportnumpyasnp x=np.arange(-5,5,0.1)y_step=step_function(x)y_sigmoid=sigmoid(x)y_relu=relu(x)# 绘制三种函数对比plt.figure(figsize=(12,4))plt.subplot(1,3,1)plt.plot(x,y_step)plt.title("Step Function")plt.subplot(1,3,2)plt.plot(x,y_sigmoid)plt.title("Sigmoid Function")plt.subplot(1,3,3)plt.plot(x,y_relu)plt.title("ReLU Function")plt.tight_layout()importnumpyasnpclassActivationFunctions:"""激活函数实现类"""@staticmethoddefsigmoid(x):"""Sigmoid激活函数"""return1/(1+np.exp(-x))@staticmethoddefsigmoid_derivative(x):"""Sigmoid导数"""returnx*(1-x)@staticmethoddefrelu(x):"""ReLU激活函数"""returnnp.maximum(0,x)@staticmethoddefrelu_derivative(x):"""ReLU导数"""returnnp.where(x>0,1,0)@staticmethoddeftanh(x):"""Tanh激活函数"""returnnp.tanh(x)@staticmethoddeftanh_derivative(x):"""Tanh导数"""return1-x**2# 使用示例x=np.array([-2,-1,0,1,2])activations=ActivationFunctions()print("Sigmoid:",activations.sigmoid(x))print("ReLU:",activations.relu(x))print("Tanh:",activations.tanh(x))激活函数是神经网络的核心组件,它:
从阶跃函数到Sigmoid,再到ReLU,激活函数的发展反映了深度学习领域的进步。理解不同激活函数的特性和适用场景,是构建高效神经网络模型的基础。
思考题:在实际项目中,你会如何根据具体任务选择激活函数?欢迎在评论区分享你的经验!