0x01 tf.keras.layers.Conv2D

1
2
3
4
5
6
7
tf.keras.layers.Conv2D(
filters, kernel_size, strides=(1, 1), padding='valid', data_format=None,
dilation_rate=(1, 1), activation=None, use_bias=True,
kernel_initializer='glorot_uniform', bias_initializer='zeros',
kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,
kernel_constraint=None, bias_constraint=None, **kwargs
)

filters

filters:过滤器数目,更喜欢叫卷积核数目;此处设置为nn,就会产生nn个通道数。

kernel_size

kernel_size:卷积核大小,一般设置为3×35×53×3、5×5;如果卷积核长和宽一样时,可以用一个整数表示,不推荐!推荐使用(3,3)(3,3)这种格式!!!

strides

strides=(1,1):滑动步长,默认是(1,1)

padding

padding:填充格式,默认“valid”

填充格式 效果 输出大小
same 边缘用0填充 height =width = P/strides
valid 不填充 height =width = (P-S)/strides +1

input_shape

是4D的!!!(batch size, height, width, channels)

0x02 np.random.shuffle

实现两个序列完成相同的乱序,在机器学习中保持 image-label的对应关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np

a = np.arange(0,10,1)
b = np.arange(10,20,1)
print(a,b)
#result:[0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19]
state = np.random.get_state()
np.random.shuffle(a)
print(a)
#result:[6 4 5 3 7 2 0 1 8 9]
np.random.set_state(state)
np.random.shuffle(b)
print(b)
#result:[16 14 15 13 17 12 10 11 18 19]

0x03 plt.pie

为权重画饼图,权重为零的不显示。

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt

Weights = [0,0.3,0.2,0,0.1,0.15,0.25]
labels = ["Client {}".format(i) for i in range(len(Weights)) if Weights[i]]

print(labels)

plt.pie(x=[Weights[i] for i in range(len(Weights)) if Weights[i]],
labels=labels,
autopct='%1.1f%%')
plt.show()

0x04 tf.keras.Model.fit

1
2
3
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, 
validation_split=0.0, validation_data=None, shuffle=True, class_weight=None,
sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1)

返回值是 History对象,History.history记录了损失函数和其他指标的数值随epoch变化的情况,如果有验证集的话,也包含了验证集的这些指标变化情况。