代码(总)
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist
from matplotlib.pyplot import MultipleLocator
def sigmoid(x):
return 1. / (1 + np.exp(-x))
def tanh(x):
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
def relu(x):
return np.where(x < 0, 0, x)
def prelu(x):
return np.where(x < 0, 0.5 * x, x)
def sigmoid(x):
return 1. / (1. + np.exp(-x))
def softmax(x):
return np.exp(x)/np.sum(np.exp(x), axis=0)
def plot_sigmoid():
x = np.arange(-10, 10, 0.5)
print(x)
y = sigmoid(x)
plt.grid()
plt.plot(x, y,label='sigmoid',color='r')
plt.legend(fontsize=20)
plt.xlabel("x",fontsize=20)
plt.ylabel("f(x)",fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlim([-10, 10])
plt.ylim([0, 1])
x_major_locator = MultipleLocator(5)
y_major_locator = MultipleLocator(0.2)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.show()
def plot_tanh():
x = np.arange(-10, 10, 0.1)
y = tanh(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.plot(x, y)
plt.xlim([-10.05, 10.05])
plt.ylim([-1.02, 1.02])
ax.set_yticks([-1.0, -0.5, 0.5, 1.0])
ax.set_xticks([-10, -5, 5, 10])
plt.tight_layout()
plt.savefig("tanh.png")
plt.show()
def plot_relu():
x = np.arange(-10, 10, 0.1)
y = relu(x)
plt.grid()
plt.plot(x, y, label='relu', color='r')
plt.legend(fontsize=20)
plt.xlabel("x",fontsize=20)
plt.ylabel("f(x)",fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlim([-10, 10])
plt.ylim([0, 10])
x_major_locator = MultipleLocator(5)
y_major_locator = MultipleLocator(5)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.show()
def plot_prelu():
x = np.arange(-10, 10, 0.1)
y = prelu(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.plot(x, y)
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.savefig("prelu.png")
plt.show()
def plot_softmax():
x = np.arange(-10, 10, 0.1)
y = softmax(x)
plt.grid()
plt.plot(x, y, label='softmax', color='r')
plt.legend(fontsize=20)
plt.xlabel("x",fontsize=20)
plt.ylabel("f(x)",fontsize=20)
plt.xlim([-10, 10])
plt.ylim([0, 0.1])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
x_major_locator = MultipleLocator(5)
y_major_locator = MultipleLocator(0.02)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.show()
if __name__ == "__main__":
plot_softmax()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具