tensorflow 高阶API
keras 模型保存和加载
ckpt 检查点设置
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, verbose=1, save_weights_only=True)
latest = tf.train.latest_checkpoint(checkpoint_dir)
model.save_weights(latest)
model.load_weights(latest)
保存整个模型(包含模型框架和权重系数):h5,不保存tf的优化器配置
model.save('my_model.h5')
new_model = keras.models.load_model('my_model.h5')
loss, acc = new_model.evaluate(test_images, test_labels)
使用keras函数式API构建复杂拓扑结构
仅配置模型:
json_string = model.to_json()
fresh_model = tf.keras.models.model_from_json(json_string)
yaml_string = model.to_yaml()
fresh_model = tf.keras.models.model_from_yaml(yaml_string)
Eager Execution
tf.enable_eager_execution()
#计算梯度(tf.GradientTape)
w = tf.Variable([[1.0]])
with tf.GradientTape() as tape:
loss = w * w
grad = tape.gradient(loss, w)
print(grad)
Estimator API:训练,评估,预测,导出以供使用操作
est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3)
Dataset API
特征列:特征列作为原始数据与模型所需的数据之间的桥梁
feature_column = input_fn()
numeric_feature_column = tf.feature_column.numeric_column(key="SepalLength")
tensorflow 低阶API
初始化变量:
session.run(tf.global_variables_initializer())
##变量存在数值关系的初始化
v = tf.get_variable("v", shape=(), initializer=tf.zeros_initializer())
w = tf.get_variable("w", initializer=v.initialized_value() + 1)
##tensorboard:
writer = tf.summary.FileWriter("/tmp/log/...", sess.graph)
##保存和恢复
saver = tf.train.Saver()
save_path = saver.save(sess, "/tmp/model.ckpt")
saver.restore(sess, "/tmp/model.ckpt")
##调试
from tensorflow.python import debug as tf_debug
sess = tf_debug.LocalCLIDebugWrapperSession(sess)