1
2
3
4
5
6
7
8
9
10
11
12
13
14
title: Linux系统上常用软件集锦
date: 2020-07-20 00:00:00
updated: 2022-01-05 00:00:00
tags: [Linux,四大组件,Activity]
type: [Linux,四大组件,Activity]
comments: Activity的生命周期完全解析
description: Activity的生命周期完全解析
keywords: Activity的生命周期完全解析
top_img:
mathjax:
katex:
aside:
aplayer:
highlight_shrink:

[TOC]

概述

源码分析

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
# 下面我们学习一下Tensorflow2.0的Keras中callback回调。
# 下面我们学习一下他的使用:Tensorboard,earlystoping、ModelCheckpoint
# 定义一个日志的文件夹
logdir = './callbacks'

if not os.path.exists(logdir):
os.mkdir(logdir)
# 输出的模型文件.输出的模型的文件的。
output_model_file = os.path.join(logdir,"fashion_mnist_model.h5")

# Tensorboard. 实例化对象传入的是一个日志文件夹

# ModelCheckpoint 实例化对象需要传入两个参数,output_model_file输出的模型文件地址
# save_best_only = True保存最好的模型文件。如果不设置,默认会保存最近的那个模型文件。

# EarlyStopping
# 三个重要的参数:
# monitor表示:我要关注的哪一个质量指标
# min_delta 是设置的一个最小的阈值。这个阈值表示这次训练和上次训练的最小的差距。
# patience: (耐心) 联系多少次的差距都比min_delta的阈值要小的话,就可以提前关闭。

# 比如:我们下面设置的训练1000次。但是如果连续十次.训练的accuracy值都比min_delta=1e-3要小,那么我们就可以提前训练。
callbacks = [
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True),
keras.callbacks.EarlyStopping(patience=10,min_delta=1e-3)
]

# 我们可以开始进行训练模型了
# 下面的方法就是我们把训练集放到这个模型里面。并且epochs = 10我们进行训练10次
# 同时我们加入了验证数据集。validation_data=(x_valid,y_valid)这样每隔一段时间他就是进行验证。
# 这个训练方法是可以返回一个值的
history=model.fit(x_train_scaled,y_train,
epochs = 1000,
validation_data=(x_valid_scaled,y_valid),
callbacks =callbacks )

callback的学习的地址:https://www.tensorflow.org/

查看训练结果

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
(base) frewen@FreweniUbuntu:~/03.ProgramSpace/20.AI/01.WorkSpace/NyxAILearning/NyxTensorflow2.0/keras$ tree
.
├── 01_tesorflow_keras_classifcation_model.ipynb
├── 02_tesorflow_keras_classifcation_model_normalize.ipynb
├── 03_tesorflow_keras_classifcation_model_callback.ipynb
├── callbacks
│   ├── fashion_mnist_model.h5
│   ├── train
│   │   └── events.out.tfevents.1640519934.FreweniUbuntu.11172.0.v2
│   └── validation
│   └── events.out.tfevents.1640519935.FreweniUbuntu.11172.1.v2
└── tensorflow-keras.py



tensorboard --logdir=callbacks
2021-12-26 20:03:08.421958: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-12-26 20:03:08.421987: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-12-26 20:03:09.587129: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2021-12-26 20:03:09.587153: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2021-12-26 20:03:09.587166: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (FreweniUbuntu): /proc/driver/nvidia/version does not exist

NOTE: Using experimental fast data loading logic. To disable, pass
"--load_fast=false" and report issues on GitHub. More details:
https://github.com/tensorflow/tensorboard/issues/4784

Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.7.0 at http://localhost:6006/ (Press CTRL+C to quit)