-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
158 lines (119 loc) · 4.6 KB
/
utils.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import random
import math
import numpy as np
from scipy.stats import t
import tensorflow as tf
from tensorflow.keras import layers, models
def get_random_number():
mu, sigma = 0, 0.1 # 均值和标准差
x = []
while len(x) < 1:
r = random.uniform(0, 1) # 生成0到1之间的随机数
y = math.sqrt(-2 * math.log(r)) * math.cos(2 * math.pi * r) # 使用Box-Muller变换生成正态分布随机数
y = y * sigma + mu # 调整均值和标准差
if -0.2 <= y <= 0.2: # 限制随机数在-0.1到0.1之间
x.append(y)
return x[0]
def get_t_random_number():
# 设置自由度和均值
df = 10
mean = 0
# 生成一个t分布的随机数
x = t.rvs(df, loc=mean)
# 缩放和平移
x_scaled = (x - mean) / np.sqrt(df / (df - 2))
x_shifted = x_scaled * 0.1
# 将结果限制在-0.1到0.1的范围内
if x_shifted < -0.1:
x_shifted = -0.1
elif x_shifted > 0.1:
x_shifted = 0.1
return x_shifted
def get_poly_random_number(num_points):
# 假设有10个点,x和y坐标都相同
x = np.zeros(num_points)
y = np.zeros(num_points)
# 生成随机半径
r = np.random.uniform(0, 1, size=num_points)
# 生成随机角度
theta = np.random.uniform(0, 2 * np.pi, size=num_points)
# 将极坐标转换为直角坐标
x = r * np.cos(theta)
y = r * np.sin(theta)
# 将x和y坐标平移,使得它们的均值为0
x = x - np.mean(x)
y = y - np.mean(y)
# 将x和y坐标缩放,使得它们的标准差为1
x = x / np.std(x)
y = y / np.std(y)
# 将x和y坐标缩放,使得它们的范围在-0.1到0.1之间
x = x * 0.1
y = y * 0.1
return x, y
def compute_loss_coefficient(step, total_steps, decay_factor):
# Ensure step is within the range [1, total_steps]
step = max(1, min(step, total_steps))
# Calculate the loss coefficient for the current step
loss_coefficient = decay_factor ** (total_steps - step)
return loss_coefficient
def residual_block(input_data, filters, stride=1):
shortcut = input_data
# 第一个卷积层
x = layers.Conv2D(filters, 3, strides=stride, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(input_data)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
# 第二个卷积层
x = layers.Conv2D(filters, 3, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
# 如果输入和输出尺寸不一致,使用1x1卷积调整shortcut的尺寸
if stride > 1 or input_data.shape[-1] != filters:
shortcut = layers.Conv2D(filters, 1, strides=stride, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(shortcut)
shortcut = layers.BaStchNormalization()(shortcut)
shortcut = layers.ReLU()(shortcut)
# 添加卷积和dropout
shortcut = layers.Conv2D(filters, 3, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(shortcut)
shortcut = layers.BatchNormalization()(shortcut)
shortcut = layers.Dropout(0.3)(shortcut)
shortcut = layers.ReLU()(shortcut)
shortcut = layers.Conv2D(filters, 3, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(shortcut)
shortcut = layers.BatchNormalization()(shortcut)
shortcut = layers.ReLU()(shortcut)
# 残差连接
x = layers.Add()([x, shortcut])
x = layers.ReLU()(x)
return x
def create_wrn28_10():
input_data = layers.Input(shape=(28, 28, 1))
x = layers.Conv2D(16, 3, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(input_data)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
# 第一个残差块组
x = residual_block(x, 16)
for _ in range(9):
x = residual_block(x, 16)
# 第二个残差块组
x = residual_block(x, 32, stride=2)
for _ in range(9):
x = residual_block(x, 32)
# 第三个残差块组
x = residual_block(x, 64, stride=2)
for _ in range(9):
x = residual_block(x, 64)
# 全局平均池化层
x = layers.GlobalAveragePooling2D()(x)
# 输出层
output = layers.Dense(10, activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.01))(x)
model = models.Model(inputs=input_data, outputs=output)
return model
if __name__ == "__main__":
# print(get_poly_random_number(1000))
model = create_wrn28_10()
print(model.summary())