python - 从 TF2 中的 SavedModel 获取操作
问题描述
是否可以在保存的模型中获取对内部操作的引用?
我已经从 tfhub 下载了一个保存的模型,并且在检查saved_model.pb
文件后知道我需要哪个操作。我想获得对操作的引用,以便在推理过程中记录值。
更具体地说,我想transformer/layer_1/dropout_5/dropout/Mul:z:0
从 bert 中提取 op:
max_seq_length = 128
input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="input_word_ids")
input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="input_mask")
segment_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="segment_ids")
bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2",
trainable=True)
pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
# I think I need to add a reference to the internal op to the outputs here.
model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=[pooled_output, sequence_output])
解决方案
详尽而有用的教程-> https://www.tensorflow.org/guide/saved_model
Keras 保存模型的详细指南 ->
https://www.tensorflow.org/guide/keras/save_and_serialize
推荐阅读
- xml - 在scala中过滤Elem的子节点?
- windows - 是否可以让控制台等待另一个子进程?
- r - R如何在列中找到任何月份
- omnet++ - omnet++ 模拟 WSN 中的抗反应干扰方法
- r - 如何部分旋转数据框?
- python - 有没有办法在输出文本文件的单词之间创建空格
- flutter - Flutter Web:右键单击 -> 浏览器上下文菜单 -> 在新选项卡中打开链接
- swift - 无法转换类型“绑定”的值
'到预期的参数类型'绑定 ' - java - 更改 JTable 中特定单元格的属性(在 netbeans 中)
- swift - 从 Firebase 云消息传递接收自定义数据(布尔型、整数型)