畅游人工智能之海--Keras教程之回调函数(二)

畅游人工智能之海--Keras教程之回调函数(二) | tensorboard函数

回调函数 tensorboard

机器学习过程中可视化的有力工具,在keras中的API如下所示:

1
2
3
4
5
6
7
8
9
10
11
tf.keras.callbacks.TensorBoard(
log_dir="logs",
histogram_freq=0,
write_graph=True,
write_images=False,
update_freq="epoch",
profile_batch=2,
embeddings_freq=0,
embeddings_metadata=None,
**kwargs
)

参数解释

  • \(log\_dir\):存储日志文件的路径,这些日志文件将会被tensorboard解析
  • \(histogram\_freq\):模型个层的激活和权重的直方图计算的频率,单位是epoch,如果被置为0,就不进行该计算
  • \(write\_graph\):是否对graph进行可视化,如果使用该选项,日志文件会变得很大
  • \(write\_images\):是否可视化模型参数
  • \(update\_freq\):记录指标和loss的频率,可以是\('batch','epoch'\)或者一个整数,如果是一个整数就是说若干batch记录一次,如果记录的太频繁会降低记录速度
  • \(profile\_batch\):分析批次中以采样计算特征

使用方法

编写keras程序,比如一段主程序如下所示:

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
def test_tensor(epochs = 100):
# 数据预处理--the 1st core step
letter_num = len(APPEARED_LETTERS)
data, label = load_data(pic_folder) # 图片数据预处理
data_train, data_test, label_train, label_test = \
train_test_split(data, label, test_size=0.1, random_state=0)
label_categories_train = to_categorical(label_train, letter_num) # one-hot编码
label_categories_test = to_categorical(label_test, letter_num) # one-hot编码
# x_train,y_train,x_test,y_test 是表示神经网络的输入输出的常用表示
x_train, y_train, x_test, y_test = data_train, label_categories_train,data_test, label_categories_test
# 定义神经网络结构--the 2nd core step
model = None
try:
model = get_model()
except Exception as e:
print('The model you defined has some bugs')
print(traceback.print_exc())
choice = input('backup model will be used, please input 1 for continuing,0 for stopping the program.')
if choice == 1:
model = backup_model()
elif choice == 0:
print('exit!')
exit()
# 编译模型--the 3rd core step
model.compile(
optimizer='adadelta',
loss=['categorical_crossentropy'],
metrics=['accuracy'],
)
# 定义训练中间结果存储
check_point = ModelCheckpoint(
os.path.join(weight_folder, '{epoch:02d}.hdf5'))

tensorboard_callback = TensorBoard(log_dir="logs")
# 训练神经网络--the 4th core step
his = model.fit(
x_train, y_train, batch_size=128, epochs=epochs,
validation_split=0.1, callbacks=[check_point,tensorboard_callback],
)
return his

程序运行结束之后,在文件的同目录下发现多出来一个logs文件夹,这个文件夹的绝对路径命名为\(XXX\),随后在命令行中输入如下命令行tensorboard --logdir=XXX,待tensorboard启动后,使用浏览器中的本地服务器,进入https://localhost:6006/就可以看到tensorboard处理的可视化结果了,如图所示

1593781912781

目录栏目有两个选项,scalar如下所示,展示的metrics的变化情况

1593781856532

graph展示的是模型的结构,可视化如下所示:

1593782009989

完整代码已经被上传在https://github.com/1173710224/tensorboard-usage/tree/master可以通过调整模型的参数查看具体每个参数的作用