简体   繁体   English

在 tensorflow 中修改预训练模型

[英]Modify pretrained model in tensorflow

I want to know how to make changes to a graph loaded from tensorflow's meta and checkpoint files like:我想知道如何对从 tensorflow 的元和检查点文件加载的图形进行更改,例如:

saver = tf.train.import_meta_graph('***.meta') saver.restore(sess,tf.train.latest_checkpoint('./'))

For example, there are old_layer1 -> old_layer2 in existing graph with pretrained weights.例如,在具有预训练权重的现有图中有old_layer1 -> old_layer2 I want to insert one then it becomes old_layer1 -> new_layer -> old_layer2 , and new_layer are randomly initialized since there are no pretrained parameter for it.我想插入一个然后它变成old_layer1 -> new_layer -> old_layer2 ,并且new_layer被随机初始化,因为它没有预训练参数。 Answer here said its impossible, since tf's graph only allow append, is this true? 这里的答案说不可能,因为 tf 的图只允许追加,这是真的吗?

So I wonder if this can be worked around by loading the pretrained layers as individual variables, and assigning pre-trained weights as initial values and connect them by myself, so that I can add new layers between old ones.所以我想知道是否可以通过将预训练层作为单个变量加载,并将预训练权重分配为初始值并自己连接它们来解决这个问题,以便我可以在旧层之间添加新层。 But I don't know how to do this in code.但我不知道如何在代码中做到这一点。

Doing this with raw tensorflow can be complicated since the tf graph does not encode directly the structure of the layers.使用原始 tensorflow 执行此操作可能很复杂,因为 tf 图不直接对层的结构进行编码。 If your model was built with tf.keras, however, this is fairly straightforward as loading a keras model also loads its layer structure.但是,如果您的模型是使用 tf.keras 构建的,则这相当简单,因为加载 keras 模型也会加载其层结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM