TensorFlow 笔记5--模型复用

TensorFlow 笔记5–模型复用


参考文档https://github.com/ageron/handson-ml/blob/master/11_deep_learning.ipynb


一、模型复用

法1:直接拷贝构建模型的代码

# 只需在session中恢复模型数据,注意路径替换
with tf.Session() as sess:
    saver.restore(sess, "./my_model_final.ckpt")

法2:恢复模型图

1、恢复模型的图

# 路径应该为自己保存模型的路径
saver = tf.train.import_meta_graph("./my_model_final.ckpt.meta")

2、获得模型的op

# 法1:查看模型有哪些op,根据op的名称获得op

# 查看op
for op in tf.get_default_graph().get_operations():
    print(op.name)
# 查看tensor
for var in tf.global_variables():
    print(var.name)    

# 根据op的名称获得op,名称一定要对应
X = tf.get_default_graph().get_tensor_by_name("X:0")
y = tf.get_default_graph().get_tensor_by_name("y:0")
accuracy = tf.get_default_graph().get_tensor_by_name("eval/accuracy:0")
training_op = tf.get_default_graph().get_operation_by_name("GradientDescent")

# 法二:在原模型中添加字典存放所需的op,重用模型时通过字典获得op

# 在原模型中添加字典存放所需的op
for op in (X, y, accuracy, training_op):
    tf.add_to_collection("my_important_ops", op)

# 重用模型时通过字典获得op
X, y, accuracy, training_op = tf.get_collection("my_important_ops")

3、在session中恢复模型数据

with tf.Session() as sess:
    saver.restore(sess, "./my_model_final.ckpt")

重复使用变量:

# 预测时重复恢复模型
tf.get_variable_scope().reuse_variables()

二、只使用模型的一部分

法一:重新构建图,恢复部分模型数据

1、重新构建图

# 需要重用的那几层必须和原模型一样
# 模型参数
n_inputs = 28 * 28  
n_hidden1 = 300 
n_hidden2 = 50  
n_hidden3 = 50  
n_hidden4 = 20  
n_outputs = 10  

X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
y = tf.placeholder(tf.int32, shape=(None), name="y")

# 构建图,添加层
with tf.name_scope("dnn"):
    hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.relu, name="hidden1")       # reused
    hidden2 = tf.layers.dense(hidden1, n_hidden2, activation=tf.nn.relu, name="hidden2") # reused
    hidden3 = tf.layers.dense(hidden2, n_hidden3, activation=tf.nn.relu, name="hidden3") # reused
    hidden4 = tf.layers.dense(hidden3, n_hidden4, activation=tf.nn.relu, name="hidden4") # new!
    logits = tf.layers.dense(hidden4, n_outputs, name="outputs")                         # new!

# 图剩下的部分和原模型的就一模一样了

2、恢复部分模型数据

# 创建saver指定要恢复的数据,注意这个saver的名称和保存现在的模型的saver不能一样
reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                               scope="hidden[123]") # regular expression
restore_saver = tf.train.Saver(reuse_vars) # to restore layers 1-3

# 保存现在的模型的saver
saver = tf.train.Saver()

# 恢复模型部分数据
with tf.Session() as sess:
    restore_saver.restore(sess, "./my_model_final.ckpt")

法二:恢复图,修改图,恢复模型数据
1、恢复图并获得所需的op

# 恢复图
saver = tf.train.import_meta_graph("./my_model_final.ckpt.meta")

# 获得所需的op,注意名称
X = tf.get_default_graph().get_tensor_by_name("X:0")
y = tf.get_default_graph().get_tensor_by_name("y:0")
hidden3 = tf.get_default_graph().get_tensor_by_name("dnn/hidden4/Relu:0")

2、修改图,添加层

# 要添加层的参数
n_hidden4 = 20  
n_outputs = 10  

# 添加层
new_hidden4 = tf.layers.dense(hidden3, n_hidden4, activation=tf.nn.relu, name="new_hidden4")
new_logits = tf.layers.dense(new_hidden4, n_outputs, name="new_outputs")

3、使用部分模型数据

# 可以恢复全部数据(只用其中一部分),也可以恢复部分层数据
with tf.Session() as sess:
    saver.restore(sess, "./my_model_final.ckpt")

三、使用其它框架的权重

如果只有模型的权重数据没有图,可以自己构建图,然后加载权重到相应的层。

法一:

# 模型参数
n_inputs = 2
n_hidden1 = 3

# 权重数据 
original_w = [[1., 2., 3.], [4., 5., 6.]] # Load the weights from the other framework
original_b = [7., 8., 9.]                 # Load the biases from the other framework

# 构建图
X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.relu, name="hidden1")
# [...] Build the rest of the model

# 获得要加载权重的变量名
graph = tf.get_default_graph()
assign_kernel = graph.get_operation_by_name("hidden1/kernel/Assign")
assign_bias = graph.get_operation_by_name("hidden1/bias/Assign")
init_kernel = assign_kernel.inputs[1]
init_bias = assign_bias.inputs[1]

init = tf.global_variables_initializer()

with tf.Session() as sess:
    # 权重赋给相应的变量后,喂给模型
    sess.run(init, feed_dict={init_kernel: original_w, init_bias: original_b})
    # [...] Train the model on your new task
    print(hidden1.eval(feed_dict={X: [[10.0, 11.0]]}))  # not shown in the book

法二:

n_inputs = 2
n_hidden1 = 3

original_w = [[1., 2., 3.], [4., 5., 6.]] # Load the weights from the other framework
original_b = [7., 8., 9.]                 # Load the biases from the other framework

X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.relu, name="hidden1")
# [...] Build the rest of the model

# Get a handle on the variables of layer hidden1
with tf.variable_scope("", default_name="", reuse=True):  # root scope
    hidden1_weights = tf.get_variable("hidden1/kernel")
    hidden1_biases = tf.get_variable("hidden1/bias")

# Create dedicated placeholders and assignment nodes
original_weights = tf.placeholder(tf.float32, shape=(n_inputs, n_hidden1))
original_biases = tf.placeholder(tf.float32, shape=n_hidden1)
assign_hidden1_weights = tf.assign(hidden1_weights, original_weights)
assign_hidden1_biases = tf.assign(hidden1_biases, original_biases)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    sess.run(assign_hidden1_weights, feed_dict={original_weights: original_w})
    sess.run(assign_hidden1_biases, feed_dict={original_biases: original_b})
    # [...] Train the model on your new task
    print(hidden1.eval(feed_dict={X: [[10.0, 11.0]]}))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MOOC(大规模开放式在线课程)是一种通过网络平台开设的在线教育课程,可以为广大学习者提供方便灵活的学习机会。人工智能实践:TensorFlow笔记,是由北京大学推出的一门针对人工智能领域的实践课程,旨在帮助学习者掌握使用TensorFlow框架进行深度学习的基本方法和技巧。 该课程的代码提供了一系列丰富的示例和实践项目,通过这些代码我们可以了解和掌握TensorFlow的使用方法。其中包括数据处理、模型构建、模型训练与评估等关键步骤。通过学习和实践,我们可以学会如何搭建神经网络模型,进行图像分类、文本生成等任务。 在这门课程中,北京大学的代码示例主要围绕深度学习的常用库TensorFlow展开,通过给出具体的代码实现,解释了每部分的原理和操作方法,帮助学习者理解基本概念和技术,熟悉TensorFlow框架和编程语言的使用。 此外,这门课程还涵盖了一些实践项目,例如基于TensorFlow的手写数字识别、图像分类与预测、文本生成等。通过完成这些实践项目,我们可以加深对TensorFlow的理解并提高实践能力。 总之,人工智能实践: TensorFlow笔记 - 北京大学代码是一门结合了理论与实践的在线课程,通过教授深度学习的基本概念和TensorFlow的应用方法,帮助学习者掌握人工智能领域的基本技能。通过这门课程,我们可以学习到TensorFlow的使用方法,掌握一定的实践能力,并将这些知识应用于实际项目当中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值