-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraph.py
More file actions
86 lines (70 loc) · 2.24 KB
/
Copy pathgraph.py
File metadata and controls
86 lines (70 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import matplotlib
matplotlib.use('TkAgg')#TkAgg是图形后端
import matplotlib.pyplot as plt
import seaborn as sns
# 设置seaborn的风格
sns.set(style="whitegrid")
# 生成数据
x = np.linspace(-10, 10, 400)
y_relu = np.maximum(0, x)
# 绘制ReLU函数
plt.figure(figsize=(8, 6))
plt.plot(x, y_relu, linewidth=2, color='royalblue')
plt.title('ReLU Activation Function', fontsize=18, fontweight='bold', color='darkblue')
plt.xlabel('Input (x)', fontsize=14)
plt.ylabel('ReLU(x)', fontsize=14)
plt.axhline(0, color='black',linewidth=0.7)
plt.axvline(0, color='black',linewidth=0.7)
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()
# 生成数据
y_sigmoid = 1 / (1 + np.exp(-x))
# 绘制Sigmoid函数
plt.figure(figsize=(8, 6))
plt.plot(x, y_sigmoid, linewidth=2, color='seagreen')
plt.title('Sigmoid Activation Function', fontsize=18, fontweight='bold', color='darkgreen')
plt.xlabel('Input (x)', fontsize=14)
plt.ylabel('Sigmoid(x)', fontsize=14)
plt.axhline(0, color='black',linewidth=0.7)
plt.axvline(0, color='black',linewidth=0.7)
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()
# 设置seaborn的风格
sns.set(style="whitegrid")
# Softmax函数实现
def softmax(x):
orig_shape = x.shape
if len(x.shape) > 1:
# Matrix
constant_shift = np.max(x, axis=1).reshape(1, -1)
x -= constant_shift
x = np.exp(x)
normlize = np.sum(x, axis=1).reshape(1, -1)
x /= normlize
else:
# Vector
constant_shift = np.max(x)
x -= constant_shift
x = np.exp(x)
normlize = np.sum(x)
x /= normlize
assert x.shape == orig_shape
return x
# Softmax输入
softmax_inputs = np.arange(-10, 10, 0.1) # 改为0.1步长增加平滑度
softmax_outputs = softmax(softmax_inputs)
# 绘制图形
plt.figure(figsize=(10, 6))
plt.plot(softmax_inputs, softmax_outputs, color='dodgerblue', linewidth=2, label='Softmax Output')
# 添加标题和标签
plt.title('Softmax Activation Function', fontsize=18, fontweight='bold', color='darkblue')
plt.xlabel('Input Values', fontsize=14)
plt.ylabel('Softmax Output', fontsize=14)
# 设置网格
plt.grid(True, linestyle='--', alpha=0.6)
# 显示图例
plt.legend(fontsize=12)
# 展示图像
plt.tight_layout()
plt.show()